Skip to content

Commit f77aa18

Browse files
committed
✨ Added onClickCell, onMouseEnterCell, onMouseLeaveCell handler props
1 parent 02734af commit f77aa18

File tree

2 files changed

+25
-13
lines changed

2 files changed

+25
-13
lines changed

README.md

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ This fork added/fixed:
2222
- Added `align` col config
2323
- Added `headerAlign` col config to override default header cell 'center' text-align
2424
- Lighter border colors
25-
- Added `onCellUpdate` and `onHeaderUpdate` (see usage below)
25+
- Added update handlers `onCellUpdate` and `onHeaderUpdate` (see usage below)
26+
- Added cell event handlers `onClickCell`, `onMouseEnterCell`, and `onMouseLeaveCell` (see usage below)
2627

2728
## [Demo](https://bsssshhhhhhh.github.io/svelte-data-grid-demo/)
2829
### [Demo repo](https://github.com/bsssshhhhhhh/svelte-data-grid-demo)
@@ -43,20 +44,20 @@ Svelte Data Grid is a svelte v3 component for displaying and editing any amount
4344
## Install:
4445

4546
To install this fork, replace `x.y` with the latest in [releases](https://github.com/fuzzthink/svelte-data-grid/releases)
46-
```
47+
```cs
4748
npm install -D https://github.com/fuzzthink/svelte-data-grid/tarball/v1.x.y
4849
yarn add -D https://github.com/fuzzthink/svelte-data-grid/tarball/v1.x.y
4950
```
5051

5152
## Usage:
5253
Within a svelte component:
53-
```
54+
```js
5455
import DataGrid from "svelte-data-grid";
5556
<DataGrid rows={myRows} allowColumnReordering={false} columns={myColumnDefinitions} on:columnOrderUpdated={saveNewColumnOrder}>
5657
```
5758

5859
Outside of svelte:
59-
```
60+
```js
6061
import DataGrid from "svelte-data-grid";
6162
const grid = new DataGrid({
6263
target: document.querySelector('#my-grid-wrapper'),
@@ -72,13 +73,14 @@ grid.$on('columnOrderUpdated', () => {
7273
// save new column order
7374
});
7475
```
76+
7577
To learn more about using DataGrid outside of svelte, read [svelte's guide](https://svelte.dev/docs#Client-side_component_API) on how to interact with a svelte component. It is possible to integrate into any framework.
7678

7779
DataGrid requires 2 properties to be passed in order to display data: `rows` and `columns`.
7880

7981
`columns` is an array of objects containing at least 3 properties: `display`, `dataName`, and `width`. A svelte component can be specified in `headerComponent` and `cellComponent` if any custom cell behavior is required.
8082

81-
```
83+
```js
8284
[
8385
{
8486
display: 'Fruit Name', // What will be displayed as the column header
@@ -98,7 +100,7 @@ DataGrid requires 2 properties to be passed in order to display data: `rows` and
98100

99101
`rows` is an array of objects containing the data for each table row.
100102

101-
```
103+
```js
102104
[
103105
{
104106
fruitName: 'Apple',
@@ -121,15 +123,15 @@ DataGrid requires 2 properties to be passed in order to display data: `rows` and
121123
Version 2 added early support for editing data. Due to the lack of using a keyed each block to render the rows, maintaining focus on controls as the user scrolls is a tad wonky. This will be resolved in a future version.
122124

123125
Import the components:
124-
```
126+
```js
125127
import TextboxCell from 'svelte-data-grid/src/textbox-cell.svelte';
126128
import SelectCell from 'svelte-data-grid/src/select-cell.svelte';
127129
import CheckboxCell from 'svelte-data-grid/src/checkbox-cell.svelte';
128130
```
129131

130132
### Textbox Cell
131133
Textbox cell will debounce the user input, only recording changes after 400ms has elapsed since the user stops typing.
132-
```
134+
```js
133135
{
134136
display: 'Name',
135137
dataName: 'name',
@@ -141,7 +143,7 @@ Textbox cell will debounce the user input, only recording changes after 400ms ha
141143
### Select Cell
142144

143145
SelectCell requires that you provide an `options` array in your cell definition:
144-
```
146+
```js
145147
{
146148
display: 'Eye Color',
147149
dataName: 'eyeColor',
@@ -166,7 +168,7 @@ SelectCell requires that you provide an `options` array in your cell definition:
166168

167169
### Checkbox Cell
168170
CheckboxCell will set the checked state of the checkbox depending on the boolean value of the row's data.
169-
```
171+
```js
170172
{
171173
display: 'Active',
172174
dataName: 'isActive',
@@ -187,7 +189,7 @@ Components will be passed the following properties:
187189

188190

189191
MyCustomCell.svelte
190-
```
192+
```js
191193
<script>
192194
export let data = {
193195
colors: {
@@ -203,12 +205,12 @@ export let data = {
203205
```
204206

205207
Import the component
206-
```
208+
```js
207209
import MyCustomCell from './MyCustomCell.svelte';
208210
```
209211

210212
`columns` option:
211-
```
213+
```js
212214
[
213215
{
214216
display: 'Fruit Color'
@@ -222,6 +224,10 @@ import MyCustomCell from './MyCustomCell.svelte';
222224
## Custom Header Components
223225
Header components can also be specified in `columns` entries as the `headerComponent` property. Header components are only passed `column`, the column object from `columns`.
224226

227+
## onClickCell, onMouseEnterCell, onMouseLeaveCell
228+
229+
You can pass these handlers to handle mouse on cell events. The params are all: (value, columnName, iRow, iCol)
230+
225231
## onCellUpdate, onHeaderUpdate
226232

227233
`on:valueUpdated={handler}` doesn't seem to work for me (at least not for textbox-cell, `event.*` were all `undefined`).

src/index.svelte

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,9 @@
164164
export let Striped=false;
165165
export let tableWidth = 0; // allow user to define table width. Defining this will __override__ last col's width settings to the reminding width
166166
export let onCellUpdate = () => {}
167+
export let onClickCell = () => {}
168+
export let onMouseEnterCell = () => {}
169+
export let onMouseLeaveCell = () => {}
167170
export let onHeaderUpdate = () => {}
168171
169172
onMount(() => {
@@ -1108,6 +1111,9 @@
11081111
)}px; height: {rowHeight}px; line-height: {rowHeight}px; width: {
11091112
j===columnWidths.length-1 ? lastColWidth : columnWidths[j]+'px'
11101113
}; {column.align ? 'text-align:'+column.align+';' : ''}"
1114+
on:click={() => onClickCell(row.data[column.dataName], column.dataName, i, j)}
1115+
on:mouseenter={() => onMouseEnterCell(row.data[column.dataName], column.dataName, i, j)}
1116+
on:mouseleave={() => onMouseLeaveCell(row.data[column.dataName], column.dataName, i, j)}
11111117
role="cell">
11121118
{#if column.cellComponent}
11131119
<svelte:component

0 commit comments

Comments
 (0)