-
Notifications
You must be signed in to change notification settings - Fork 426
/
Copy pathexample-dynamic-filtered-with-jquery-tabs.html
425 lines (367 loc) · 14.4 KB
/
example-dynamic-filtered-with-jquery-tabs.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="shortcut icon" type="image/ico" href="favicon.ico" />
<title>SlickGrid example: jQuery tabs with a dynamic filtered grid in each pane</title>
<link rel="stylesheet" href="../dist/styles/css/slick.grid.css" type="text/css"/>
<link rel="stylesheet" href="../dist/styles/css/slick.pager.css" type="text/css"/>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.13.2/themes/smoothness/jquery-ui.min.css" />
<link rel="stylesheet" href="examples.css" type="text/css"/>
<link rel="stylesheet" href="../dist/styles/css/slick.columnpicker.css" type="text/css"/>
<style>
.my-grid {
background: white;
outline: 0;
border: 1px solid gray;
}
.cell-title {
font-weight: bold;
}
.cell-effort-driven {
justify-content: center;
}
.cell-selection {
border-right-color: silver;
border-right-style: solid;
background: #f5f5f5;
color: gray;
text-align: right;
font-size: 10px;
}
.slick-row.selected .cell-selection {
background-color: transparent; /* show default selected row background */
}
</style>
</head>
<body>
<h2 class="title">Example - Dynamic Example with Filters inside jQuery tabs</h2>
<table width="100%">
<tr>
<td valign="top" width="50%">
<button id='add-tab'>Add tab</button>
<div id="tabs">
<ul>
<li><a href="#tabs-1">Grid 1</a></li>
</ul>
<div id="tabs-1">
<div><strong><i>This tab is the original</i></strong></div>
<br/>
<b>Search:</b>
<hr/>
<div style="padding:6px;">
<label style="width:200px;float:left">Show tasks with % at least: </label>
<div style="padding:2px;">
<div style="width:100px;display:inline-block;" id="pcSlider_1"></div>
</div>
<br/>
<label style="width:200px;float:left">And title including:</label>
<input type=text id="txtSearch_1" style="width:100px;">
<br/><br/>
<button id="btnSelectRows_1">Select first 10 rows</button>
<br/><br/>
<div id="grid_1" style="width:600px;height:300px;" class="my-grid"></div>
<div>
</div>
</div>
</td>
<td valign="top">
<h2>
<a href="/examples/index.html" style="text-decoration: none; font-size: 22px">⌂</a>
Demonstrates:
</h2>
<ul>
<li>jQuery tabs containing a grid in each pane, with filtering editing and sorting as per example4-model.</li>
</ul>
<h2>View Source:</h2>
<ul>
<li><A href="https://github.com/6pac/SlickGrid/blob/master/examples/example-dynamic-filtered-with-jquery-tabs.html" target="_sourcewindow"> View the source for this example on Github</a></li>
</ul>
</td>
</tr>
</table>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.0/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.13.2/jquery-ui.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/sortablejs/Sortable.min.js"></script>
<script src="../dist/browser/slick.core.js"></script>
<script src="../dist/browser/slick.interactions.js"></script>
<script src="../dist/browser/slick.grid.js"></script>
<script src="../dist/browser/slick.formatters.js"></script>
<script src="../dist/browser/slick.editors.js"></script>
<script src="../dist/browser/plugins/slick.rowselectionmodel.js"></script>
<script src="../dist/browser/slick.dataview.js"></script>
<script src="../dist/browser/controls/slick.pager.js"></script>
<script src="../dist/browser/controls/slick.columnpicker.js"></script>
<script>
var gridElementName = "grid_";
var pagerElementName = "gridPager_";
var sliderElementName = "pcSlider_";
var searchElementName = "txtSearch_";
var selectButtonElementName = "btnSelectRows_";
var gridArray;
var dataViewArray;
// -----------------------------------------------------------------------------------
// HANDY HINTS
//
// There are many ways to achieve dynamic grids. The approach used in this example is
// to declare arrays for all the grid housekeeping objects such as search text, sort
// direction etc, and for all the important objects such as grids and dataViews.
// In a single-grid page, these are usually variables, but for multiple grid pages,
// declaring an array allows us to keep a set of data for each grid indexed by the
// 'gridIndex' from 1 to n.
// Events are declared once within the function that creates the grid, with the grid,
// dataview and gridIndex local vars being used to force a closure and lock the event
// to that grid/dataView.
//
// Another approach would be to create single events used by all grids that can
// determine which grid they are associated with and respond accordingly.
// To facilitate this, all grid events pass the args.grid parameter which is the
// grid itself, and all dataView events pass the args.dataView parameter similarly.
//
// Where the grid is available and you want the dataView, use grid.getData().
// Where the dataView is available and you want the grid, use GetDataViewIndex()
// and reference the grid using gridArray[gridIndex].
//
// Getting the gridIndex from the grid or dataView also allows reference to the arrays
// of housekeeping objects discussed earlier, and makes it easy get a handle on a control
// in the grid's tab by constructing element ids eg. sliderElementName above.
// The two following functions are not used in this page, but have been tested and can be
// utilised as just described to get the gridIndex from the grid or dataView object.
function GetGridIndex(grid) {
for (var i=1; i<gridArray.length; i++) {
if (gridArray[i] === grid) { return i; }
}
return -1;
}
function GetDataViewIndex(dataView) {
for (var i=1; i<dataViewArray.length; i++) {
if (dataViewArray[i] === dataView) { return i; }
}
return -1;
}
// -----------------------------------------------------------------------------------
var sortcolArray = [];
var sortdirArray = [];
var percentCompleteThresholdArray = [];
var searchStringArray = [];
var h_runfiltersArray = [];
// generic handlers (can be used in all grids)
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};
}
}
function myFilter(item, args) {
if (item["percentComplete"] < args.percentCompleteThreshold) {
return false;
}
if (args.searchString != "" && item["title"].indexOf(args.searchString) == -1) {
return false;
}
return true;
}
function percentCompleteSort(a, b) {
return a["percentComplete"] - b["percentComplete"];
}
function CreateColumns() {
var columns = [
{id: "sel", name: "#", field: "num", behavior: "select", cssClass: "cell-selection", width: 40, cannotTriggerInsert: true, resizable: false, selectable: false },
{id: "title", name: "Title", field: "title", width: 120, minWidth: 120, cssClass: "cell-title", editor: Slick.Editors.Text, validator: requiredFieldValidator, sortable: true},
{id: "duration", name: "Duration", field: "duration", editor: Slick.Editors.Text, sortable: true},
{id: "%", defaultSortAsc: false, name: "% Complete", field: "percentComplete", width: 95, resizable: false, formatter: Slick.Formatters.PercentCompleteBar, editor: Slick.Editors.PercentComplete, sortable: true},
{id: "start", name: "Start", field: "start", minWidth: 60, editor: Slick.Editors.Date, sortable: true},
{id: "finish", name: "Finish", field: "finish", minWidth: 60, editor: Slick.Editors.Date, sortable: true},
{id: "effort-driven", name: "Effort Driven", width: 90, minWidth: 20, maxWidth: 90, cssClass: "cell-effort-driven", field: "effortDriven", formatter: Slick.Formatters.Checkmark, editor: Slick.Editors.Checkbox, cannotTriggerInsert: true, sortable: true}
];
return columns;
}
function CreateOptions() {
var options = {
editable: true,
enableAddRow: true,
enableCellNavigation: true,
asyncEditorLoading: true,
forceFitColumns: false,
editorLock: new Slick.EditorLock()
};
return options;
}
function CreateData(dataView, gridIndex) {
var data = [];
// prepare the data
for (var i = 0; i < 50000; i++) {
var d = (data[i] = {});
d["id"] = "id_" + i;
d["num"] = 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);
}
dataView.beginUpdate();
dataView.setItems(data);
dataView.setFilterArgs({
percentCompleteThreshold: percentCompleteThresholdArray[gridIndex],
searchString: searchStringArray[gridIndex]
});
dataView.setFilter(myFilter);
dataView.endUpdate();
}
function CreateGrid(gridIndex) {
if (!gridArray) { gridArray = []; dataViewArray=[]; }
// define element names
var gridId = gridElementName + gridIndex;
var pagerId = pagerElementName + gridIndex;
// create grid and associated components
var dataView = new Slick.Data.DataView({ inlineFilters: true });
var columns = CreateColumns();
var options = CreateOptions();
var grid = new Slick.Grid("#" + gridId, dataView, columns, options);
grid.setSelectionModel(new Slick.RowSelectionModel());
var pager = new Slick.Controls.Pager(dataView, grid, $("#" + pagerId));
var columnpicker = new Slick.Controls.ColumnPicker(columns, grid, options);
gridArray[gridIndex] = grid;
dataViewArray[gridIndex] = dataView;
// init arrays
sortcolArray[gridIndex] = "title";
sortdirArray[gridIndex] = 1;
percentCompleteThresholdArray[gridIndex] = 0;
searchStringArray[gridIndex] = "";
h_runfiltersArray[gridIndex] = null;
// wire up events. any reference to gridIndex creates a closure and locks
// the function to the current grid
function comparer(a, b) {
var x = a[sortcolArray[gridIndex]], y = b[sortcolArray[gridIndex]];
return (x == y ? 0 : (x > y ? 1 : -1));
}
grid.onCellChange.subscribe(function (e, args) {
dataView.updateItem(args.item.id, args.item);
});
grid.onAddNewRow.subscribe(function (e, args) {
var item = {"num": data.length, "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};
$.extend(item, args.item);
dataView.addItem(item);
});
grid.onKeyDown.subscribe(function (e) {
// select all rows on ctrl-a
if (e.which != 65 || !e.ctrlKey) {
return false;
}
var rows = [];
for (var i = 0; i < dataView.getLength(); i++) {
rows.push(i);
}
grid.setSelectedRows(rows);
e.preventDefault();
});
grid.onSort.subscribe(function (e, args) {
sortdirArray[gridIndex] = args.sortAsc ? 1 : -1;
sortcolArray[gridIndex] = args.sortCol.field;
// using native sort with comparer
// preferred method but can be very slow in IE with huge datasets
dataView.sort(comparer, args.sortAsc);
});
// wire up model events to drive the grid
dataView.onRowCountChanged.subscribe(function (e, args) {
grid.updateRowCount();
grid.render();
});
dataView.onRowsChanged.subscribe(function (e, args) {
grid.invalidateRows(args.rows);
grid.render();
});
dataView.onPagingInfoChanged.subscribe(function (e, pagingInfo) {
var isLastPage = pagingInfo.pageNum == pagingInfo.totalPages - 1;
var enableAddRow = isLastPage || pagingInfo.pageSize == 0;
var options = grid.getOptions();
if (options.enableAddRow != enableAddRow) {
grid.setOptions({enableAddRow: enableAddRow});
}
});
h_runfiltersArray[gridIndex] = null;
// wire up the slider to apply the filter to the model
$("#" + sliderElementName + gridIndex).slider({
"range": "min",
"slide": function (event, ui) {
grid.getEditorLock().cancelCurrentEdit();
if (percentCompleteThresholdArray[gridIndex] != ui.value) {
window.clearTimeout(h_runfiltersArray[gridIndex]);
h_runfiltersArray[gridIndex] = window.setTimeout(updateFilter, 10);
percentCompleteThresholdArray[gridIndex] = ui.value;
}
}
});
// wire up the search textbox to apply the filter to the model
$("#" + searchElementName + gridIndex).keyup(function (e) {
grid.getEditorLock().cancelCurrentEdit();
// clear on Esc
if (e.which == 27) {
this.value = "";
}
searchStringArray[gridIndex] = this.value;
updateFilter();
});
function updateFilter() {
dataView.setFilterArgs({
percentCompleteThreshold: percentCompleteThresholdArray[gridIndex],
searchString: searchStringArray[gridIndex]
});
dataView.refresh();
}
$("#" + selectButtonElementName + gridIndex).click(function () {
if (!grid.getEditorLock().commitCurrentEdit()) {
return;
}
var rows = [];
for (var i = 0; i < 10 && i < dataView.getLength(); i++) {
rows.push(i);
}
grid.setSelectedRows(rows);
});
// create data and refresh display
CreateData(dataView, gridIndex);
// if you don't want the items that are not visible (due to being filtered out
// or being on a different page) to stay selected, pass 'false' to the second arg
dataView.syncGridSelection(grid, true);
}
$(function () {
// take care of jqueryui tabs and adding tab
$( "div#tabs" ).tabs();
$("button#add-tab").click(function() {
var num_tabs = $("div#tabs ul li").length + 1;
$("div#tabs ul").append(
"<li><a href='#tab" + num_tabs + "'>Grid " + num_tabs + "</a></li>"
);
$("div#tabs").append(
"<div id='tab" + num_tabs + "'>"
+ ' <b>Search:</b>\
<hr/>\
<div style="padding:6px;">\
<label style="width:200px;float:left">Show tasks with % at least: </label>\
\
<div style="padding:2px;">\
<div style="width:100px;display:inline-block;" id="' + sliderElementName + num_tabs + '"></div>\
</div>\
<br/>\
<label style="width:200px;float:left">And title including:</label>\
<input type=text id="' + searchElementName + num_tabs + '" style="width:100px;">\
<br/><br/>\
<button id="' + selectButtonElementName + num_tabs + '">Select first 10 rows</button>\
<br/><br/>\
'
+ '<div id="' + gridElementName + num_tabs + '" style="width:600px;height:300px;" class="my-grid"></div>'
+ "</div>"
);
CreateGrid(num_tabs);
$("div#tabs").tabs("refresh");
});
CreateGrid(1);
})
</script>
</body>
</html>