-
Notifications
You must be signed in to change notification settings - Fork 429
Expand file tree
/
Copy pathexample-0070-plugin-state.html
More file actions
149 lines (130 loc) · 4.49 KB
/
Copy pathexample-0070-plugin-state.html
File metadata and controls
149 lines (130 loc) · 4.49 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<link rel="shortcut icon" type="image/ico" href="favicon.ico" />
<title>SlickGrid example: Plugin: State</title>
<link rel="stylesheet" href="../dist/styles/css/slick.columnpicker.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-icons.css" type="text/css"/>
<link rel="stylesheet" href="../dist/styles/css/slick-alpine-theme.css" type="text/css"/>
</head>
<body>
<h2 class="title">Example - Grid State Plugin</h2>
<div style="position:relative">
<div id="gridContainer">
<div id="myGrid" style="width:600px;height:500px;"></div>
</div>
<div class="options-panel">
<h2>
<a href="/examples/index.html" style="text-decoration: none; font-size: 22px">⌂</a>
About:
</h2>
<p>
This example demonstrates using the <b>Slick.State</b> plugin persisting grid column width, order, visibility and sort order using Local Storage.
For this demo, we use Local Storage, but you could technically save this data into a database and reload at any time your previous session.
</p>
<h2>Options:</h2>
<button data-test="clear-storage" onclick="clearLocalStorage()">Clear Local Storage & Page Reload</button>
</div>
</div>
<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.dataview.js"></script>
<script src="../dist/browser/plugins/slick.state.js"></script>
<script src="../dist/browser/controls/slick.columnpicker.js"></script>
<script id="script_tag_example" type="text/javascript">
var grid;
var data = [];
var options = {
enableColumnReorder: true,
multiColumnSort: true,
enableCellNavigation: true
};
var columns = [];
// Set up some test columns.
for (var i = 0; i < 10; i++) {
columns.push({
id: i,
name: String.fromCharCode("A".charCodeAt(0) + i),
field: i,
width: 90,
sortable: true
});
}
// Set up some test data.
for (var i = 0; i < 100; i++) {
var d = (data[i] = {});
d.id = i;
for (var j = 0; j < columns.length; j++) {
d[j] = Math.round(Math.random() * 10) - 5;
}
}
function clearLocalStorage() {
localStorage.clear();
window.location.reload();
}
document.addEventListener("DOMContentLoaded", function() {
var dataView = new Slick.Data.DataView();
var statePersistor = new Slick.State({
cid: 'sample-grid',
defaultColumns: columns
});
statePersistor.onStateChanged.subscribe(function (e, args) {
console.log("onStateChanged", args);
});
grid = new Slick.Grid("#myGrid", dataView, columns, options);
var columnpicker = new Slick.Controls.ColumnPicker(columns, grid, options);
columnpicker.onColumnsChanged.subscribe(function () {
statePersistor.save();
});
grid.registerPlugin(statePersistor);
dataView.onRowCountChanged.subscribe(function (e, args) {
grid.updateRowCount();
grid.render();
});
dataView.onRowsChanged.subscribe(function (e, args) {
grid.invalidateRows(args.rows);
grid.render();
});
function sortDataView(cols) {
dataView.sort(function (dataRow1, dataRow2) {
for (var i = 0, l = cols.length; i < l; i++) {
if (!cols[i].sortCol) continue;
var field = cols[i].sortCol.field;
var sign = cols[i].sortAsc ? 1 : -1;
var value1 = dataRow1[field], value2 = dataRow2[field];
var result = (value1 === value2 ? 0 : (value1 > value2 ? 1 : -1)) * sign;
if (result !== 0) {
return result;
}
}
return 0;
});
grid.invalidate();
grid.render();
}
grid.onSort.subscribe(function (e, args) {
sortDataView(args.sortCols);
});
grid.init();
dataView.beginUpdate();
dataView.setItems(data);
dataView.endUpdate();
statePersistor.restore()
.then(function (state) {
grid.setSortColumns(state.sortcols || []);
var columns = grid.getColumns();
var sortCols = (grid.getSortColumns() || []).map(function (col) {
return {
sortCol: columns[grid.getColumnIndex(col.columnId)],
sortAsc: col.sortAsc
};
});
sortDataView(sortCols);
});
});
</script>
</body>
</html>