Skip to content

Commit cb4b5a5

Browse files
committed
✨ 📝 Added ability to past onHeaderUpdate for custom header actions. Added usage info and details of what has been added in this fork.
1 parent cc55926 commit cb4b5a5

File tree

3 files changed

+76
-14
lines changed

3 files changed

+76
-14
lines changed

README.md

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,30 @@
11
[![npm](https://img.shields.io/npm/v/svelte-data-grid.svg?style=flat-square)](https://npmjs.org/package/svelte-data-grid)
2-
# Svelte Data Grid
2+
# Svelte Data Grid, slightly enhanced
3+
4+
This is a fork of [gamalielmendez's fork](https://github.com/gamalielmendez/svelte-data-grid)
5+
of [svelte-data-grid](https://github.com/bsssshhhhhhh/svelte-data-grid).
6+
7+
gamalielmendez's fork added:
8+
- Infinite scroll
9+
- Ability to select row
10+
- Move selection cursor with mouse wheel
11+
- Changecursor event
12+
- Move cursor with the keyboard arrows
13+
- Striped rows
14+
15+
This fork added/fixed:
16+
- Render "" instead of "undefined" if no data
17+
- Changed `on:change` to `on:blur` per warning message
18+
- Added tableWidth prop, allow last column to have auto width
19+
- Fixed input loses focus
20+
- Added debounce
21+
- Fixed dispatch
22+
- Added align prop
23+
- Lighter border colors
24+
- Added onCellUpdate and onHeaderUpdate (see usage below)
325

426
## [Demo](https://bsssshhhhhhh.github.io/svelte-data-grid-demo/)
5-
27+
### [Demo repo](https://github.com/bsssshhhhhhh/svelte-data-grid-demo)
628

729
Svelte Data Grid is a svelte v3 component for displaying and editing any amount of data.
830

@@ -197,6 +219,45 @@ import MyCustomCell from './MyCustomCell.svelte';
197219
## Custom Header Components
198220
Header components can also be specified in `columns` entries as the `headerComponent` property. Header components are only passed `column`, the column object from `columns`.
199221

222+
## onCellUpdate, onHeaderUpdate
223+
224+
`on:valueUpdated={handler}` doesn't seem to work for me (at least not for textbox-cell, `event.*` were all `undefined`).
225+
Thus, `onCellUpdate` callback is added for cell updates.
226+
227+
Likewise,`onHeaderUpdate` callback is added for custom events trigger on a custom header column. Eg.
228+
229+
MyCustomHeaderCol.svelte
230+
```svelte
231+
<script>
232+
export function onUpdate(action) {
233+
dispatch('update', {
234+
action,
235+
})
236+
}
237+
</script>
238+
239+
<i on:click={evt => onUpdate('someAction')}>
240+
```
241+
242+
MyCustomHeaderCol.svelte
243+
```svelte
244+
<script>
245+
$: columns = [ ...otherCols,
246+
{ headerComponent: MyCustomHeaderCol, disallowResize: true },
247+
]
248+
const onCellUpdate = (event) => {
249+
const { rowNumber, value } = event.detail
250+
}
251+
const onHeaderUpdate = (event) => {
252+
const { action } = event.detail
253+
}
254+
</script>
255+
<DataGrid {rows} {columns} {...props} {tableWidth}
256+
{onHeaderUpdate}
257+
{onCellUpdate}
258+
/>
259+
```
260+
200261
## Options:
201262

202263
Svelte Data Grid provides a few options for controlling the grid and its interactions:

src/index.svelte

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@
164164
export let Striped=false;
165165
export let tableWidth=0; // allow user to define table width
166166
export let onCellUpdate = () => {}
167+
export let onHeaderUpdate = () => {}
167168
168169
onMount(() => {
169170
editHistory = new EditHistory(rows);
@@ -340,9 +341,11 @@
340341
}
341342
342343
/**
343-
* DOESN'T WORK!
344344
* Event handler for when a value has been updated
345345
* @param {Object} event Event object with row and column objects
346+
* @note `on:valueUpdated={handler}` doesn't seem to work for me
347+
* (at least not for textbox-cell, `event.*` were all `undefined`).
348+
* Thus, `onCellUpdate` callback is added and will be used instead.
346349
*/
347350
function onCellUpdated(event) {
348351
rows[event.detail.rowNumber][event.detail.column.dataName] =
@@ -974,7 +977,6 @@
974977
.stripedRow{
975978
background-color: #ccc;
976979
}
977-
978980
</style>
979981

980982
<svelte:window
@@ -1016,7 +1018,9 @@
10161018
use:dragCopy={allowColumnReordering}
10171019
role="columnheader">
10181020
{#if column.headerComponent}
1019-
<svelte:component this={column.headerComponent} {column} />
1021+
<svelte:component this={column.headerComponent} {column}
1022+
on:update={onHeaderUpdate}
1023+
/>
10201024
{:else}
10211025
<div class="cell-default">{column.display || ''}</div>
10221026
{/if}

src/textbox-cell.svelte

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
export let updateRate=2000;
99
let prevColumn;
1010
let prevRow;
11+
let timeoutId;
12+
1113
// [svelte-upgrade warning]
1214
// beforeUpdate and afterUpdate handlers behave
1315
// differently to their v2 counterparts
@@ -24,9 +26,6 @@
2426
prevColumn = column;
2527
}
2628
});
27-
// [svelte-upgrade warning]
28-
// beforeUpdate and afterUpdate handlers behave
29-
// differently to their v2 counterparts
3029
afterUpdate(() => {
3130
/* Since data-grid isn't using a keyed each block to display the rows, we need to update
3231
the focus as the grid scrolls. When this cell component receives a new row, check if the column's active row
@@ -40,21 +39,19 @@
4039
prevRow = row;
4140
}
4241
});
43-
// [svelte-upgrade suggestion]
44-
// review these functions and remove unnecessary 'export' keywords
45-
export function onFocus(event) {
42+
43+
function onFocus(event) {
4644
column.activeRow = rowNumber;
4745
}
48-
export function onBlur(event) {
46+
function onBlur(event) {
4947
// if blur event was user-initiated and not initiated by the blur call above,
5048
// remove the activeRow property
5149
if (event.sourceCapabilities) {
5250
delete column.activeRow;
5351
}
5452
else event.srcElement.focus() // First inputbox loses focus after 1st char for some reason
5553
}
56-
let timeoutId
57-
export function onInput(event) {
54+
function onInput(event) {
5855
const value = textbox.value;
5956
clearTimeout(timeoutId)
6057
timeoutId = setTimeout(() => {

0 commit comments

Comments
 (0)