-
Notifications
You must be signed in to change notification settings - Fork 427
/
Copy pathexample7-events.html
160 lines (145 loc) · 4.95 KB
/
example7-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
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
<!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 7: Events</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"/>
<style>
.cell-title {
font-weight: bold;
}
#contextMenu {
background: #f8f8f8;
border: 1px solid #babfc7;
padding: 8px;
display: inline-block;
min-width: 100px;
z-index: 99999;
}
#contextMenu li {
padding: 4px 4px 4px 4px;
list-style: none;
cursor: pointer;
}
#contextMenu li:hover {
background-color: #e2eef8;
}
.context-header {
font-weight: bold;
margin-bottom: 5px;
}
.rotate-270 {
transform: rotate(220deg);
}
</style>
</head>
<body>
<h2 class="title">Example 7 - Events & Context Menu</h2>
<table width="100%">
<tr>
<td valign="top" width="50%">
<div id="myGrid" class="slick-container" style="width:600px;height:500px;"></div>
</td>
<td valign="top">
<h2>
<a href="/examples/index.html" style="text-decoration: none; font-size: 22px">⌂</a>
Demonstrates:
</h2>
<ul>
<li>handling events from the grid:</li>
<li>Right-click the row to open the context menu</li>
<li>Click the priority cell to toggle values</li>
</ul>
<h2>View Source:</h2>
<ul>
<li><A href="https://github.com/6pac/SlickGrid/blob/master/examples/example7-events.html" target="_sourcewindow"> View the source for this example on Github</a></li>
</ul>
</td>
</tr>
</table>
<ul id="contextMenu" style="display:none;position:absolute">
<div class="context-header">Set priority:</div>
<li data="Low">- Low</li>
<li data="Medium">- Medium</li>
<li data="High">- High</li>
</ul>
<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.core.js"></script>
<script src="../dist/browser/slick.interactions.js"></script>
<script src="../dist/browser/slick.grid.js"></script>
<script src="../dist/browser/slick.editors.js"></script>
<script>
function priorityFormatter(row, cell, value, columnDef, dataContext) {
const count = value === 'Low' ? 1 : value === 'Medium' ? 2 : 3;
let icon = '';
for (let i = 0; i < count; i++) {
icon += '<span class="sgi sgi-star-outline"></span>';
}
return icon;
}
var grid;
var data = [];
var columns = [
{id: "title", name: "Title", field: "title", width: 200, cssClass: "cell-title", editor: Slick.Editors.Text},
{id: "priority", name: "Priority", field: "priority", width: 80, selectable: false, resizable: false},
{id: "priority", name: "Priority", field: "priority", width: 80, selectable: false, resizable: false, formatter: priorityFormatter }
];
var options = {
editable: true,
enableAddRow: false,
enableCellNavigation: true,
asyncEditorLoading: false,
rowHeight: 30
};
document.addEventListener("DOMContentLoaded", function() {
for (var i = 0; i < 100; i++) {
var d = (data[i] = {});
d["title"] = "Task " + i;
d["priority"] = "Medium";
}
grid = new Slick.Grid("#myGrid", data, columns, options);
grid.onContextMenu.subscribe(function (e) {
e.preventDefault();
var cell = grid.getCellFromEvent(e);
const contextMenuElm = document.querySelector('#contextMenu');
contextMenuElm.dataset.row = cell.row;
contextMenuElm.style.top = `${e.pageY}px`;
contextMenuElm.style.left = `${e.pageX}px`;
contextMenuElm.style.display = 'block';
document.body.addEventListener('click', () => {
contextMenuElm.style.display = 'none';
});
});
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();
}
});
});
document.querySelector("#contextMenu").addEventListener('click', (e)=> {
if (e.target.nodeName.toLowerCase() !== "li") {
return;
}
if (!grid.getEditorLock().commitCurrentEdit()) {
return;
}
const row = e.target.parentNode.dataset.row;
data[row].priority = e.target.getAttribute('data');
grid.updateRow(row);
});
</script>
</body>
</html>