/
example7.js
163 lines (150 loc) · 4.51 KB
/
example7.js
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
import { autoinject } from 'aurelia-framework';
import { Column, GridOption, Formatters } from 'aurelia-slickgrid';
let columnsWithHighlightingById = {};
// create a custom Formatter to highlight negative values in red
const highlightingFormatter = (row, cell, value, columnDef, dataContext) => {
if (columnsWithHighlightingById[columnDef.id] && value < 0) {
return `<div style="color:red; font-weight:bold;">${value}</div>`;
} else {
return value;
}
};
@autoinject()
export class Example7 {
title = 'Example 7: Header Button Plugin';
subTitle = `
This example demonstrates using the <b>Slick.Plugins.HeaderButtons</b> plugin to easily add buttons to colum headers.
These buttons can be specified directly in the column definition, and are very easy to configure and use.
(<a href="https://github.com/ghiscoding/aurelia-slickgrid/wiki/Header-Menu-&-Header-Buttons" target="_blank">Wiki docs</a>)
<ul>
<li>Resize the 1st column to see all icon/command</li>
<li>Mouse hover the 2nd column to see it's icon/command</li>
<li>For all the other columns, click on top-right red circle icon to enable highlight of negative numbers.</li>
</ul>
`;
columnDefinitions;
gridOptions;
dataset = [];
gridObj;
constructor() {
// define the grid options & columns and then create the grid itself
this.defineGrid();
columnsWithHighlightingById = {};
}
attached() {
// populate the dataset once the grid is ready
this.getData();
}
defineGrid() {
this.columnDefinitions = [];
this.gridOptions = {
enableAutoResize: true,
enableHeaderButton: true,
enableHeaderMenu: false,
autoResize: {
containerId: 'demo-container',
sidePadding: 15
},
enableFiltering: false,
enableCellNavigation: true,
headerButton: {
onCommand: (e, args) => {
const column = args.column;
const button = args.button;
const command = args.command;
if (command === 'toggle-highlight') {
if (button.cssClass === 'fa fa-circle red') {
delete columnsWithHighlightingById[column.id];
button.cssClass = 'fa fa-circle-o red faded';
button.tooltip = 'Highlight negative numbers.';
} else {
columnsWithHighlightingById[column.id] = true;
button.cssClass = 'fa fa-circle red';
button.tooltip = 'Remove highlight.';
}
this.gridObj.invalidate();
}
}
}
};
}
getData() {
// Set up some test columns.
for (let i = 0; i < 9; i++) {
this.columnDefinitions.push({
id: i,
name: 'Column' + (i + 1),
field: i + '',
width: 100, // have the 2 first columns wider
sortable: true,
formatter: highlightingFormatter,
header: {
buttons: [
{
cssClass: 'fa fa-circle-o red faded',
command: 'toggle-highlight',
tooltip: 'Highlight negative numbers.'
}
]
}
});
}
// Set multiple buttons on the first column to demonstrate overflow.
this.columnDefinitions[0].name = 'Resize me!';
this.columnDefinitions[0].header = {
buttons: [
{
cssClass: 'fa fa-tag',
handler: (e) => {
alert('Tag');
}
},
{
cssClass: 'fa fa-comment',
handler: (e) => {
alert('Comment');
}
},
{
cssClass: 'fa fa-info-circle',
handler: (e) => {
alert('Info');
}
},
{
cssClass: 'fa fa-question-circle',
handler: (e) => {
alert('Help');
}
}
]
};
// Set a button on the second column to demonstrate hover.
this.columnDefinitions[1].name = 'Hover me!';
this.columnDefinitions[1].header = {
buttons: [
{
cssClass: 'fa fa-question-circle',
showOnHover: true,
tooltip: 'This button only appears on hover.',
handler: (e) => {
alert('Help');
}
}
]
};
// mock a dataset
const mockDataset = [];
for (let i = 0; i < 100; i++) {
const d = (mockDataset[i] = {});
d['id'] = i;
for (let j = 0; j < this.columnDefinitions.length; j++) {
d[j] = Math.round(Math.random() * 10) - 5;
}
}
this.dataset = mockDataset;
}
gridObjChanged(grid) {
this.gridObj = grid;
}
}