PDA

View Full Version : Count total amount of items



coshmos
26 Apr 2012, 5:37 AM
Hello everyone, I'm novice in ExtJS.

I've got a grid with 2 columns.


Product
Amount


Apple
2


Bread
5


Orange
8













Amount is the numberfield. And I need to count all amounts of all products and append it to label in another fieldcontainer (of course grid and this label in different functions /:)). So, what should I do to make this possible?

scottmartin
26 Apr 2012, 10:26 AM
Have a look at field.convert to create a calculated field:
http://docs.sencha.com/ext-js/4-0/#!/api/Ext.data.Field-cfg-convert

Regards,
Scott.

slemmon
26 Apr 2012, 6:19 PM
If you're looking to get a total from a column you might use:
http://docs.sencha.com/ext-js/4-1/#!/api/Ext.data.Store-method-sum



var grid = ?Ext.widget('gridpanel', {
title: 'Sample grid'
, width: 300
, height: 300
, renderTo: Ext.getBody()
, columns: [{
text: 'Product'
, dataIndex: 'product'
}, {
text: 'Amount'
, dataIndex: 'amount'
}]
, store: {
fields: ['product', 'amount']
, data: [{
product: 'Apple'
, amount: 2
}, {
product: 'Bread'
, amount: 5
}, {
product: 'Orange'
, amount: 8
}]
}
});


var field = Ext.widget('textfield', {
fieldLabel: 'The Sum'
, renderTo: Ext.getBody()
, margin: '10 0 0 0'
});


Ext.widget('button', {
text: 'Get Sum'
, renderTo: Ext.getBody()
, margin: '10 0 0 0'
, handler: function () {
field.setValue(grid.getStore().sum('amount'))
}
});

coshmos
26 Apr 2012, 10:06 PM
Thanks!

coshmos
26 Apr 2012, 10:07 PM
Thanks! It's better than my:


cellEditing.on('edit', function (x, y, z) {
var i = 0;
var totalAmount = 0;
store.each(function (x) {
totalAmount += x.get('amount');
i++;
});
Ext.getCmp('totalAmountLabel').setText('Total amount: ' + totalAmount);
});