This example shows support for formulas in the grid columns by implementing column.formula callback.

Array data is used here, though JSON data can also be used.

In this example,
 
Company
Balance Sheet
Revenues
Expenditure
Profits = Total - Expenditure
Domestic
Exports
Total = Domestic + Exports
 
 
 
 
 
 
 
1Exxon Mobil$339,938.00$36,130.00$376,068.00$23,333.00$352,735.00
2Wal-Mart Stores$315,654.00$11,231.00$326,885.00$24,342.00$302,543.00
3Royal Dutch Shell$306,731.00$25,311.00$332,042.00$56,231.20$275,810.80
4BP$267,600.00$22,341.00$289,941.00$71,923.40$218,017.60
5General Motors$192,604.00-$10,567.00$182,037.00$52,934.00$129,103.00
6Chevron$189,481.00$14,099.00$203,580.00$12,023.50$191,556.50
7DaimlerChrysler$186,106.30$3,536.30$189,642.60$42,734.00$146,908.60
8Toyota Motor$185,805.00$12,119.60$197,924.60$57,023.40$140,901.20
9Ford Motor$177,210.00$2,024.00$179,234.00$22,896.00$156,338.00
10ConocoPhillips$166,683.00$13,529.00$180,212.00$72,456.00$107,756.00
11General Electric$157,153.00$16,353.00$173,506.00$16,912.50$156,593.50
12Total$152,360.70$15,250.00$152,360.72$74,236.50$78,124.22
13ING Group$138,235.30$8,958.90$147,194.20$52,012.90$95,181.30
14Citigroup$131,045.00$24,589.00$155,634.00$90,342.00$65,292.00
15AXA$129,839.20$5,186.50$135,025.70$13,043.80$121,981.90
16Allianz$121,406.00$5,442.40$126,848.40$19,529.50$107,318.90
17Volkswagen$118,376.60$1,391.70$119,768.30$84,472.70$35,295.60
18Fortis$112,351.40$4,896.30$117,247.70$83,473.00$33,774.70
19Crédit Agricole$110,764.60$7,434.30$118,198.90$14,567.40$103,631.50
20American Intl. Group$108,905.00$10,477.00$119,382.00$10,533.00$108,849.00
Loading...

93
 
1
2
3
    $(function () {        
4
        var data = [[1, 'Exxon Mobil', 'Ex', 339938.0, 36130.0, 23333.0],
5
            [2, 'Wal-Mart Stores', 'WS', 315654.0, 11231.0, 24342.0],
6
            [3, 'Royal Dutch Shell', 'RDS', 306731.0, 25311.0, 56231.2],
7
            [4, 'BP', 'B', 267600.0, 22341.0, 71923.4],
8
            [5, 'General Motors', 'GM', 192604.0, -10567.0, 52934.0],
9
            [6, 'Chevron', 'C', 189481.0, 14099.0, 12023.5],
10
            [7, 'DaimlerChrysler', 'DC', 186106.3, 3536.3, 42734.0],
11
            [8, 'Toyota Motor', 'TM', 185805.0, 12119.6, 57023.4],
12
            [9, 'Ford Motor', 'FM', 177210.0, 2024.0, 22896.0],
13
            [10, 'ConocoPhillips', 'CP', 166683.0, 13529.0, 72456.0],
14
            [11, 'General Electric', 'GE', 157153.0, 16353.0, 16912.5],
15
            [12, 'Total', 'T', '152360.7', 15250.0, 74236.5],
16
            [13, 'ING Group', 'IG', 138235.3, 8958.9, 52012.9],
17
            [14, 'Citigroup', 'CG', 131045.0, 24589.0, 90342.0],
18
            [15, 'AXA', 'A', 129839.2, 5186.5, 13043.8],
19
            [16, 'Allianz', 'AZ', 121406.0, 5442.4, 19529.5],
20
            [17, 'Volkswagen', 'VW', 118376.6, 1391.7, 84472.7],
21
            [18, 'Fortis', 'F', 112351.4, 4896.3, 83473.0],
22
            [19, 'Crédit Agricole', 'CA', 110764.6, 7434.3, 14567.4],
23
            [20, 'American Intl. Group', 'AIG', 108905.0, 10477.0, 10533.0]];
24
25
26
        var obj = { 
27
            height: 430,
28
            width: 'flex',
29
            showTop: false,            
30
            flex: {one: true},      
31
            sortModel: {space: true},      
32
            selectionModel: {column: true},
33
            resizable: true,
34
            cellSave: function(evt, ui){
35
                this.refreshRow(ui);
36
            }
37
        };
38
        
39
        obj.colModel = [            
40
            { title: "Company", dataIndx: 1 },
41
            { title: "Balance Sheet", align: "center", colModel: [
42
                { title: "Revenues", dataType: "float", align: "center", colModel: [
43
                    {
44
                        title: "Domestic",
45
                        dataIndx: 3,
46
                        format: '$##,###.00',
47
                        dataType: "float"
48
                    },
49
                    {
50
                        title: "Exports",
51
                        dataIndx: 4,
52
                        format: '$##,###.00',
53
                        dataType: "float"
54
                    },
55
                    {
56
                        title: "Total = Domestic + Exports",
57
                        width: 120,
58
                        editable: false,
59
                        dataIndx: 6,
60
                        dataType: "float",
61
                        format: '$##,###.00',
62
                        formula: function (ui) {                           
63
                            var rd = ui.rowData;
64
                            return rd[3] + rd[4];
65
                        }
66
                    }
67
                ]},
68
                {
69
                    title: "Expenditure",
70
                    dataType: "float",
71
                    format: '$##,###.00',
72
                    dataIndx: 5
73
                },
74
                {
75
                    title: "Profits = Total - Expenditure",
76
                    width: 140,
77
                    dataType: "float",
78
                    editable: false,
79
                    dataIndx: 7,
80
                    format: '$##,###.00',
81
                    formula: function (ui) {                        
82
                        var rd = ui.rowData;
83
                        return rd[6] - rd[5];
84
                    }
85
                }
86
            ]}
87
        ];
88
89
        obj.dataModel = { data: data };
90
91
        var $grid = $("#grid_group").pqGrid(obj);
92
    });
93