-
Notifications
You must be signed in to change notification settings - Fork 423
/
example5-collapsing.html
280 lines (234 loc) · 8.1 KB
/
example5-collapsing.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
<!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 5: Collapsing</title>
<link rel="stylesheet" href="../dist/styles/css/slick-icons.css" type="text/css"/>
<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="https://cdn.jsdelivr.net/npm/flatpickr/dist/flatpickr.min.css">
<style>
.cell-title {
font-weight: bold;
}
.cell-effort-driven {
justify-content: center;
}
.cell-title {
display: inline-flex;
align-items: center;
}
.toggle {
cursor: pointer;
height: 1em;
width: 1em;
display: inline-block;
font-size: 15px;
}
</style>
</head>
<body>
<table width="100%">
<tr>
<td valign="top" width="650px;">
<div style="border:1px solid gray;background:wheat;padding:6px;border-radius:2px">
<label>Show tasks with % at least: </label>
<div style="padding:4px;">
<input style="width:100px;display:inline-block;" id="pcSlider" type="range" min="1" max="100" value="1">
</div>
<br/>
<label>And title including:</label>
<input type=text id="txtSearch">
</div>
<br/>
<div id="myGrid" class="slick-container" style="width:650px;height:500px;"></div>
</td>
<td valign="top">
<div class="options-panel" style="margin: 10px 0 0 50px">
<h2>
<a href="/examples/index.html" style="text-decoration: none; font-size: 22px">⌂</a>
Demonstrates:
</h2>
<ul>
<li>implementing expand/collapse as a filter for DataView</li>
</ul>
<h2>View Source:</h2>
<ul>
<li><A href="https://github.com/6pac/SlickGrid/blob/master/examples/example5-collapsing.html" target="_sourcewindow"> View the source for this example on Github</a></li>
</ul>
</div>
</td>
</tr>
</table>
<script src="https://cdn.jsdelivr.net/npm/flatpickr"></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/slick.dataview.js"></script>
<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 TaskNameFormatter = function (row, cell, value, columnDef, dataContext) {
if (value == null || value == undefined || dataContext === undefined) { return ""; }
value = value.replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">");
var spacer = "<span style='display:inline-block;height:1px;width:" + (15 * dataContext["indent"]) + "px'></span>";
var idx = dataView.getIdxById(dataContext.id);
if (data[idx + 1] && data[idx + 1].indent > data[idx].indent) {
if (dataContext._collapsed) {
return spacer + " <span class='toggle expand sgi sgi-plus-box-outline'></span> " + value;
} else {
return spacer + " <span class='toggle collapse sgi sgi-minus-box-outline'></span> " + value;
}
} else {
return spacer + " <span class='toggle'></span> " + value;
}
};
var dataView;
var grid;
var data = [];
var columns = [
{id: "title", name: "Title", field: "title", width: 170, cssClass: "cell-title", formatter: TaskNameFormatter, editor: Slick.Editors.Text, validator: requiredFieldValidator},
{id: "duration", name: "Duration", field: "duration", editor: Slick.Editors.Text},
{id: "%", name: "% Complete", field: "percentComplete", width: 95, resizable: false, formatter: Slick.Formatters.PercentCompleteBar, editor: Slick.Editors.PercentComplete},
{id: "start", name: "Start", field: "start", minWidth: 60, editor: Slick.Editors.Flatpickr},
{id: "finish", name: "Finish", field: "finish", minWidth: 60, editor: Slick.Editors.Flatpickr},
{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}
];
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;
}
if (item.parent != null) {
var parent = data[item.parent];
while (parent) {
if (parent._collapsed || (parent["percentComplete"] < percentCompleteThreshold) || (searchString != "" && parent["title"].indexOf(searchString) == -1)) {
return false;
}
parent = data[parent.parent];
}
}
return true;
}
function percentCompleteSort(a, b) {
return a["percentComplete"] - b["percentComplete"];
}
var indent = 0;
var parents = [];
// prepare the data
for (var i = 0; i < 1000; i++) {
var d = (data[i] = {});
var parent;
if (Math.random() > 0.8 && i > 0) {
indent++;
parents.push(i - 1);
} else if (Math.random() < 0.3 && indent > 0) {
indent--;
parents.pop();
}
if (parents.length > 0) {
parent = parents[parents.length - 1];
} else {
parent = null;
}
d["id"] = "id_" + i;
d["indent"] = indent;
d["parent"] = parent;
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 Slick.Data.DataView({ inlineFilters: true });
dataView.beginUpdate();
dataView.setItems(data);
dataView.setFilter(myFilter);
dataView.endUpdate();
// initialize the grid
grid = new Slick.Grid("#myGrid", dataView, columns, options);
grid.onCellChange.subscribe(function (e, args) {
dataView.updateItem(args.item.id, args.item);
});
grid.onAddNewRow.subscribe(function (e, args) {
var item = {
"id": "new_" + (Math.round(Math.random() * 10000)),
"indent": 0,
"title": "New task",
"duration": "1 day",
"percentComplete": 0,
"start": "01/01/2009",
"finish": "01/01/2009",
"effortDriven": false};
Slick.Utils.extend(item, args.item);
dataView.addItem(item);
});
grid.onClick.subscribe(function (e, args) {
if (e.getNativeEvent().target.classList.contains("toggle")) {
var item = dataView.getItem(args.row);
if (item) {
if (!item._collapsed) {
item._collapsed = true;
} else {
item._collapsed = false;
}
dataView.updateItem(item.id, item);
}
e.stopImmediatePropagation();
}
});
// 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();
});
var h_runfilters = null;
// wire up the slider to apply the filter to the model
var slider = document.getElementById("pcSlider");
var sliderCallback = function() {
Slick.GlobalEditorLock.cancelCurrentEdit();
if (percentCompleteThreshold != this.value) {
window.clearTimeout(h_runfilters);
h_runfilters = window.setTimeout(() => dataView.refresh(), 10);
percentCompleteThreshold = this.value;
}
}
slider.oninput = sliderCallback;
// wire up the search textbox to apply the filter to the model
document.getElementById("txtSearch").addEventListener("keyup", function (e) {
Slick.GlobalEditorLock.cancelCurrentEdit();
// clear on Esc
if (e.which == 27) {
this.value = "";
}
searchString = this.value;
dataView.refresh();
});
</script>
</body>
</html>