PDA

View Full Version : [Resolved] Total column about others columns values ?



khatuido
1 Mar 2010, 5:18 AM
I got a grid with 2 columns A and B

Is there a simple way to create a third column which is the sum : C = A + B

Exemple grid

A B C
10 5 15
7 5 12
3 11 14
...

fay
1 Mar 2010, 5:28 AM
Just add another column to your grid, add a renderer and use this to calculate the total:


var grid = new Ext.grid.GridPanel({
//...
columns: [
{header: 'C', renderer: function(value, metadata, record, rowIndex, colIndex, store)
{
return record.data.A + record.data.B;
}
},
//...
],

khatuido
1 Mar 2010, 6:55 AM
It's pretty good but i need put a "dataIndex" else it doesn't work.

So i decide add a dataIndex : 'total'

It works but the sort doesn't work, it sort on the second column. Do you know why?

fay
1 Mar 2010, 7:01 AM
Did you try:


var grid = new Ext.grid.GridPanel({
//...
columns: [
{header: 'Total', sortable: true, dataIndex: 'total', renderer: function(value, metadata, record, rowIndex, colIndex, store)
{
record.data.total = record.data.A + record.data.B;
return record.data.total;
}
},
//...
],

Animal
1 Mar 2010, 7:12 AM
This is a case for a convert function, not a renderer. The Field must exist, not just the data property.

khatuido
1 Mar 2010, 7:27 AM
I am little lost, i just want a column which is the sum about other column. And it could be better if i can sort this column.

What is a convert function?

if i do like this :
record.data.total = record.data.A + record.data.B;
return record.data.total;

total must be added in my field ? even if there is no data behind it?

In fact this grid is something like this :

var store = new Ext.data.ArrayStore({
data : [['CU1G',0,0,0,0],['CU2G',0,0,0,0],['CU3G',0,0,0,0]],
fields: ['0','1','2','3','4'],
autoLoad:true
});

So i need add a field 'total' even if there is no data behind?

khatuido
1 Mar 2010, 7:38 AM
Did you try:


var grid = new Ext.grid.GridPanel({
//...
columns: [
{header: 'Total', sortable: true, dataIndex: 'total', renderer: function(value, metadata, record, rowIndex, colIndex, store)
{
record.data.total = record.data.A + record.data.B;
return record.data.total;
}
},
//...
],

i tried something like that but the sort still doesn't work very well. It sort on the second column.

the display result is good only the sort on this new column doesn't work. And i tried remove the sort with put on the columnModel "sortable : false" but there is still a sort possible.

Condor
1 Mar 2010, 7:44 AM
Use a calculated field (http://www.extjs.com/forum/showthread.php?t=28826).

khatuido
2 Mar 2010, 6:21 AM
Yeah with this i can do what i wanted, thanks!