/
example-plugin-headermenu.html
231 lines (205 loc) · 7.56 KB
/
example-plugin-headermenu.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
<!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="../slick.grid.css" type="text/css"/>
<link rel="stylesheet" href="../css/smoothness/jquery-ui-1.11.3.custom.css" type="text/css"/>
<link rel="stylesheet" href="../plugins/slick.headermenu.css" type="text/css"/>
<link rel="stylesheet" href="../controls/slick.columnpicker.css" type="text/css"/>
<link rel="stylesheet" href="examples.css" type="text/css"/>
<style>
/**
* Style the drop-down menu here since the plugin stylesheet mostly contains structural CSS.
*/
.slick-header-menu {
border: 1px solid #718BB7;
background: #f0f0f0;
padding: 2px;
-moz-box-shadow: 2px 2px 2px silver;
-webkit-box-shadow: 2px 2px 2px silver;
min-width: 100px;
z-index: 20;
}
.slick-header-menuitem {
padding: 2px 4px;
border: 1px solid transparent;
border-radius: 3px;
}
.slick-header-menuitem:hover {
border-color: silver;
background: white;
}
.slick-header-menuitem-disabled {
border-color: transparent !important;
background: inherit !important;
}
.icon-help {
background-image: url(../images/help.png);
}
</style>
</head>
<body>
<div style="position:relative">
<div style="width:500px;">
<div id="myGrid" style="width:100%;height:500px;"></div>
</div>
<div class="options-panel">
<h2>Demonstrates:</h2>
<p>
This example demonstrates using the <b>Slick.Plugins.HeaderMenu</b> plugin to add drop-down menus to column
headers. (Hover over the headers.)
</p>
<hr>
<h3>Auto-align option</h3>
<p>
Auto-align (defaults to False), will calculate whether it has enough space to show the drop menu to the right.
If it calculates that the drop menu is to fall outside of the viewport, it will automatically align the drop menu to the left.
However please note that to simulate a left alignement, we actually need to know the width of the drop menu.
The width of the drop menu is easy to find, it is the <i><b>"max-width"</b></i> set in the <i><b>".slick-header-menu"</b></i> CSS class.
</p>
<hr>
<h3>Auto-Align Header Menu Drop</h3>
<button onclick="autoAlignMenu(true)">ON</button>
<button onclick="autoAlignMenu(false)">OFF</button>
<h2>View Source:</h2>
<ul>
<li><A href="https://github.com/6pac/SlickGrid/blob/master/examples/example-plugin-headermenu.html" target="_sourcewindow"> View the source for this example on Github</a></li>
</ul>
</div>
</div>
<script src="../lib/jquery-1.11.2.min.js"></script>
<script src="../lib/jquery.event.drag-2.3.0.js"></script>
<script src="../slick.core.js"></script>
<script src="../slick.grid.js"></script>
<script src="../plugins/slick.headermenu.js"></script>
<script src="../controls/slick.columnpicker.js"></script>
<script>
var data = [];
var grid;
var headerMenuPlugin;
var columns = [
{id: "title", name: "Title", field: "title", width: 90},
{id: "duration", name: "Duration", field: "duration", sortable: true, width: 90},
{id: "%", name: "% Complete", field: "percentComplete", sortable: true, width: 90},
{id: "start", name: "Start", field: "start"},
{id: "finish", name: "Finish", field: "finish"},
{id: "effort-driven", name: "Effort Driven", field: "effortDriven"}
];
for (var i = 0; i < columns.length; i++) {
columns[i].header = {
menu: {
items: [
{
iconImage: "../images/sort-asc.gif",
title: "Sort Ascending",
disabled: !columns[i].sortable,
command: "sort-asc"
},
{
iconImage: "../images/sort-desc.gif",
title: "Sort Descending",
disabled: !columns[i].sortable,
command: "sort-desc"
},
{
title: "Hide Column",
command: "hide",
tooltip: "Can't hide this column"
},
{
iconCssClass: "icon-help",
title: "Help",
command: "help"
}
]
}
};
}
// keep a copy of all column for the array of visible columns
var visibleColumns = columns;
var options = {
enableColumnReorder: false,
multiColumnSort: true
};
var executeSort = function(cols) {
data.sort(function (dataRow1, dataRow2) {
for (var i = 0, l = cols.length; i < l; i++) {
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();
};
var removeColumnByIndex = function(array, index) {
return array.filter(function (el, i) {
return index !== i;
});
};
function autoAlignMenu(auto) {
// for the autoAlign to work properly, you need to provide the width of the drop menu.
// this width is the one showing in the CSS max-width, by default 100px
headerMenuPlugin.setOptions({ autoAlign: auto, width: 100 });
}
$(function () {
data = [];
for (var i = 0; i < 500; i++) {
data[i] = {
title: "Task " + i,
duration: Math.round(Math.random() * 25) + " days",
percentComplete: Math.round(Math.random() * 100),
start: "01/01/2009",
finish: "01/05/2009",
effortDriven: (i % 5 == 0)
};
}
grid = new Slick.Grid("#myGrid", data, columns, options);
var columnpicker = new Slick.Controls.ColumnPicker(columns, grid, options);
headerMenuPlugin = new Slick.Plugins.HeaderMenu({});
grid.onSort.subscribe(function (e, args) {
executeSort(args.sortCols);
});
headerMenuPlugin.onCommand.subscribe(function(e, args) {
if(args.command === "hide") {
var columnIndex = grid.getColumnIndex(args.column.id);
visibleColumns = removeColumnByIndex(visibleColumns, columnIndex);
grid.setColumns(visibleColumns);
}else if(args.command === "sort-asc" || args.command === "sort-desc") {
// get previously sorted columns
// getSortColumns() only returns sortAsc & columnId, we want the entire column definition
var oldSortColumns = grid.getSortColumns();
var cols = $.map(oldSortColumns, function(col) {
// get the column definition but only keep column which are not equal to our current column
if(col.columnId !== args.column.id) {
return {sortCol: columns[grid.getColumnIndex(col.columnId)], sortAsc: col.sortAsc };
}
});
// add to the column array, the column sorted by the header menu
var isSortedAsc = (args.command === "sort-asc");
cols.push({ sortAsc: isSortedAsc, sortCol: args.column });
// update the grid sortColumns array which will at the same add the visual sort icon(s) on the UI
newSortColumns = $.map(cols, function(col) {
return {columnId: col.sortCol.id, sortAsc: col.sortAsc };
});
grid.setSortColumns(newSortColumns);
executeSort(cols);
}else {
alert("Command: " + args.command);
}
});
// subscribe to event when column visibility is changed via the menu
columnpicker.onColumnsChanged.subscribe(function(e, args) {
console.log('Columns changed via the menu');
});
grid.registerPlugin(headerMenuPlugin);
})
</script>
</body>
</html>