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 |
1 | Exxon Mobil | $339,938.00 | $36,130.00 | $376,068.00 | $23,333.00 | $352,735.00 |
2 | Wal-Mart Stores | $315,654.00 | $11,231.00 | $326,885.00 | $24,342.00 | $302,543.00 |
3 | Royal Dutch Shell | $306,731.00 | $25,311.00 | $332,042.00 | $56,231.20 | $275,810.80 |
4 | BP | $267,600.00 | $22,341.00 | $289,941.00 | $71,923.40 | $218,017.60 |
5 | General Motors | $192,604.00 | -$10,567.00 | $182,037.00 | $52,934.00 | $129,103.00 |
6 | Chevron | $189,481.00 | $14,099.00 | $203,580.00 | $12,023.50 | $191,556.50 |
7 | DaimlerChrysler | $186,106.30 | $3,536.30 | $189,642.60 | $42,734.00 | $146,908.60 |
8 | Toyota Motor | $185,805.00 | $12,119.60 | $197,924.60 | $57,023.40 | $140,901.20 |
9 | Ford Motor | $177,210.00 | $2,024.00 | $179,234.00 | $22,896.00 | $156,338.00 |
10 | ConocoPhillips | $166,683.00 | $13,529.00 | $180,212.00 | $72,456.00 | $107,756.00 |
11 | General Electric | $157,153.00 | $16,353.00 | $173,506.00 | $16,912.50 | $156,593.50 |
12 | Total | $152,360.70 | $15,250.00 | $152,360.72 | $74,236.50 | $78,124.22 |
13 | ING Group | $138,235.30 | $8,958.90 | $147,194.20 | $52,012.90 | $95,181.30 |
14 | Citigroup | $131,045.00 | $24,589.00 | $155,634.00 | $90,342.00 | $65,292.00 |
15 | AXA | $129,839.20 | $5,186.50 | $135,025.70 | $13,043.80 | $121,981.90 |
16 | Allianz | $121,406.00 | $5,442.40 | $126,848.40 | $19,529.50 | $107,318.90 |
17 | Volkswagen | $118,376.60 | $1,391.70 | $119,768.30 | $84,472.70 | $35,295.60 |
18 | Fortis | $112,351.40 | $4,896.30 | $117,247.70 | $83,473.00 | $33,774.70 |
19 | Crédit Agricole | $110,764.60 | $7,434.30 | $118,198.90 | $14,567.40 | $103,631.50 |
20 | American Intl. Group | $108,905.00 | $10,477.00 | $119,382.00 | $10,533.00 | $108,849.00 |
x1
2<div id="grid_group" style="margin:auto;"></div>
3
931
2
3$(function () {
4var 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
26var obj = {
27height: 430,
28width: 'flex',
29showTop: false,
30flex: {one: true},
31sortModel: {space: true},
32selectionModel: {column: true},
33resizable: true,
34cellSave: function(evt, ui){
35this.refreshRow(ui);
36}
37};
38
39obj.colModel = [
40{ title: "Company", dataIndx: 1 },
41{ title: "Balance Sheet", align: "center", colModel: [
42{ title: "Revenues", dataType: "float", align: "center", colModel: [
43{
44title: "Domestic",
45dataIndx: 3,
46format: '$##,###.00',
47dataType: "float"
48},
49{
50title: "Exports",
51dataIndx: 4,
52format: '$##,###.00',
53dataType: "float"
54},
55{
56title: "Total = Domestic + Exports",
57width: 120,
58editable: false,
59dataIndx: 6,
60dataType: "float",
61format: '$##,###.00',
62formula: function (ui) {
63var rd = ui.rowData;
64return rd[3] + rd[4];
65}
66}
67]},
68{
69title: "Expenditure",
70dataType: "float",
71format: '$##,###.00',
72dataIndx: 5
73},
74{
75title: "Profits = Total - Expenditure",
76width: 140,
77dataType: "float",
78editable: false,
79dataIndx: 7,
80format: '$##,###.00',
81formula: function (ui) {
82var rd = ui.rowData;
83return rd[6] - rd[5];
84}
85}
86]}
87];
88
89obj.dataModel = { data: data };
90
91var $grid = $("#grid_group").pqGrid(obj);
92});
93