-
Notifications
You must be signed in to change notification settings - Fork 427
/
Copy pathexample-header-row.html
156 lines (137 loc) · 4.25 KB
/
example-header-row.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
<!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"/>
<style>
input.filter {
border: 1px solid #dadada;
border-radius: 2px;
}
input.filter::placeholder {
opacity: 0.4;
}
.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>
</head>
<body>
<h2 class="title">Example - Header Row (filters)</h2>
<div style="position:relative">
<div style="width:600px;">
<div id="myGrid" class="slick-container" style="width:100%;height:500px;"></div>
</div>
<div class="options-panel">
<h2>
<a href="/examples/index.html" style="text-decoration: none; font-size: 22px">⌂</a>
Demonstrates:
</h2>
<ul>
<li>Using a fixed header row to implement column-level filters</li>
<li>Type numbers in textboxes to filter grid data</li>
</ul>
<h2>View Source:</h2>
<ul>
<li><A href="https://github.com/6pac/SlickGrid/blob/master/examples/example-header-row.html" target="_sourcewindow"> View the source for this example on Github</a></li>
</ul>
</div>
</div>
<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.dataview.js"></script>
<script>
var dataView;
var grid;
var data = [];
var options = {
enableCellNavigation: true,
showHeaderRow: true,
headerRowHeight: 30,
explicitInitialization: true
};
var columns = [];
var columnFilters = {};
for (var i = 0; i < 10; i++) {
columns.push({
id: i,
name: String.fromCharCode("A".charCodeAt(0) + i),
field: i,
width: 60
});
}
function filter(item) {
for (var columnId in columnFilters) {
if (columnId !== undefined && columnFilters[columnId] !== "") {
var c = grid.getColumns()[grid.getColumnIndex(columnId)];
if (item[c.field] != columnFilters[columnId]) {
return false;
}
}
}
return true;
}
document.addEventListener("DOMContentLoaded", function() {
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);
}
}
dataView = new Slick.Data.DataView();
grid = new Slick.Grid("#myGrid", dataView, columns, options);
dataView.onRowCountChanged.subscribe(function (e, args) {
grid.updateRowCount();
grid.render();
});
dataView.onRowsChanged.subscribe(function (e, args) {
grid.invalidateRows(args.rows);
grid.render();
});
const headerRowElm = grid.getHeaderRow();
headerRowElm.addEventListener('change', filterKeyup);
headerRowElm.addEventListener('keyup', filterKeyup);
function filterKeyup(e) {
const inputFilter = e.target;
const columnId = inputFilter.dataset.columnid;
if (columnId != null) {
columnFilters[columnId] = (e.target.value || '').trim();
dataView.refresh();
}
}
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);
}
});
grid.init();
dataView.beginUpdate();
dataView.setItems(data);
dataView.setFilter(filter);
dataView.endUpdate();
});
</script>
</body>
</html>