1. Excel formula can be executed ( instead of defined ) and its value returned in column render callback. Please note that I've used undocumented grid.iFormulas.exec method to solve this use case.
render: function(ui){
var ri = ui.rowIndx+1,
ci = ui.colIndx,
fn = "SUM("+ pq.toLetter(ci-7) + ri + ":"+ pq.toLetter(ci-1) + ri +")";
return this.iFormulas.exec( fn ).toString();
}
2. Apply some formula on parent row cell by referring respective child row cells
columnTemplate: { minWidth: '10%', maxWidth: '80%', width: 100,
render: function(ui){
var rd = ui.rowData, di = ui.dataIndx, vals = [], children = rd.children;
if( (typeof(rd[di]) != "string") && children ){
vals = children.map(function(r){
return r[di]
})
rd[di] = pq.aggregate.sum(vals);
return rd[di].toString();
}
}
},
Complete code:
$(function () {
var data = [
{
"id": 0,
"Item": "Product1",
"2_MAR_MON": 1,
"3_MAR_TUE": 2,
"4_MAR_WED": 3,
"5_MAR_THU": 4,
"6_MAR_FRI": 5,
"7_MAR_SAT": 6,
"8_MAR_SUN": ''
},
{
"id": 1,
"Item": "Product2",
"2_MAR_MON": 7,
"3_MAR_TUE": 8,
"4_MAR_WED": 9,
"5_MAR_THU": 10,
"6_MAR_FRI": 11,
"7_MAR_SAT": 12,
"8_MAR_SUN": ''
},
{
"id": 2,
"Item": "GroupProduct",
pq_rowprop:{edit: false},//make the row uneditable.
/*pq_fn: {
"2_MAR_MON": "=SUM(CONCATENATE(CHAR(COLUMN()),ROW()+1,\":\",(CHAR(COLUMN())),ROW()+3))",
"3_MAR_TUE": "=SUM(CONCATENATE(CHAR(COLUMN()),ROW()+1,\":\",(CHAR(COLUMN())),ROW()+3))",
"4_MAR_WED": "=SUM(CONCATENATE(CHAR(COLUMN()),ROW()+1,\":\",(CHAR(COLUMN())),ROW()+3))",
"5_MAR_THU": "=SUM(CONCATENATE(CHAR(COLUMN()),ROW()+1,\":\",(CHAR(COLUMN())),ROW()+3))",
"6_MAR_FRI": "=SUM(CONCATENATE(CHAR(COLUMN()),ROW()+1,\":\",(CHAR(COLUMN())),ROW()+3))",
"7_MAR_SAT": "=SUM(CONCATENATE(CHAR(COLUMN()),ROW()+1,\":\",(CHAR(COLUMN())),ROW()+3))"
},*/
"children": [
{
"id": 3,
"Item": "Sub Product1",
"2_MAR_MON": 7,
"3_MAR_TUE": 8,
"4_MAR_WED": 9,
"5_MAR_THU": 10,
"6_MAR_FRI": 11,
"7_MAR_SAT": 12,
"8_MAR_SUN": ''
},
{
"id": 4,
"Item": "Sub Product2",
"2_MAR_MON": 7,
"3_MAR_TUE": 8,
"4_MAR_WED": 9,
"5_MAR_THU": 10,
"6_MAR_FRI": 11,
"7_MAR_SAT": 12,
"8_MAR_SUN": ''
},
{
"id": 5,
"Item": "Sub Product3",
"2_MAR_MON": 7,
"3_MAR_TUE": 8,
"4_MAR_WED": 9,
"5_MAR_THU": 10,
"6_MAR_FRI": 11,
"7_MAR_SAT": 12,
"8_MAR_SUN": ''
}
]
},
{
"id": 6,
"Item": "Product4",
"2_MAR_MON": 7,
"3_MAR_TUE": 8,
"4_MAR_WED": 9,
"5_MAR_THU": 10,
"6_MAR_FRI": 11,
"7_MAR_SAT": 12,
"8_MAR_SUN": ''
}
];
var obj = {
height: 'flex',
maxHeight: 500,
resizable: true,
stripeRows: false,
treeModel: {
dataIndx: 'Item'
},
scrollModel: { autoFit: true },
cellSave: function(){
//cell changes affect other cells.
this.refresh();
},
columnTemplate: { minWidth: '10%', maxWidth: '80%', width: 100,
render: function(ui){
var rd = ui.rowData, di = ui.dataIndx, vals = [], children = rd.children;
if( (typeof(rd[di]) != "string") && children ){
vals = children.map(function(r){
return r[di]
})
rd[di] = pq.aggregate.sum(vals);
return rd[di].toString();
}
}
},
colModel: [
{ title: 'Item', dataType: 'string', dataIndx: 'Item' },
{ title: '2_MAR_MON', dataType: 'string' , dataIndx: '2_MAR_MON' },
{ title: '3_MAR_TUE', dataType: 'integer', dataIndx: '3_MAR_TUE' },
{ title: '4_MAR_WED', dataType: 'integer', dataIndx: '4_MAR_WED' },
{ title: '5_MAR_THU', dataType: 'integer', dataIndx: '5_MAR_THU' },
{ title: '6_MAR_FRI', dataType: 'integer', dataIndx: '6_MAR_FRI' },
{ title: '7_MAR_SAT', dataType: 'integer', dataIndx: '7_MAR_SAT' },
{ title: '8_MAR_SUN', dataType: 'integer', dataIndx: '8_MAR_SUN' },
{ title: 'Summary', dataIndx: 'W2020-08_Week', editable: false,
render: function(ui){
var ri = ui.rowIndx+1,
ci = ui.colIndx,
fn = "SUM("+ pq.toLetter(ci-7) + ri + ":"+ pq.toLetter(ci-1) + ri +")";
return this.iFormulas.exec( fn ).toString();
}
}
],
dataModel: { data: data }
};
pq.grid("#tree_grid", obj);
});