This repository was archived by the owner on Jun 1, 2025. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathExample8.tsx
More file actions
209 lines (192 loc) · 8.88 KB
/
Example8.tsx
File metadata and controls
209 lines (192 loc) · 8.88 KB
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
import i18next from 'i18next';
import { type Column, Formatters, type GridOption, SlickgridReact, } from '../../slickgrid-react';
import React, { useState, useEffect } from 'react';
import { withTranslation } from 'react-i18next';
import './example8.scss'; // provide custom CSS/SASS styling
const Example8: React.FC = () => {
const defaultLang = 'en';
const [columnDefinitions, setColumnDefinitions] = useState<Column[]>([]);
const [dataset, setDataset] = useState<any[]>([]);
const [gridOptions, setGridOptions] = useState<GridOption | undefined>(undefined);
const [selectedLanguage, setSelectedLanguage] = useState<string>(defaultLang);
// const [visibleColumns, setVisibleColumns] = useState<Column[]>([]);
const [hideSubTitle, setHideSubTitle] = useState(false);
useEffect(() => {
i18next.changeLanguage(defaultLang);
defineGrid();
getData();
}, []);
function getColumnDefinitions(): Column[] {
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: [
{
iconCssClass: 'mdi mdi-help-circle',
titleKey: 'HELP',
command: 'help',
tooltip: 'Need assistance?',
cssClass: 'bold',
textCssClass: (columnDef.id === 'title' || columnDef.id === 'completed') ? '' : 'blue',
positionOrder: 99,
itemUsabilityOverride: (args) => {
return !(args.column.id === 'title' || args.column.id === 'completed');
},
itemVisibilityOverride: (args) => {
return (args.column.id !== 'percentComplete');
},
action: (_e: Event, args: any) => {
console.log('execute an action on Help', args);
}
},
{ divider: true, command: '', positionOrder: 98 },
{
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;
}
function 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',
onCommand: (_e, args) => {
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
};
}
function defineGrid() {
const gridOptions = getGridOptions();
const columnDefinitions = getColumnDefinitions();
setColumnDefinitions(columnDefinitions);
setGridOptions(gridOptions);
}
function getData() {
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)
};
}
setDataset(mockDataset);
}
async function switchLanguage() {
const nextLanguage = (selectedLanguage === 'en') ? 'fr' : 'en';
await i18next.changeLanguage(nextLanguage);
setSelectedLanguage(nextLanguage);
}
return !gridOptions ? null : (
<div id="demo-container" className="container-fluid">
<h2>
Example 8: Header Menu Plugin
<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>
<button className="ms-2 btn btn-outline-secondary btn-sm btn-icon" type="button" data-test="toggle-subtitle" onClick={() => setHideSubTitle(!hideSubTitle)}>
<span className="mdi mdi-information-outline" title="Toggle example sub-title details"></span>
</button>
</h2>
{hideSubTitle ? null : <div className="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>
</div>}
<button className="btn btn-outline-secondary btn-sm btn-icon me-1" onClick={() => switchLanguage()}>
<i className="mdi mdi-translate me-1"></i>
Switch Language
</button>
<b>Locale:</b> <span style={{ fontStyle: 'italic' }} data-test="selected-locale">{selectedLanguage + '.json'}</span>
<SlickgridReact gridId="grid8"
columnDefinitions={columnDefinitions}
gridOptions={gridOptions}
dataset={dataset}
/>
</div>
);
}
export default withTranslation()(Example8);