-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathExample8.tsx
266 lines (241 loc) · 10.8 KB
/
Example8.tsx
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
import i18next, { type TFunction } from 'i18next';
import { type Column, Formatters, type GridOption, SlickgridReact, } from '../../slickgrid-react';
import React from 'react';
import { withTranslation } from 'react-i18next';
import type BaseSlickGridState from './state-slick-grid-base';
import './example8.scss'; // provide custom CSS/SASS styling
interface Props {
t: TFunction;
}
interface State extends BaseSlickGridState {
selectedLanguage: string,
visibleColumns: Column[]
}
class Example8 extends React.Component<Props, State> {
title = 'Example 8: Header Menu Plugin';
subTitle = `
This example demonstrates using the <b>SlickHeaderMenu</b> plugin to easily add menus to colum headers.<br/>
These menus can be specified directly in the column definition, and are very easy to configure and use.
(<a href="https://ghiscoding.gitbook.io/slickgrid-react/grid-functionalities/header-menu-header-buttons" target="_blank">Docs</a>)
<ul>
<li>Now enabled by default in the Global Grid Options, it will add the default commands of (hide column, sort asc/desc)</li>
<li>Hover over any column header to see an arrow showing up on the right</li>
<li>Try Sorting (multi-sort) the 2 columns "Duration" and "% Complete" (the other ones are disabled)</li>
<li>Try hiding any columns (you use the "Column Picker" plugin by doing a right+click on the header to show the column back)</li>
<li>Note: The "Header Button" & "Header Menu" Plugins cannot be used at the same time</li>
<li>You can change the menu icon via SASS variables as shown in this demo (check all SASS variables)</li>
<li>Use override callback functions to change the properties of show/hide, enable/disable the menu or certain item(s) from the list</li>
<ol>
<li>These callbacks are: "itemVisibilityOverride", "itemUsabilityOverride"</li>
<li>for example if we want to disable the "Help" command over the "Title" and "Completed" column</li>
<li>for example don't show Help on column "% Complete"</li>
</ol>
</ul>
`;
constructor(public readonly props: Props) {
const defaultLang = 'en';
super(props);
// define the grid options & columns and then create the grid itself
this.state = {
columnDefinitions: [],
dataset: [],
gridOptions: undefined,
selectedLanguage: defaultLang,
visibleColumns: []
};
i18next.changeLanguage(defaultLang);
}
componentDidMount() {
this.defineGrid();
document.title = this.title;
// populate the dataset once the grid is ready
this.getData();
}
getColumnDefinitions() {
const columnDefinitions: Column[] = [
{ id: 'title', name: 'Title', field: 'title', nameKey: 'TITLE' },
{ id: 'duration', name: 'Duration', field: 'duration', nameKey: 'DURATION', sortable: true },
{ id: 'percentComplete', name: '% Complete', field: 'percentComplete', nameKey: 'PERCENT_COMPLETE', sortable: true },
{ id: 'start', name: 'Start', field: 'start', nameKey: 'START' },
{ id: 'finish', name: 'Finish', field: 'finish', nameKey: 'FINISH' },
{ id: 'completed', name: 'Completed', field: 'completed', nameKey: 'COMPLETED', formatter: Formatters.checkmarkMaterial }
];
columnDefinitions.forEach((columnDef) => {
columnDef.header = {
menu: {
commandItems: [
// add Custom Header Menu Item Commands which will be appended to the existing internal custom items
// you cannot override an internal command but you can hide them and create your own
// also note that the internal custom commands are in the positionOrder range of 50-60,
// if you want yours at the bottom then start with 61, below 50 will make your command(s) show on top
{
iconCssClass: 'mdi mdi-help-circle',
// you can disable a command with certain logic
// HOWEVER note that if you use "itemUsabilityOverride" has precedence when it is defined
// disabled: (columnDef.id === 'completed'),
titleKey: 'HELP', // use "title" as plain string OR "titleKey" when using a translation key
command: 'help',
tooltip: 'Need assistance?',
cssClass: 'bold', // container css class
textCssClass: (columnDef.id === 'title' || columnDef.id === 'completed') ? '' : 'blue', // just the text css class
positionOrder: 99,
itemUsabilityOverride: (args) => {
// for example if we want to disable the "Help" command over the "Title" and "Completed" column
return !(args.column.id === 'title' || args.column.id === 'completed');
},
itemVisibilityOverride: (args) => {
// for example don't show Help on column "% Complete"
return (args.column.id !== 'percentComplete');
},
action: (_e: Event, args: any) => {
// you can use the "action" callback and/or subscribe to the "onCallback" event, they both have the same arguments
console.log('execute an action on Help', args);
}
},
// you can also add divider between commands (command is a required property but you can set it to empty string)
{ divider: true, command: '', positionOrder: 98 },
// you can use "divider" as a string too, but if you do then make sure it's the correct position in the list
// (since there's no positionOrder when using 'divider')
// 'divider',
{
// we can also have multiple nested sub-menus
command: 'custom-actions', title: 'Hello', positionOrder: 99,
commandItems: [
{ command: 'hello-world', title: 'Hello World' },
{ command: 'hello-slickgrid', title: 'Hello SlickGrid' },
{
command: 'sub-menu', title: `Let's play`, cssClass: 'green', subMenuTitle: 'choose your game', subMenuTitleCssClass: 'text-italic salmon',
commandItems: [
{ command: 'sport-badminton', title: 'Badminton' },
{ command: 'sport-tennis', title: 'Tennis' },
{ command: 'sport-racquetball', title: 'Racquetball' },
{ command: 'sport-squash', title: 'Squash' },
]
}
]
},
{
command: 'feedback', title: 'Feedback', positionOrder: 100,
commandItems: [
{ command: 'request-update', title: 'Request update from supplier', iconCssClass: 'mdi mdi-star', tooltip: 'this will automatically send an alert to the shipping team to contact the user for an update' },
'divider',
{
command: 'sub-menu', title: 'Contact Us', iconCssClass: 'mdi mdi-account', subMenuTitle: 'contact us...', subMenuTitleCssClass: 'italic',
commandItems: [
{ command: 'contact-email', title: 'Email us', iconCssClass: 'mdi mdi-pencil-outline' },
{ command: 'contact-chat', title: 'Chat with us', iconCssClass: 'mdi mdi-message-text-outline' },
{ command: 'contact-meeting', title: 'Book an appointment', iconCssClass: 'mdi mdi-coffee' },
]
}
]
}
]
}
};
});
return columnDefinitions;
}
getGridOptions(): GridOption {
return {
enableAutoResize: true,
enableHeaderMenu: true,
autoResize: {
container: '#demo-container',
rightPadding: 10
},
enableFiltering: false,
enableCellNavigation: true,
headerMenu: {
hideSortCommands: false,
hideColumnHideCommand: false,
subItemChevronClass: 'mdi mdi-chevron-down mdi-rotate-270',
// you can use the "onCommand" (in Grid Options) and/or the "action" callback (in Column Definition)
onCommand: (_e, args) => {
// e.preventDefault(); // preventing default event would keep the menu open after the execution
const command = args.item?.command;
if (command.includes('hello-')) {
alert(args?.item.title);
} else if (command.includes('sport-')) {
alert('Just do it, play ' + args?.item?.title);
} else if (command.includes('contact-')) {
alert('Command: ' + args?.item?.command);
} else if (args.command === 'help') {
alert('Please help!!!');
}
},
},
enableTranslate: true,
i18n: i18next
};
}
defineGrid() {
const gridOptions = this.getGridOptions();
const columnDefinitions = this.getColumnDefinitions();
this.setState((state: any) => {
return {
...state,
columnDefinitions,
gridOptions
};
});
}
getData() {
// Set up some test columns.
const mockDataset: any[] = [];
for (let i = 0; i < 1000; i++) {
mockDataset[i] = {
id: 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',
completed: (i % 5 === 0)
};
}
this.setState((state: any) => {
return {
...state,
dataset: mockDataset,
};
});
}
async switchLanguage() {
const nextLanguage = (this.state.selectedLanguage === 'en') ? 'fr' : 'en';
await i18next.changeLanguage(nextLanguage);
this.setState((state: any) => {
return {
...state,
selectedLanguage: nextLanguage
};
});
}
render() {
return !this.state.gridOptions ? '' : (
<div id="demo-container" className="container-fluid">
<h2>
{this.title}
<span className="float-end font18">
see
<a target="_blank"
href="https://github.com/ghiscoding/slickgrid-react/blob/master/src/examples/slickgrid/Example8.tsx">
<span className="mdi mdi-link-variant"></span> code
</a>
</span>
</h2>
<div className="subtitle" dangerouslySetInnerHTML={{ __html: this.subTitle }}></div>
<button className="btn btn-outline-secondary btn-sm btn-icon me-1" onClick={() => this.switchLanguage()}>
<i className="mdi mdi-translate me-1"></i>
Switch Language
</button>
<b>Locale:</b> <span style={{ fontStyle: 'italic' }} data-test="selected-locale">{this.state.selectedLanguage + '.json'}</span>
<SlickgridReact gridId="grid8"
columnDefinitions={this.state.columnDefinitions}
gridOptions={this.state.gridOptions}
dataset={this.state.dataset}
/>
</div>
);
}
}
export default withTranslation()(Example8);