-
Notifications
You must be signed in to change notification settings - Fork 9
/
example03-events.html
83 lines (69 loc) · 2.49 KB
/
example03-events.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
---
title: 'Events'
demonstrates:
- handling events from the grid
- right-click the row to open the context menu
- click the priority cell to toggle values
requires_scripts:
- dist/compat/slick.editors.js
---
<div id="myGrid"></div>
<div id="contextMenu" class="dropdown-menu" style="display:none;">
<b class="p-2">Set priority:</b>
<a class="dropdown-item ps-4" href="#" data-priority="Low">Low</a>
<a class="dropdown-item ps-4" href="#" data-priority="Medium">Medium</a>
<a class="dropdown-item ps-4" href="#" data-priority="High">High</a>
</div>
<script>
var data = [];
for (var i = 0; i < 100; i++) {
data.push({
title: "Task " + i,
priority: "Medium"
})
}
var columns = [
{ field: "title", width: 200, cssClass: "cell-title", editor: Slick.TextEditor },
{ field: "priority", width: 80, selectable: false, resizable: false }
];
var grid = new Slick.Grid("#myGrid", data, columns, {
editable: true,
enableAddRow: false,
enableCellNavigation: true,
asyncEditorLoading: false,
rowHeight: 30
});
const contextMenu = document.getElementById('contextMenu');
function hideContextMenu() {
contextMenu.style.display = 'none';
document.body.removeEventListener('click', hideContextMenu);
}
grid.onContextMenu.subscribe(function (e) {
e.preventDefault();
var cell = grid.getCellFromEvent(e);
contextMenu.dataset.row = cell.row;
contextMenu.style.top = e.pageY + 'px';
contextMenu.style.left = e.pageX + 'px';
contextMenu.style.display = 'block';
document.body.addEventListener('click', hideContextMenu);
});
grid.onClick.subscribe(function (e) {
var cell = grid.getCellFromEvent(e);
if (grid.getColumns()[cell.cell].id == "priority") {
if (!grid.getEditorLock().commitCurrentEdit()) {
return;
}
var states = { "Low": "Medium", "Medium": "High", "High": "Low" };
data[cell.row].priority = states[data[cell.row].priority];
grid.updateRow(cell.row);
e.stopPropagation();
}
});
contextMenu.addEventListener('click', function(e) {
if (!e.target.matches('[data-priority]') || !grid.getEditorLock().commitCurrentEdit())
return;
var row = parseInt(this.dataset.row);
data[row].priority = e.target.dataset.priority;
grid.updateRow(row);
});
</script>