Repository navigation
Expand file tree
/
Copy pathexamples-basic.html
More file actions
398 lines (367 loc) · 20.7 KB
/
Copy pathexamples-basic.html
File metadata and controls
398 lines (367 loc) · 20.7 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
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
<meta name="kmwc:title" content="📦 Basic Examples">
<meta name="kmwc:subtitle" content="Core grid features with all editor types">
<div class="card">
<h2>BU01 · Employee Grid</h2>
<p>A comprehensive example showing text, number, date, select, combobox, and autocomplete editors with keyboard navigation.
Hover a <strong>Name</strong> or <strong>Salary</strong> cell: the <code>getTooltipCallback</code> tooltip is built from the cell context when it opens —
select a row (click its #) or edit a salary and hover again to see the chip follow the row state.</p>
<div class="controls">
<label>
<input type="checkbox" id="isStriped" checked> Striped
</label>
<label>
<input type="checkbox" id="isHoverable" checked> Hoverable
</label>
<label>
<input type="checkbox" id="sortable" checked> Sortable
</label>
<label>
<input type="checkbox" id="isPagingEnabled"> Pageable
</label>
<label>
<input type="checkbox" id="isEditable" checked> Editable
</label>
<label>
<input type="checkbox" id="navigate" checked> Navigate Mode
</label>
<label>
<input type="checkbox" id="isDirtyIndicatorShown" checked> Dirty Indicator
</label>
</div>
<web-grid id="grid" style="max-height: 400px;"></web-grid>
<p class="mt-1">
<strong>Current Sort:</strong>
<span id="sortState" class="muted">No sorting applied</span><br>
<small class="muted">Click column header to sort. Ctrl+Click to add columns (cascading sort).</small>
</p>
<p>
<strong>Focused Row:</strong>
<span id="focusedRowState" class="muted">None</span>
</p>
</div>
<div class="card">
<h2>BU02 · Keyboard Navigation</h2>
<p>Navigate mode enables Excel-like keyboard navigation. Use the controls above to toggle it on/off.</p>
<div class="grid-2">
<div>
<h3>Cell Navigation</h3>
<ul>
<li><code>Arrow Up/Down</code> - Move between rows</li>
<li><code>Arrow Left/Right</code> - Move between columns</li>
<li><code>Tab / Shift+Tab</code> - Next/previous editable cell</li>
<li><code>Home / End</code> - First/last cell in row</li>
<li><code>PageUp / PageDown</code> - Move up/down by ~10 rows</li>
</ul>
</div>
<div>
<h3>Jump Navigation</h3>
<ul>
<li><code>Ctrl+Home</code> - First cell in grid</li>
<li><code>Ctrl+End</code> - Last cell in grid</li>
<li><code>Ctrl+PageUp</code> - First cell in column</li>
<li><code>Ctrl+PageDown</code> - Last cell in column</li>
</ul>
</div>
</div>
<div class="note mt-1">
<div class="note-title">Editing Keys</div>
<ul>
<li><code>Enter / F2</code> - Start editing the focused cell</li>
<li><code>Space</code> - Toggle checkbox or open dropdown/datepicker</li>
<li><code>Escape</code> - Cancel edit and clear focus</li>
<li><code>Type any character</code> - Start editing with that character</li>
</ul>
</div>
</div>
<div class="card">
<h2>BU03 · Disabled / Readonly States</h2>
<div class="grid">
<div class="form-group">
<label>Disabled Grid</label>
<web-grid id="grid-disabled" disabled></web-grid>
<small>No interaction, not focusable</small>
</div>
<div class="form-group">
<label>Readonly Grid</label>
<web-grid id="grid-readonly" readonly></web-grid>
<small>The value can't change; still focusable and copyable</small>
</div>
</div>
</div>
<script type="module">
// Sample employee data
const sampleData = [
{ id: 1847, name: 'Alice Johnson', email: 'alice@example.com', department: 'Engineering', salary: 85000, startDate: '2020-03-15', status: 'Active', statusTooltip: 'Employee is currently working', manager: 'John Doe', location: 'New York', phone: '555-0101' },
{ id: 2391, name: 'Bob Smith', email: 'bob@example.com', department: 'Marketing', salary: 72000, startDate: '2019-07-22', status: 'Active', statusTooltip: 'Employee is currently working', manager: 'Jane Smith', location: 'Los Angeles', phone: '555-0102' },
{ id: 1052, name: 'Carol Williams', email: 'carol@example.com', department: 'Engineering', salary: 92000, startDate: '2018-11-01', status: 'Active', statusTooltip: 'Employee is currently working', manager: 'John Doe', location: 'New York', phone: '555-0103' },
{ id: 3764, name: 'David Brown', email: 'david@example.com', department: 'Sales', salary: 68000, startDate: '2021-01-10', status: 'Active', statusTooltip: 'Employee is currently working', manager: 'Mike Johnson', location: 'Chicago', phone: '555-0104' },
{ id: 2908, name: 'Eva Martinez', email: 'eva@example.com', department: 'HR', salary: 65000, startDate: '2020-06-20', status: 'Active', statusTooltip: 'Employee is currently working', manager: 'Sarah Lee', location: 'Miami', phone: '555-0105' },
{ id: 1523, name: 'Frank Lee', email: 'frank@example.com', department: 'Engineering', salary: 88000, startDate: '2019-02-14', status: 'Active', statusTooltip: 'Employee is currently working', manager: 'John Doe', location: 'New York', phone: '555-0106' },
{ id: 4281, name: 'Grace Kim', email: 'grace@example.com', department: 'Marketing', salary: 75000, startDate: '2020-09-05', status: 'On Leave', statusTooltip: 'On parental leave until March 2025', manager: 'Jane Smith', location: 'Los Angeles', phone: '555-0107' },
{ id: 3156, name: 'Henry Chen', email: 'henry@example.com', department: 'Sales', salary: 71000, startDate: '2021-04-18', status: 'Active', statusTooltip: 'Employee is currently working', manager: 'Mike Johnson', location: 'Chicago', phone: '555-0108' },
{ id: 1739, name: 'Ivy Wang', email: 'ivy@example.com', department: 'Engineering', salary: 95000, startDate: '2017-08-30', status: 'Active', statusTooltip: 'Employee is currently working', manager: 'John Doe', location: 'Seattle', phone: '555-0109' },
{ id: 4592, name: 'Jack Davis', email: 'jack@example.com', department: 'HR', salary: 62000, startDate: '2022-02-01', status: 'Active', statusTooltip: 'Employee is currently working', manager: 'Sarah Lee', location: 'Miami', phone: '555-0110' },
{ id: 2847, name: 'Karen Wilson', email: 'karen@example.com', department: 'Engineering', salary: 89000, startDate: '2019-05-12', status: 'Active', statusTooltip: 'Employee is currently working', manager: 'John Doe', location: 'New York', phone: '555-0111' },
{ id: 3620, name: 'Leo Garcia', email: 'leo@example.com', department: 'Marketing', salary: 73000, startDate: '2020-11-25', status: 'Active', statusTooltip: 'Employee is currently working', manager: 'Jane Smith', location: 'Los Angeles', phone: '555-0112' },
]
// Options for select editors
const departmentOptions = [
{ value: 'Engineering', label: 'Engineering', icon: '⚙️', subtitle: 'Product & Development' },
{ value: 'Marketing', label: 'Marketing', icon: '📢', subtitle: 'Brand & Communications' },
{ value: 'Sales', label: 'Sales', icon: '💼', subtitle: 'Revenue & Growth' },
{ value: 'HR', label: 'HR', icon: '👥', subtitle: 'People & Culture' },
{ value: 'Finance', label: 'Finance', icon: '💰', subtitle: 'Accounting & Budget' },
{ value: 'Legal', label: 'Legal', icon: '⚖️', subtitle: 'Compliance', disabled: true },
{ value: 'Operations', label: 'Operations', icon: '🔧', subtitle: 'Infrastructure & Support' },
{ value: 'Research', label: 'Research', icon: '🔬', subtitle: 'R&D & Innovation' },
{ value: 'Design', label: 'Design', icon: '🎨', subtitle: 'UX & Visual Design' },
{ value: 'Quality', label: 'Quality Assurance', icon: '✅', subtitle: 'Testing & QA' },
{ value: 'Security', label: 'Security', icon: '🔒', subtitle: 'InfoSec & Compliance' },
{ value: 'Data', label: 'Data Science', icon: '📊', subtitle: 'Analytics & ML' },
{ value: 'DevOps', label: 'DevOps', icon: '🚀', subtitle: 'CI/CD & Automation' },
{ value: 'Support', label: 'Customer Support', icon: '🎧', subtitle: 'Help & Service' },
{ value: 'Training', label: 'Training', icon: '📚', subtitle: 'Learning & Development' },
{ value: 'Procurement', label: 'Procurement', icon: '📦', subtitle: 'Purchasing & Vendors' },
{ value: 'Facilities', label: 'Facilities', icon: '🏢', subtitle: 'Office & Maintenance' },
{ value: 'Communications', label: 'Communications', icon: '📣', subtitle: 'Internal & External Comms' },
{ value: 'Strategy', label: 'Strategy', icon: '🎯', subtitle: 'Planning & Analysis' },
{ value: 'Partnerships', label: 'Partnerships', icon: '🤝', subtitle: 'Business Development' },
{ value: 'Compliance', label: 'Compliance', icon: '📋', subtitle: 'Regulatory Affairs' },
{ value: 'IT', label: 'IT Services', icon: '💻', subtitle: 'Tech Support & Systems' },
{ value: 'Product', label: 'Product Management', icon: '📱', subtitle: 'Roadmap & Features' },
{ value: 'Executive', label: 'Executive', icon: '👔', subtitle: 'C-Suite & Leadership' }
]
const statusOptions = [
{ value: 'Active', label: 'Active', icon: '✅' },
{ value: 'Inactive', label: 'Inactive', icon: '⏸️' },
{ value: 'On Leave', label: 'On Leave', icon: '🏖️' },
{ value: 'Terminated', label: 'Terminated', icon: '❌', disabled: true }
]
const locationOptions = [
{ value: 'New York', label: 'New York' },
{ value: 'Los Angeles', label: 'Los Angeles' },
{ value: 'Chicago', label: 'Chicago' },
{ value: 'Miami', label: 'Miami' },
{ value: 'Seattle', label: 'Seattle' },
{ value: 'Boston', label: 'Boston' }
]
// Escape row data for the HTML tooltip below
const esc = (text) => String(text ?? '').replace(/[&<>"']/g, (c) => ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' })[c])
// Column definitions
const columns = [
{ field: 'id', title: 'ID', width: '60px', horizontalAlign: 'center', isEditable: false },
{
field: 'name',
title: 'Name',
width: '150px',
headerInfo: 'Full name of the employee',
editor: 'text',
// Rich tooltip built from the cell ctx, resolved each time it opens: bold name,
// italic department / manager, and a chip from the row's live state (selected /
// unsaved / locked) — select a row (click its #) or edit it, then hover again.
// Row data is escaped (isTooltipHtml renders markup); no interactive content —
// tooltips are pointer-events: none and close when the pointer leaves the cell.
isTooltipHtml: true,
getTooltipCallback: ({ value: val, row, rowIndex, controller }) => {
const [chipText, chipColor] = controller.isRowSelected(rowIndex) ? ['✓ selected row', '#16a34a']
: controller.isRowDirty(rowIndex) ? ['✎ unsaved changes', '#d97706']
: controller.isRowLocked(row) ? [`🔒 locked · ${row.status}`, '#6b7280']
: ['click # to select', '#6366f1']
return `<div style="display: grid; gap: .25rem;">`
+ `<strong style="font-size: 1.05em;">${esc(val)}</strong>`
+ `<em style="opacity: .8;">${esc(row.department)} · reports to ${esc(row.manager)}</em>`
+ `<span data-kmwc-fixed-colors style="justify-self: start; padding: 0 .45rem; border-radius: 999px; font-size: .85em; background: ${chipColor}; color: #fff;">${esc(chipText)}</span>`
+ `</div>`
}
},
{ field: 'email', title: 'Email', width: '180px', editor: 'text' },
{
field: 'department',
title: 'Dept (select)',
width: '160px',
editor: 'select',
editorOptions: {
options: departmentOptions,
showOnFocus: false,
iconMember: 'icon',
subtitleMember: 'subtitle',
disabledMember: 'disabled'
},
dropdownToggleVisibility: 'on-focus'
},
{
field: 'salary',
title: 'Salary',
width: '100px',
horizontalAlign: 'right',
editor: 'number',
cellClass: 'salary-highlight',
getCellClassCallback: ({ value: val, row }) => val >= 90000 ? 'salary-high' : null,
getDisplayValueCallback: ({ value: val }) => val != null ? `$${val.toLocaleString()}` : '',
// The draft value (ctx.value) while the row has unsaved edits — edit it and hover again
isTooltipHtml: true,
getTooltipCallback: ({ value: val, row, rowIndex, controller }) => {
if (val == null) return null
const isEdited = controller.isCellDirty(rowIndex, 'salary')
return `<div style="display: grid; gap: .25rem;">`
+ `<strong style="font-size: 1.05em;">$${val.toLocaleString()} / year</strong>`
+ `<em style="opacity: .8;">${esc(row.name)} · ≈ $${Math.round(val / 12).toLocaleString()} / month</em>`
+ `<span data-kmwc-fixed-colors style="justify-self: start; padding: 0 .45rem; border-radius: 999px; font-size: .85em; color: #fff; background: ${isEdited ? '#d97706' : val >= 90000 ? '#059669' : '#6366f1'};">`
+ `${isEdited ? '✎ edited, not saved' : val >= 90000 ? '★ 90k+ band' : 'standard band'}</span>`
+ `</div>`
}
},
{
field: 'startDate',
title: 'Start Date',
width: '140px',
editor: 'date',
editorOptions: {
dateFormat: 'DD.MM.YYYY',
minDate: '2015-01-01',
maxDate: '2030-12-31',
outputFormat: 'iso',
editStartSelection: 'select-all'
},
getDisplayValueCallback: ({ value: val }) => {
if (!val) return ''
const date = new Date(val)
const day = String(date.getDate()).padStart(2, '0')
const month = String(date.getMonth() + 1).padStart(2, '0')
const year = date.getFullYear()
return `${day}.${month}.${year}`
}
},
{
field: 'status',
title: 'Status (select)',
width: '140px',
editor: 'select',
editorOptions: {
options: statusOptions,
showOnFocus: false,
iconMember: 'icon',
disabledMember: 'disabled'
},
shouldOpenDropdownOnEnter: true,
tooltipMember: 'statusTooltip'
},
{
field: 'manager',
title: 'Manager (auto)',
width: '160px',
editor: 'autocomplete',
editorOptions: {
editStartSelection: 'select-all',
initialOptions: [
{ value: 'John Doe', label: 'John Doe' },
{ value: 'Jane Smith', label: 'Jane Smith' }
],
placeholder: 'Search managers...',
minSearchLength: 2,
debounceMs: 300,
showOnFocus: false,
searchCallback: async ({ term: query, row, signal }) => {
await new Promise(resolve => setTimeout(resolve, 500))
const allManagers = [
{ value: 'John Doe', label: 'John Doe' },
{ value: 'Jane Smith', label: 'Jane Smith' },
{ value: 'Mike Johnson', label: 'Mike Johnson' },
{ value: 'Sarah Lee', label: 'Sarah Lee' },
{ value: 'Robert Williams', label: 'Robert Williams' },
{ value: 'Emily Brown', label: 'Emily Brown' }
]
return allManagers.filter(m =>
m.label.toLowerCase().includes(query.toLowerCase())
)
}
}
},
{
field: 'location',
title: 'Location (combo)',
width: '150px',
editor: 'combobox',
editorOptions: { options: locationOptions, showOnFocus: false }
},
{ field: 'phone', title: 'Phone', width: '100px', editor: 'text' }
]
// Initialize grid
const grid = document.getElementById('grid')
grid.items = sampleData
grid.columns = columns
grid.pageSize = 5
grid.isEditingEnabled = true
grid.editTrigger = 'navigate'
grid.isRowNumbersShown = true
grid.sortMode = 'multiple'
grid.tooltipShowDelay = 0
grid.tooltipHideDelay = 100
// Lock rows that are On Leave or Inactive
grid.rowLocking = {
getIsLockedCallback: ({ row }) => row.status === 'On Leave' || row.status === 'Inactive'
}
// Inject custom styles for dynamic classes (Shadow DOM requires this)
grid.getCustomStylesCallback = () => `
.salary-highlight { background-color: #fef9c3 !important; }
.salary-high { background-color: #d1fae5 !important; color: #065f46; }
.row-inactive { opacity: 0.6; background-color: #f3f4f6 !important; }
`
// Dynamic row styling based on status
grid.getRowClassCallback = ({ row, rowIndex: index }) => {
if (row.status === 'On Leave' || row.status === 'Inactive') return 'row-inactive'
return null
}
// Ctrl+Delete runs the `delete` built-in: confirm first (veto), the grid removes the row
grid.beforeActionCallback = ({ action, row }) => action !== 'delete' || confirm(`Delete ${row.name}?`)
grid.onRowDelete = ({ rows }) => console.log('deleted', rows.map(r => r.name))
// Display focused row
const focusedRowEl = document.getElementById('focusedRowState')
grid.onFocusChange = ({ rowIndex, row }) => {
focusedRowEl.innerHTML = `Row ${rowIndex} — <strong>${row.name}</strong> (${row.department}, $${row.salary?.toLocaleString()})`
}
// Display current sort state (cascading sort)
const sortStateEl = document.getElementById('sortState')
grid.onDataRequest = ({ sort }) => {
if (!sort || sort.length === 0) {
sortStateEl.textContent = 'No sorting applied'
} else {
sortStateEl.innerHTML = sort.map((s, i) =>
`<span class="pill pill--info"><sup>${i + 1}</sup> ${s.column} ${s.direction === 'asc' ? '▲' : '▼'}</span>`
).join(' ')
}
}
// Disabled / Readonly States — a few editable rows (the attribute blocks the edits)
for (const id of ['grid-disabled', 'grid-readonly']) {
const stateGrid = document.getElementById(id)
stateGrid.items = sampleData.slice(0, 4).map(row => ({ ...row }))
stateGrid.columns = [
{ field: 'name', title: 'Name', editor: 'text' },
{ field: 'department', title: 'Department', editor: 'text' },
{ field: 'salary', title: 'Salary', horizontalAlign: 'right', editor: 'number' },
]
stateGrid.isEditingEnabled = true
}
// Wire up controls
document.getElementById('isStriped').addEventListener('change', (e) => {
grid.isStriped = e.target.checked
})
document.getElementById('isHoverable').addEventListener('change', (e) => {
grid.isHoverable = e.target.checked
})
document.getElementById('sortable').addEventListener('change', (e) => {
grid.sortMode = e.target.checked ? 'multiple' : 'none'
})
document.getElementById('isPagingEnabled').addEventListener('change', (e) => {
grid.isPagingEnabled = e.target.checked
})
document.getElementById('isEditable').addEventListener('change', (e) => {
grid.isEditingEnabled = e.target.checked
})
document.getElementById('navigate').addEventListener('change', (e) => {
grid.editTrigger = e.target.checked ? 'navigate' : 'dblclick'
})
document.getElementById('isDirtyIndicatorShown').addEventListener('change', (e) => {
grid.isDirtyIndicatorShown = e.target.checked
})
</script>