-
Notifications
You must be signed in to change notification settings - Fork 427
/
Copy pathexample16-row-detail.html
512 lines (451 loc) · 19 KB
/
example16-row-detail.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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
<!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" />
<link rel="stylesheet" href="../dist/styles/css/example-demo.css" type="text/css"/>
<link rel="stylesheet" href="../dist/styles/css/slick-alpine-theme.css" type="text/css"/>
<link rel="stylesheet" href="../dist/styles/css/slick.rowdetailview.css" type="text/css"/>
<style>
.detail {
padding: 5px
}
.preload {
font-size: 18px;
}
input.filter {
border: 1px solid #dadada;
border-radius: 2px;
}
input.filter::placeholder {
opacity: 0.4;
}
.dynamic-cell-detail {
box-shadow: inset 0 0 0 1px #b0c1d0;
background-color: #f8f8f8;
}
.dynamic-cell-detail > :first-child {
vertical-align: middle;
line-height: 13px;
padding: 10px;
margin-left: 20px;
}
.slick-headerrow-column {
background: #f1f1f1;
text-overflow: clip;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
.slick-headerrow-column input {
margin: 0;
padding: 0;
width: 100%;
height: 100%;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
/* Style the tab */
.tab {
overflow: hidden;
border: 1px solid #ccc;
background-color: #f1f1f1;
}
/* Style the buttons inside the tab */
.tab button {
background-color: inherit;
float: left;
border: none;
outline: none;
cursor: pointer;
padding: 14px 16px;
transition: 0.3s;
font-size: 17px;
}
/* Change background color of buttons on hover */
.tab button:hover {
background-color: #ddd;
}
/* Create an active/current tablink class */
.tab button.active {
background-color: #ccc;
}
/* Style the tab content */
.tabcontent {
display: none;
padding: 6px 12px;
border: 1px solid #ccc;
border-top: none;
}
.options-panel {
width: 375px;
}
</style>
</head>
<body>
<h2 class="title">Example 16 - Row Detail Plugin</h2>
<div style="position:relative">
<td valign="top" width="50%">
<div id="myGrid" class="slick-container" style="width:600px;height:500px;"></div>
</td>
<br/>
<div class="options-panel">
<h2>
<a href="/examples/index.html" style="text-decoration: none; font-size: 22px">⌂</a>
Demonstrates:
</h2>
<ul>
<li>RowDetailView Plugin</li>
<li>See more detail of an item by toggling a row detail panel.<li></li>
<li>User can override the logic to display the expandable icon with "expandableOverride". e.g. every 2nd rows is selectable.</li>
<li>"useRowClick: True" to toggle from any column.</li>
<li>"useRowClick: False" to toggle from the toggle icon only.</li>
<li>"panelRows: 4" DetailView is (RowHeight * 4).</li>
<li>"loadOnce: True" Try to shrink and reopen the same row.</li>
<li>"loadOnce: False DEFAULT" Will call load every time row is expanded.</li>
<li>"singleRowExpand: True" Multiple rows can be expanded at any time.</li>
<li>"singleRowExpand: False DEFAULT" Will only expand 1 row at a time.</li>
<li> </li>
<li>Example of using non templated view e.g. check any row multiple of 5.<li>
</ul>
<h2>Options:</h2>
<p>
<button onclick="detailView.setOptions({ loadOnce: true, useRowClick: detailView.useRowClick })">Load Once ON</button>
<button onclick="detailView.setOptions({ loadOnce: false, useRowClick: detailView.useRowClick})">Load Once OFF</button>
<button onclick="detailView.collapseAll()" data-test="collapse-all-btn">Collapse All</button>
</p>
<p>
<input type="number" id="panelRowInput"/> <button onclick="setPanelRows()">Set Panel Rows Option</button>
</p>
<p>
<input type="number" id="maxRowInput"/> <button onclick="setMaxRows()">Set Max Panel Rows Option</button>
</p>
<p>
Click anywhere on the row to expand:
<p>
<button onclick="detailView.setOptions({ loadOnce: detailView.loadOnce, useRowClick: true })">Row Click ON</button>
<button onclick="detailView.setOptions({ loadOnce: detailView.loadOnce, useRowClick: false })">Row Click OFF</button>
</p>
How many rows cab be opened at a time (defaults to multiple):
<p>
<button onclick="detailView.setOptions({ singleRowExpand: true })">Single Row Expand ON</button>
<button onclick="detailView.setOptions({ singleRowExpand: false })">Single Row Expand OFF (multiple)</button>
</p>
<h2>View Source:</h2>
<ul>
<li><A href="https://github.com/6pac/SlickGrid/blob/master/examples/example16-row-detail.html" target="_sourcewindow"> View the source for this example on Github</a></li>
</ul>
</p>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/sortablejs/Sortable.min.js"></script>
<script src="sortable-cdn-fallback.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/plugins/slick.rowdetailview.js"></script>
<script src="../dist/browser/plugins/slick.rowselectionmodel.js"></script>
<script src="../dist/browser/slick.dataview.js"></script>
<script>
var dataView;
var detailView;
var grid;
var data = [];
var columnFilters = {};
var sortcol = "title";
var sortdir = 1;
var fakeNames = ['John Doe', 'Jane Doe', 'Chuck Norris', 'Bumblebee', 'Jackie Chan', 'Elvis Presley', 'Bob Marley', 'Mohammed Ali', 'Bruce Lee', 'Rocky Balboa'];
var columns = [
{id: "sel", name: "#", field: "num", behavior: "select", cssClass: "cell-selection", width: 40, resizable: false, selectable: false },
{id: "title", name: "Title", field: "title", width: 85, minWidth: 85, cssClass: "cell-title", sortable: true},
{id: "duration", name: "Duration", field: "duration", sortable: true},
{id: "%", name: "% Complete", field: "percentComplete", width: 95, resizable: false, formatter: Slick.Formatters.PercentCompleteBar, sortable: true},
{id: "start", name: "Start", field: "start", minWidth: 60},
{id: "finish", name: "Finish", field: "finish", minWidth: 60},
{id: "effort-driven", name: "Effort Driven", width: 90, minWidth: 20, maxWidth: 90, cssClass: "cell-effort-driven", field: "effortDriven", formatter: Slick.Formatters.Checkmark}
];
var options = {
editable: false,
enableAddRow: false,
enableCellNavigation: true,
enableColumnReorder: true,
explicitInitialization: true,
headerRowHeight: 30,
showHeaderRow: true,
};
function DataItem(i) {
this.id = i;
this.num = i;
this.percentComplete = Math.round(Math.random() * 100);
this.effortDriven = (i % 5 == 0);
this.start = "01/01/2009";
this.finish = "01/05/2009";
this.title = "Task " + i;
this.duration = "5 days";
}
function setMaxRows(amount) {
var value = document.querySelector("#maxRowInput").value;
var maxRows = parseInt(value);
var options = detailView.getOptions();
if(!isNaN(maxRows)) {
options.maxRows = maxRows;
}
detailView.setOptions(options);
}
function setPanelRows(amount) {
var value = document.querySelector("#panelRowInput").value;
var panelRows = parseInt(value);
var options = detailView.getOptions();
if(!isNaN(panelRows)) {
options.panelRows = panelRows;
}
detailView.setOptions(options);
}
function hookAssigneeOnClick(itemId) {
const assigneeElm = document.querySelector("#who-is-assignee_" + itemId);
if (assigneeElm) {
assigneeElm.addEventListener("click", assigneHandler.bind(this, itemId));
}
}
function assigneHandler(itemId) {
alert("Assignee is " + document.querySelector("#assignee_" + itemId).value);
}
function destroyAssigneeOnClick(itemId) {
const handler = document.querySelector("#who-is-assignee_" + itemId)
if (handler) {
handler.removeEventListener("click", assigneHandler.bind(this, itemId));
}
}
function loadingTemplate() {
return '<div class="preload">Loading...</div>';
}
function loadView(itemDetail) {
return [
'<div>',
'<h4>' + itemDetail.title + '</h4>',
'<div class="detail">',
'<label>Assignee:</label> <input id="assignee_' + itemDetail.id + '" type="text" value="' + itemDetail.assignee + '"/>',
'<span style="float: right">Find out who is the Assignee <button id="who-is-assignee_' + itemDetail.id + '">Click Me</button></span></div>',
'<div class="detail"><label>Reporter:</label> <span>' + itemDetail.reporter + '</span></div>',
'<div class="detail"><label>Duration:</label> <span>' + itemDetail.duration + '</span></div>',
'<div class="detail"><label>% Complete:</label> <span>' + itemDetail.percentComplete + '</span></div>',
'<div class="detail"><label>Start:</label> <span>' + itemDetail.start + '</span></div>',
'<div class="detail"><label>Finish:</label> <span>' + itemDetail.finish + '</span></div>',
'<div class="detail"><label>Effort Driven:</label> <span>' + itemDetail.effortDriven + '</span></div>',
'<div class="detail"><label>Effort Driven:</label> <span>' + itemDetail.effortDriven + '</span></div>',
'</div>'
].join('');
}
function filter(item) {
for (var columnId in columnFilters) {
if (columnId !== undefined && columnFilters[columnId] !== "") {
var column = grid.getColumns()[grid.getColumnIndex(columnId)];
// IMPORTANT with Row Detail View plugin
// if the row is a padding row, we just get the value we're filtering on from it's parent
item = detailView.getFilterItem(item);
if (item[column.field] !== undefined) {
var filterResult = typeof item[column.field].indexOf === 'function'
? (item[column.field].indexOf(columnFilters[columnId]) === -1)
: (item[column.field] != columnFilters[columnId]);
if (filterResult) {
return false;
}
}
}
}
return true;
}
// fill the template with a delay to simulate a server call
function simulateServerCall(item) {
window.setTimeout(function() {
// let's add some property to our item for a better simulation
var itemDetail = item;
itemDetail.assignee = fakeNames[randomNumber(0, 9)];
itemDetail.reporter = fakeNames[randomNumber(0, 9)];
if(itemDetail.num % 5 == 0) {
notifyNonTemplatedView(itemDetail)
} else {
notifyTemplate(itemDetail)
}
}, 1000);
}
// notify the onAsyncResponse with the "args.item" (required property)
// the plugin will then use itemDetail to populate the detail panel with "postTemplate"
function notifyTemplate(itemDetail) {
detailView.onAsyncResponse.notify({
"item": itemDetail
}, undefined, this);
}
// notify the onAsyncResponse with the "args.item" (required property)
// the plugin will then use itemDetail to populate the detail panel with "postTemplate"
function notifyNonTemplatedView(itemDetail) {
var rowIdx = grid.getData().getIdxById(itemDetail.id);
var temp =
"<div class=\"tab\">"+
"<button class=\"tablinks\" onclick=\"openCity(event, 'London'," + rowIdx +")\">London</button>" +
"<button class=\"tablinks\" onclick=\"openCity(event, 'Paris'," + rowIdx +")\">Paris</button>" +
"<button class=\"tablinks\" onclick=\"openCity(event, 'Tokyo'," + rowIdx +")\">Tokyo</button>" +
"</div>" +
"<div id=\"London\" class=\"tabcontent\">" +
"<h3>London</h3>" +
"<p>London is the capital city of England.</p>" +
"<button onclick=\"detailView.resizeDetailView(data[" + rowIdx + "])\">Resize detail view</button>" +
"</div>" +
"<div id=\"Paris\" class=\"tabcontent\">" +
"<h3>Paris</h3>" +
"<p>Paris is the capital of France.</p> " +
"</div>" +
"<div id=\"Tokyo\" class=\"tabcontent\">" +
"<div>" +
"<h4>Direct view inserting without template</h4>" +
"<span>This is not using the template</span><br/>" +
"<button onclick=\"detailView.resizeDetailView(data[" + rowIdx + "])\">Resize detail view</button>" +
"<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>" +
"This is to make the loaded area longer" +
"<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>" +
"Done Yay" +
"</div>" +
"</div>";
detailView.onAsyncResponse.notify({
"item": itemDetail,
// detailView Ignores any template and use what's passed through instead
"detailView": temp,
}, undefined, this);
}
/** Callback to open a city from row detail "Task 5" */
function openCity(evt, cityName, rowIdx) {
var i, tabcontent, tablinks;
tabcontent = document.getElementsByClassName("tabcontent");
for (i = 0; i < tabcontent.length; i++) {
tabcontent[i].style.display = "none";
}
tablinks = document.getElementsByClassName("tablinks");
for (i = 0; i < tablinks.length; i++) {
tablinks[i].className = tablinks[i].className.replace(" active", "");
}
document.getElementById(cityName).style.display = "block";
evt.currentTarget.className += " active";
/* You can save the detail view here OR on the filter keyup
the reason we save the detail view is to keep the entire content of the view
before doing certain action like filtering/sorting/reorderingColumn/...
*/
// detailView.saveDetailView(data[rowIdx]);
}
function randomNumber(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min);
}
function comparer(a, b) {
var x = a[sortcol], y = b[sortcol];
return (x == y ? 0 : (x > y ? 1 : -1));
}
document.addEventListener("DOMContentLoaded", function() {
// prepare the data
for (var i = 0; i < 5000; i++) {
data[i] = new DataItem(i);
}
dataView = new Slick.Data.DataView();
// create the row detail plugin
detailView = new Slick.Plugins.RowDetailView({
cssClass: "detailView-toggle",
preTemplate: loadingTemplate,
postTemplate: loadView,
process: simulateServerCall,
useRowClick: true,
// how many grid rows do we want to use for the detail panel
// also note that the detail view adds an extra 1 row for padding purposes
// example, if you choosed 6 panelRows, the display will in fact use 5 rows
panelRows: 6,
// make only every 2nd row an expandable row,
// by using the override function to provide custom logic of which row is expandable
// you can override it here in the options or externally by calling the method on the plugin instance
expandableOverride: function(row, dataContext, grid) {
return (dataContext.id % 2 === 1);
}
});
// push the plugin as the first column
columns.unshift(detailView.getColumnDefinition());
grid = new Slick.Grid("#myGrid", dataView, columns, options);
// register the column
grid.setSelectionModel(new Slick.RowSelectionModel({selectActiveRow: false}));
grid.registerPlugin(detailView);
detailView.onBeforeRowDetailToggle.subscribe(function(e, args) {
// you coud cancel opening certain rows
// if (args.item.id === 1) {
// e.preventDefault();
// return false;
// }
console.log('before toggling row detail', args.item);
});
detailView.onAfterRowDetailToggle.subscribe(function(e, args) {
console.log('after toggling row detail', args.item);
if (args.item._collapsed) {
destroyAssigneeOnClick(args.item.id);
} else {
}
});
detailView.onAsyncEndUpdate.subscribe(function(e, args) {
console.log('finished updating the post async template', args);
hookAssigneeOnClick(args.item.id);
});
// the following subscribers can be useful to Save/Re-Render a View
// when it goes out of viewport or back to viewport range
detailView.onRowOutOfViewportRange.subscribe(function(e, args) {
destroyAssigneeOnClick(args.item.id);
});
detailView.onRowBackToViewportRange.subscribe(function(e, args) {
hookAssigneeOnClick(args.item.id);
});
grid.onSort.subscribe(function (e, args) {
sortdir = args.sortAsc ? 1 : -1;
sortcol = 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);
});
function filterInputHandler (e) {
const inputFilter = e.target;
const columnId = inputFilter.dataset.columnid;
if (columnId != null) {
/* we can save the detail view content before filtering (this will save on every keyup)
OR you can save in other area of your code (like in the "openCity()" function)
the reason we save the detail view is to keep the entire content of the view
before doing certain action like filtering/sorting/reorderingColumn/...
it's up to you to decide where and if you need to save the detail view
*/
var expandedRows = detailView.getExpandedRows();
expandedRows.forEach(function (row) {
detailView.saveDetailView(row);
});
// keep filter in global variable & filter dataset by calling a refresh
columnFilters[columnId] = (inputFilter.value || '').trim();
dataView.refresh();
}
}
const headerRowElm = grid.getHeaderRow();
headerRowElm.addEventListener('change', filterInputHandler);
headerRowElm.addEventListener('keyup', filterInputHandler);
grid.onHeaderRowCellRendered.subscribe(function(e, args) {
if (args.column.field !== 'sel') {
args.node.innerHTML = ''; // empty it
const inputElm = document.createElement('input');
inputElm.className = 'filter';
inputElm.placeholder = '🔎︎';
inputElm.dataset.columnid = args.column.id;
inputElm.value = columnFilters[args.column.id] || '';
args.node.appendChild(inputElm);
}
});
// initialize the model after all the events have been hooked up
grid.init();
dataView.beginUpdate();
dataView.setItems(data, 'num');
dataView.setFilter(filter);
dataView.endUpdate();
});
</script>
</body>
</html>