SMART datagrid v.1 > Examples

Back  Forward

Row Summary  Example

We use Calculated Column or Derived Field to display the total values of each row. In two "Sum" columns below, the first is connected to Derived Field, and the second is Calculated Column. In this example, though two cases show the same result, the intended use of Calculated Column and Derived Field can be different. Most of all, the footer of Calculated Column cannot display the sum as in the grid below. It is because the value of the cells contained in Calculated Column is not separately stored but temporarily calculated when displayed. 

Grid - 1
rows
Code -1
    // Set Derived Field.
    ds.setFields([...], [
        {
            fieldName: "fld_sum",
            dataType: "number",
            callback: function (ds, fld, row, values) {
                var sum = 0;
                for (var i = 2; i <= 12; i++) sum += values[i];
                return sum;
            }
            // or 
            //expression: "y2000 + y2001 + y2002 + y2003 + y2004 + y2005 + y2006 + y2007 + y2008 + y2009 + y2010"
        }
    ]);
Code -2
    grid.setColumns([
        ...,
        {
            type: "calced",
            cached: true,
            valueCallback: function (column, row) {
                var sum = 0;
                for (var i = 2; i <= 12; i++) {
                    var v = row.getValue(i);
                    sum += isNaN(v) ? 0 : v;
                }
                return sum;
            },
            // or valueExpression
            //valueExpression: "y2000 + y2001 + y2002 + y2003 + y2004 + y2005 + y2006 + y2007 + y2008 + y2009 + y2010",
            footer: { 
                expression: "sum",
                styles: { numberFormat: "#,##0", fontBold: true }
            }
        }
    ]);

You can directly use the field name as variable identifier in DerivedField.expression or CalculatedColumn.valueExpression

View Source JSP 

See Also
DerivedField
CalculatedColumn