Related
let's say I have a list like the example below
<Categories>myList = [
Categories(
nameCategory: 'Book',
amount: '20'
),
Categories(
nameCategory: 'Book',
amount: '40'
),
Categories(
nameCategory: 'Food',
amount: '20'
),
Categories(
nameCategory: 'Food',
amount: '15'
),
];
How I can combine the duplicate values of that list and count the value of the list based on name ??
I can combine the list and the count value of the list but that only works just in a general list like sum total
what I want to do is make a new List but only combine several parts that share the same property like the same category or same class like that
this is an example what I want to achieve
<Categories> anotherList= [
Categories(
nameCategory: 'Book',
amount: '60'
),
Categories(
nameCategory: 'Food',
amount: '35'
),
];
I would replace your List<Categories> with a Map<String, Categories>. Then you can easily look up the Categories object given its name and mutate the existing Categories object. For example, something like:
var mergedCategories = <String, Categories>{};
for (var categories in myList) {
var name = categories.nameCategory;
var amount = categories.amount;
(mergedCategories[name] ??= Categories(nameCategory: name, amount: 0))
.amount += amount;
}
You're essentially trying to get an aggregate value from a list, which is what List.fold is meant to help with.
Here's an example of how you might use it:
class Category {
final String name;
int amount;
Category({required this.name, required this.amount});
String toString() => "Category(name: $name, amount: $amount)";
}
void main() {
final categories = [
Category(
name: 'Book',
amount: 20
),
Category(
name: 'Book',
amount: 40
),
Category(
name: 'Food',
amount: 20
),
Category(
name: 'Food',
amount: 15
),
];
/**
* Here is where the aggregation is done
*/
final List<Category> aggregated = categories.fold([], (list, item) {
try {
// Check whether the category is already in the aggregate
final existingCategory = list.firstWhere((c) => c.name == item.name);
// Category is already in the list, so just add the amount of the current item.
existingCategory.amount += item.amount;
return list;
} catch (_) {
// The category has not yet been added - so add it here
list.add(item);
return list;
}
});
print(aggregated);
}
I've changed your category class a bit for simplicity, but the principle should be the same. You can read more about the fold function here: https://api.dart.dev/stable/2.13.4/dart-core/Iterable/fold.html
A pretty straightforward method is by using the groupBy function provided by the collection.dart package.
import 'package:collection/collection.dart';
groupBy<Categories, String>(list, (c) => c.nameCategory).values.map(
(list) => list.reduce(
(a, b) => new Categories(a.nameCategory, a.amount + b.amount)
)
);
In this list, I want to display out all the items that contains this specific name.
My list items: ['US', 'SG', 'US']
print(list.contains("US"));
Using .contains() returns me a true or false but it doesn’t return me the list of strings that contains that. I want to only extract out the items that has 'US' from the list. In this case, there's 2 of it. Please help!
You can try doing it the following way -
List<String> myList = ['US', 'SG', 'US'];
print(myList.where((item) => item.contains("US")));
You can also display it directly inside a Text widget in the following way -
Text(
myList.where((item) => item.contains("US")).join(" "),
//The join function joins the elements of the Iterable into a single string with the separator provided as an argument.
),
Hope this helps!
UPDATE:
To display each of the word separately as a list you can display them inside a Column in the following way -
Column(
children: myList.map((value) {
if(value.contains("US")){
return Text(value,);
} else {
return Container();
//Return an empty Container for non-matching case
}
}).toList(),
)
The same thing can be used inside a ListView instead of Column if you want it to be scrollable.
Something like this?
var myList = ['US', 'SG', 'US'];
myList.forEach((w){
if(w == "US")
print(w);
});
To show:
class SO extends StatelessWidget {
var myList = ['US', 'SG', 'US'];
#override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
mainAxisSize: MainAxisSize.min,
children: myList.where((w) => w == "US").map((w) => Text(w)).toList(),
),
);
}
}
or for a single line display use, Text instead of Column widget mentioned above
Text(myList.where((w) => w.contains("US")).join(" "))
If you are using "list.contains". It will only show the list exist or not , If you want to print the value you have to use follow :
var fruits = [‘banana’, ‘pineapple’, ‘watermelon’];fruits.forEach((fruit) => print(fruit)); // => banana pineapple watermelon
If you want to print just banana then you can use this
var fruits = [‘banana’, ‘pineapple’, ‘watermelon’];fruits.(fruit) => print(fruit[0]); // => banana
Firstly, I will be using ember-cli for development and I get JSON from the server in embedded format:
Single Objects
{
"id" : 1,
"name" : "Tom",
"surname" : "Smith",
"address" : {
"id" : 2,
"street" : "23",
"city" : "...",
...
}
}
Arrays
[
{
"id" : 1,
"name" : "Tom",
"surname" : "Smith",
"address" : {
"id" : 2,
"street" : "23",
"city" : "...",
...
}
},
{
"id" : 2,
"name" : "Tom",
"surname" : "Smith",
"address" : {
"id" : 4,
"street" : "23",
"city" : "...",
...
}
},
...
]
I have worked out how to append the prefix onto each payload using the following in RestAdapter.
export default DS.RESTSerializer.extend({
extract : function(store, type, payload, id, requestType) {
var typeKey = type.typeKey,
typeKeyPlural = typeKey.pluralize();
//Add name of model to payload as expected by ember data
var modelName = Array.isArray(payload) ? typeKeyPlural : typeKey;
var normalizedPayload = {};
normalizedPayload[modelName] = payload;
return this._super(store, type, normalizedPayload, id, requestType);
},
)}
I have searched around all over the place and can see all these different ways of embedding records in ember.
The official docs say to us the DS.EmbeddedRecordsMixin Class. But this would mean I would need to create a DS.ActiveModelSerializer for every single model, I would rather define the attribute in the model itself {embedded : "always"}.
http://emberjs.com/api/data/classes/DS.EmbeddedRecordsMixin.html
This actually sort of worked but is obviously old because the parameters have since changed. It is a bit of a hack in my opinion.
http://mozmonkey.com/2013/12/serializing-embedded-relationships-ember-data-beta/
There is this project, but no docs or example of how to get it working.
https://github.com/pixelhandler/ember-data-extensions
All in all I am very confused.
The correct approach is to use a serializer and the DS.EmbeddedRecordsMixin.
You are really fighting against separation of concerns by specifying serializer options in your models.
So, that said, lets get down to the evil.
You can simply copy DS.EmbeddedRecordsMixininto your application and modify the way it checks its options to instead examine the relationship meta data on the model type. You can then extendyour default ApplicationSerializer with your custom mixin so that you don't have to specify a serializer for all your models.
You will need to modify the following function, from this:
attrsOption: function(attr) {
var attrs = this.get('attrs');
return attrs && (attrs[camelize(attr)] || attrs[attr]);
}
To something like this (note the extra param):
attrsOption: function(attr, type) {
var meta = type.metaForProperty(attr) || type.metaForProperty(camelize(attr));
return meta && meta.options;
}
You will also need to modify all the callers of attrsOption to pass the model type down, but you will then have your very own embedded record mixin that gets its options from the model type.
Then when specifying your model relationships you can use the embedded record options as follows:
address: DS.belongsTo('address', { embedded: 'always' })
I am using Ignite UI grid.
The columns is dynamically build from the database like this:-
$.post('/Main/GetColumns',function(data){
$("#mygrid").igGrid({
columns: data,
width: "100%",
height: "100%",
})
});
The problem is that i dont know which of the column will be of datatype number since data is comming from database for columns and i have to right align the numeric columns.
The only code i have found is
args.owner.element.find("tr td:nth-child(3)").css("text-align", "right");
to set 3rd column as right align.
Since i dont know the column order, i am only left to check for datatype and right align the column,
Is there any way to align column on basis of datatype or any other method to do this?
The data type if the column is used for it's representation(formatting) and editing behavior, but there's no extra markup generated that you can use to target with styling.
However, you are building column definitions server side, where you know exactly what type each column is while creating its definition, no?
Update: It's been a while since the original answer and for future reference you can use the columnCssClass to apply your class to the actual TD rather than the template. The latter is still a valid option for advanced tinkering.
Easiest way I can think of is through Column templates - this way you can add whatever styling / formatting to the columns. For example, based of whatever logic you need, you return some columns as:
{
key: 'status',
dataType: 'bool',
headerText: 'Status',
template: '<div class="rightAlign"> ${status} </div>'
}
You apply "text-align:right;" though the class and skip adding template for columns that should be with default look. Since this definition is generated on the server (imagine my example uses Node.js :P ) you can have those templates static, or create them differently each time - it's up to you.
JSFiddle: http://jsfiddle.net/damyanpetev/wsZ8c/
Note: Make sure you use a block (div,p) in this case as you need something that will take up the entire grid cell in order to align text inside.
If that solution doesn't fit, you will have to go through columns and apply styling on the client in a similar way you were thinking of.
Here is how I dynamically align the text in the columns in the infragistics igHierarchicalGrid according to their data types:
$("#grid1").on("iggriddatarendered", function (event, args) {
var columns = $("#grid1").igHierarchicalGrid("option", "columns");
//var RDate = $("#grid1").igHierarchicalGrid("getCellValue", 1, 1);
var columnIndex = 0;
var trtd = 2;
for (var idx = 0; idx < columns.length; idx++) {
if (columns[idx].dataType == "number" || columns[idx].dataType == "double")
args.owner.element.find("tr td:nth-child(" + trtd + ")").css("text-align", "right");
if (columns[idx].dataType == "string" || columns[idx].dataType == "date")
args.owner.element.find("tr td:nth-child(" + trtd + ")").css("text-align", "left");
columnIndex++;
trtd = columnIndex + 2;
}
});
As you see I am starting with vartd = 2 and this is because there are 2 elements in the table
(I use hierachcical grid) before the columns in the grid are available. You must debug and investigate if in your case
the columns of the grid are coming after the second DOM element or after the first.
In easy way you can add css into columnCssClass property and applied into grid where you were define column information
Style:
<style>
.right-align {
text-align: right;
}
.left-align {
text-align: left;
}
.center-align {
text-align: center;
}
</style>
and grid code snippet:
{ headerText: 'Option', key: "Option", dataType: "string", width: "10%", hidden: true },
{ headerText: 'ID', key: "Program_Id", dataType: "string", width: "10%", columnCssClass: "right-align" },
{ headerText: 'Desc', key: "Program_Des", dataType: "string", width: "10%", columnCssClass: "left-align" },
{ headerText: 'Status', key: "program_Status", dataType: "Bool", width: "10%", columnCssClass: "center-align" },
I use numberField in ExtJS Form and want to enter only positive numbers, in range of 0-99 and it should accept only 2 characters (and not more than 2).
{
xtype:"textfield",
allowNegative: false,
allowDecimals: false,
minValue: 0,
maxValue: 99,
maxLength: 2
}
gives an error in above code but it is accepting more then 2 characters.
I also tried below but issue is same:
{
xtype:"textfield",
regex: /^\d{0,2}$/,
regexText: "<b>Error</b></br>Invalid Number entered.",
validator: function(v) {
return /^\d{0,2}$/.test(v)?true:"Invalid Number";
}
}
How to restrict to input more then 2 characters?
if you're using version 3, the TextField's maxLength documentation describes using the autoCreate property to state the maximum length (the doc example shows a NumberField but it is also supported by the TextField class):
maxLength : Number Maximum input field length allowed by validation
(defaults to Number.MAX_VALUE). This behavior is intended to provide
instant feedback to the user by improving usability to allow pasting
and editing or overtyping and back tracking. To restrict the maximum
number of characters that can be entered into the field use autoCreate
to add any attributes you want to a field, for example:
var myField =
new Ext.form.NumberField({
id: 'mobile',
anchor:'90%',
fieldLabel: 'Mobile',
maxLength: 16, // for validation
autoCreate: {tag: 'input', type: 'text', size: '20', autocomplete:
'off', maxlength: '10'} });
Using version 4, a TextField has the enforceMaxLength property.
If you're set on using a regex, both versions support the maskRe property, although I don't think this prevents invalid values that are pasted.
Not sure if vtype was there before ExtJS 4.x but this is how you can use vtype.
var validMileRadius = /^([0-9]{1,4})/;
Ext.apply(Ext.form.field.VTypes, {
// vtype validation function
radius: function(val, field) {
return validMileRadius.test(val);
},
radiusText: 'Not a Valid Radius.'
});
{
xtype: 'textfield',
width: 40,
maxLength : 4,
style : {marginLeft:'10px'},
id : 'cityRadius',
enforceMaxLength :4,
vtype : 'radius'
}
Thanks
Punith
FOR EVERYBODY, WHO STUCK ON THE SAME
For ExtJS 4.x You even don't need to create VType. From ExtJS 4.x documentation for Ext.form.field.Number:
enforceMaxLength : Boolean
True to set the maxLength property on the underlying input field. Defaults to false
So, You just have to specify maxLength and set enforceMaxLength to true. According on previous post it might look like this:
{
xtype: 'textfield',
width: 40,
maxLength : 4,,
enforceMaxLength : true
style : {marginLeft:'10px'},
id : 'cityRadius'
}
Just a helper, but to restrict a textfield to allow only numbers you can set the properties of the text field with the attribute "maskRe". It allows you to create masks with regular expressions. Here is a mask that allows you to enter only the numbers:
{
xtype: 'textfield',
fieldLabel: 'Text Field(numbers-only)',
enforceMaxLength:true, //Restrict typing past maxLength: n
maxLength: 8, //Set max length validation
maskRe:/[0-9.]/ //Allow only numbers
},
maxLength: 2,
enforceMaxLength: true,
For dynamically setting the maxLength, I came up with this override:
Ext.override(Ext.form.field.Number, {
/**
* Setter: this method will set the maxLength variable on our NumberField
* class, and on its actual dom element, so we can actually restrict the user
* from entering over the maxLength
* #param {Number} maxLength
*/
setMaxLength: function(maxLength) {
this.maxLength = maxLength;
var inputEl = this.inputEl;
if (inputEl) {
var domEl = inputEl.dom;
if (domEl) {
domEl.maxLength = maxLength;
}
}
},
/**
* Shortcut method for setting the maxLength based on the maxValue... if
* maxValue is not set, then 0 is used, which means the maxLength will be 1
*/
setMaxLengthFromMaxValue: function() {
var maxValue = this.maxValue || 0;
if (maxValue >= 0) {
this.setMaxLength(maxValue.toString().length);
}
}
});
Its very simple use maskre config to restrict textfield.
if will allows you to enter just numbers & alphabets.
xtype: 'textfield',
fieldLabel: 'Text Field(numbers-only)',
enforceMaxLength:true,
maxLength: 8,
maskRe: /[A-Za-z0-9]/