forked from lawrencepit/slick-grid
-
Notifications
You must be signed in to change notification settings - Fork 1
/
example4-model.html
250 lines (189 loc) · 7.57 KB
/
example4-model.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>SlickGrid example</title>
<link rel="stylesheet" href="slick.grid.css" type="text/css" media="screen" charset="utf-8" />
<link rel="stylesheet" href="themes/smoothness/ui.all.css" type="text/css" media="screen" charset="utf-8" />
<link rel="stylesheet" href="examples.css" type="text/css" media="screen" charset="utf-8" />
<style>
.cell-title {
font-weight: bold;
}
.cell-effort-driven {
text-align: center;
}
.sort-asc {
background: silver url('images/sort-asc.png') no-repeat right center !important;
}
.sort-desc {
background: silver url('images/sort-desc.png') no-repeat right center !important;
}
</style>
</head>
<body>
<script language="JavaScript" src="lib/firebugx.js"></script>
<script language="JavaScript" src="lib/jquery-1.3.2.min.js"></script>
<script language="JavaScript" src="lib/jquery-ui-1.7.custom.min.js"></script>
<script language="JavaScript" src="lib/jquery.getScrollbarWidth.js"></script>
<script language="JavaScript" src="lib/jquery.rule-1.0.1-min.js"></script>
<script language="JavaScript" src="slick.editors.js"></script>
<script language="JavaScript" src="slick.grid.js"></script>
<script language="JavaScript" src="slick.globaleditorlock.js"></script>
<script language="JavaScript" src="slick.model.js"></script>
<table width="100%">
<tr>
<td valign="top" width="50%">
<div style="border:1px solid gray;background:wheat;padding:6px;">
<label>Show tasks with % at least: </label>
<div style="padding:4px;">
<div style="width:100px;" id="pcSlider"></div>
</div>
<br/>
<label>And title including:</label>
<input type=text id="txtSearch">
<br/>
<button id="btnResort">Sort by %</button>
</div>
<br/>
<div id="myGrid" style="width:600px;height:500px;"></div>
</td>
<td valign="top">
<h2>Demonstrates:</h2>
<ul>
<li>a filtered Model (DataView) as a data source instead of a simple array</li>
<li>grid reacting to model events (onRowCountChanged, onRowsChanged)</li>
<li>
<b>FAST</b> DataView recalculation and <b>real-time</b> grid updating in response to data changes.<br/>
The grid holds 10'000 rows, yet you are able to sort, filter, scroll, navigate and edit as if it had 50 rows.
</li>
<li>adding new rows, bidirectional sorting</li>
<li>column options: setValueHandler, cannotTriggerInsert</li>
<li><font color=red>NOTE:</font> all filters are immediately applied to new/edited rows</li>
</ul>
</td>
</tr>
</table>
<script>
function requiredFieldValidator(value) {
if (value == null || value == undefined || !value.length)
return {valid:false, msg:"This is a required field"};
else
return {valid:true, msg:null};
}
var dataView;
var grid;
var data = [];
var columns = [
{id:"title", name:"Title", field:"title", width:120, cssClass:"cell-title", editor:TextCellEditor, validator:requiredFieldValidator, setValueHandler:updateItem},
{id:"duration", name:"Duration", field:"duration", editor:TextCellEditor, setValueHandler:updateItem},
{id:"%", name:"% Complete", field:"percentComplete", width:80, resizable:false, formatter:GraphicalPercentCompleteCellFormatter, editor:PercentCompleteCellEditor, setValueHandler:updateItem},
{id:"start", name:"Start", field:"start", minWidth:60, editor:DateCellEditor, setValueHandler:updateItem},
{id:"finish", name:"Finish", field:"finish", minWidth:60, editor:DateCellEditor, setValueHandler:updateItem},
{id:"effort-driven", name:"Effort Driven", width:80, minWidth:20, maxWidth:80, cssClass:"cell-effort-driven", field:"effortDriven", formatter:BoolCellFormatter, editor:YesNoCheckboxCellEditor, setValueHandler:updateItem, cannotTriggerInsert:true}
];
var options = {
editable: true,
enableAddRow: true,
enableCellNavigation: true,
asyncEditorLoading: false
};
var percentCompleteThreshold = 0;
var searchString = "";
function myFilter(item) {
if (item["percentComplete"] < percentCompleteThreshold)
return false;
if (searchString != "" && item["title"].indexOf(searchString) == -1)
return false;
return true;
}
function percentCompleteSort(a,b) {
return a["percentComplete"] - b["percentComplete"];
}
var sortcol = "title";
var sortdir = 1;
function comparer(a,b) {
var x = a[sortcol], y = b[sortcol];
return sortdir * (x == y ? 0 : (x > y ? 1 : -1));
}
function updateItem(value,columnDef,item) {
item[columnDef.field] = value;
dataView.updateItem(item.id,item);
}
function addItem(columnDef,value) {
var item = {"id": "new_" + (Math.round(Math.random()*10000)), "title":"New task", "duration":"1 day", "percentComplete":0, "start":"01/01/2009", "finish":"01/01/2009", "effortDriven":false};
item[columnDef.field] = value;
dataView.addItem(item);
}
$(function()
{
// prepare the data
for (var i=0; i<10000; i++) {
var d = (data[i] = {});
d["id"] = "id_" + i;
d["title"] = "Task " + i;
d["duration"] = "5 days";
d["percentComplete"] = Math.round(Math.random() * 100);
d["start"] = "01/01/2009";
d["finish"] = "01/05/2009";
d["effortDriven"] = (i % 5 == 0);
}
// initialize the model
dataView = new DataView();
dataView.beginUpdate();
dataView.setItems(data);
dataView.setFilter(myFilter);
dataView.endUpdate();
// initialize the grid
grid = new SlickGrid($("#myGrid"), dataView.rows, columns, options);
grid.onAddNewRow = addItem;
grid.onColumnHeaderClick = function(columnDef) {
if (GlobalEditorLock.isEditing() && !GlobalEditorLock.commitCurrentEdit())
return;
for (var i=0; i<columns.length; i++)
grid.setColumnHeaderCssClass(columns[i].id, "", "sort-asc sort-desc");
if (sortcol == columnDef.field)
sortdir *= -1;
else
sortdir = 1;
grid.setColumnHeaderCssClass(columnDef.id, sortdir == 1 ? "sort-asc" : "sort-desc", "");
sortcol = columnDef.field;
dataView.sort(comparer);
}
// wire up model events to drive the grid
dataView.onRowCountChanged.subscribe(function(args) {
grid.resizeCanvas();
});
dataView.onRowsChanged.subscribe(function(rows) {
grid.removeRows(rows);
grid.render();
});
// wire up the slider to apply the filter to the model
$("#pcSlider").slider({
"range": "min",
"slide": function(event,ui) {
if (GlobalEditorLock.isEditing())
GlobalEditorLock.cancelCurrentEdit();
percentCompleteThreshold = ui.value;
dataView.refresh();
}
});
// wire up the search textbox to apply the filter to the model
$("#txtSearch").keyup(function(e) {
if (GlobalEditorLock.isEditing())
GlobalEditorLock.cancelCurrentEdit();
// clear on Esc
if (e.which == 27)
this.value = "";
searchString = this.value;
dataView.refresh();
})
$("#btnResort").click(function() {
if (GlobalEditorLock.isEditing())
GlobalEditorLock.cancelCurrentEdit();
dataView.sort(percentCompleteSort);
});
})
</script>
</body>
</html>