|
| 1 | +<!DOCTYPE HTML> |
| 2 | +<html> |
| 3 | +<head> |
| 4 | + <link rel="stylesheet" href="../css/smoothness/jquery-ui-1.8.16.custom.css" type="text/css"/> |
| 5 | + <link rel="stylesheet" href="../slick.grid.css" type="text/css"/> |
| 6 | + <link rel="stylesheet" href="examples.css" type="text/css"/> |
| 7 | + <style> |
| 8 | + .totals { |
| 9 | + font-weight: bold; |
| 10 | + color: gray; |
| 11 | + font-style: italic; |
| 12 | + } |
| 13 | + </style> |
| 14 | +</head> |
| 15 | +<body> |
| 16 | +<div style="position:relative"> |
| 17 | + <div style="width:600px;"> |
| 18 | + <div id="myGrid" style="width:100%;height:500px;"></div> |
| 19 | + </div> |
| 20 | + |
| 21 | + <div class="options-panel"> |
| 22 | + <h2>Demonstrates:</h2> |
| 23 | + <ul> |
| 24 | + <li>Implementing a data provider to create a totals row at the end of the grid.</li> |
| 25 | + <li>This is a simplification of what the DataView does. </li> |
| 26 | + </ul> |
| 27 | + </div> |
| 28 | +</div> |
| 29 | + |
| 30 | +<script src="../lib/firebugx.js"></script> |
| 31 | + |
| 32 | +<script src="../lib/jquery-1.7.min.js"></script> |
| 33 | +<script src="../lib/jquery-ui-1.8.16.custom.min.js"></script> |
| 34 | +<script src="../lib/jquery.event.drag-2.2.js"></script> |
| 35 | + |
| 36 | +<script src="../slick.core.js"></script> |
| 37 | +<script src="../slick.editors.js"></script> |
| 38 | +<script src="../slick.grid.js"></script> |
| 39 | + |
| 40 | +<script> |
| 41 | + var grid; |
| 42 | + var data = []; |
| 43 | + var options = { |
| 44 | + enableCellNavigation: true, |
| 45 | + headerRowHeight: 30, |
| 46 | + editable: true |
| 47 | + }; |
| 48 | + |
| 49 | + var columns = []; |
| 50 | + for (var i = 0; i < 10; i++) { |
| 51 | + columns.push({ |
| 52 | + id: i, |
| 53 | + name: String.fromCharCode("A".charCodeAt(0) + i), |
| 54 | + field: i, |
| 55 | + width: 60, |
| 56 | + editor: Slick.Editors.Integer |
| 57 | + }); |
| 58 | + } |
| 59 | + |
| 60 | + |
| 61 | + |
| 62 | + function TotalsDataProvider(data, columns) { |
| 63 | + var totals = {}; |
| 64 | + var totalsMetadata = { |
| 65 | + // Style the totals row differently. |
| 66 | + cssClasses: "totals", |
| 67 | + columns: {} |
| 68 | + }; |
| 69 | + |
| 70 | + // Make the totals not editable. |
| 71 | + for (var i = 0; i < columns.length; i++) { |
| 72 | + totalsMetadata.columns[i] = { editor: null }; |
| 73 | + } |
| 74 | + |
| 75 | + |
| 76 | + this.getLength = function() { |
| 77 | + return data.length + 1; |
| 78 | + }; |
| 79 | + |
| 80 | + this.getItem = function(index) { |
| 81 | + return (index < data.length) ? data[index] : totals; |
| 82 | + }; |
| 83 | + |
| 84 | + this.updateTotals = function() { |
| 85 | + var columnIdx = columns.length; |
| 86 | + while (columnIdx--) { |
| 87 | + var columnId = columns[columnIdx].id; |
| 88 | + var total = 0; |
| 89 | + var i = data.length; |
| 90 | + while (i--) { |
| 91 | + total += (parseInt(data[i][columnId], 10) || 0); |
| 92 | + } |
| 93 | + totals[columnId] = "Sum: " + total; |
| 94 | + } |
| 95 | + }; |
| 96 | + |
| 97 | + this.getItemMetadata = function(index) { |
| 98 | + return (index != data.length) ? null : totalsMetadata; |
| 99 | + }; |
| 100 | + |
| 101 | + this.updateTotals(); |
| 102 | + } |
| 103 | + |
| 104 | + |
| 105 | + |
| 106 | + $(function () { |
| 107 | + for (var i = 0; i < 10; i++) { |
| 108 | + var d = (data[i] = {}); |
| 109 | + d["id"] = i; |
| 110 | + for (var j = 0; j < columns.length; j++) { |
| 111 | + d[j] = Math.round(Math.random() * 10); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + var dataProvider = new TotalsDataProvider(data, columns); |
| 116 | + |
| 117 | + grid = new Slick.Grid("#myGrid", dataProvider, columns, options); |
| 118 | + |
| 119 | + grid.onCellChange.subscribe(function(e, args) { |
| 120 | + // The data has changed - recalculate the totals. |
| 121 | + dataProvider.updateTotals(); |
| 122 | + |
| 123 | + // Rerender the totals row (last row). |
| 124 | + grid.invalidateRow(dataProvider.getLength() - 1); |
| 125 | + grid.render(); |
| 126 | + }); |
| 127 | + }) |
| 128 | +</script> |
| 129 | +</body> |
| 130 | +</html> |
0 commit comments