Data Grid settings to 1x click instead of 2x click (svelte)
Please add settings to use 1x mouse click instead of 2x mouse click to edit a fields in data grid.
1x click gives way better experience, than 2x click. At least for my use cases.
Hello @kwladyka,
It is currently possible to do so by listening to the "focus-cell" event and invoking the "open-editor" event:
Code Snippet
<script>
import { Willow, Grid } from "@svar-ui/svelte-grid";
import { data, columns } from "./data.js"
function init(api) {
let editorCell = null;
let pendingClose = null;
const { editor } = api.getReactiveState();
editor.subscribe(v => {
if (v) editorCell = { id: v.id, column: v.column };
});
api.on("close-editor", () => {
pendingClose = editorCell;
setTimeout(() => (pendingClose = null));
});
api.on("focus-cell", ({ row, column, eventSource }) => {
if (eventSource !== "click" || !row || !column) return;
if (
pendingClose &&
pendingClose.id === row &&
pendingClose.column === column
) {
pendingClose = null;
return;
}
api.exec("open-editor", { id: row, column });
});
}
</script>
<Willow>
<Grid {init} {data} {columns} />
</Willow>
Demo: https://snippet.svar.dev/svelte/S8fgBE6ry
When editor closes, it refocuses the cell, which means it would instantly reopen the editor. To prevent that, we listen to "close-editor" to temporarily save currently selected cell, and compare it to the cell passed to the "open-editor" event. If they are equal, we return early from the event and the editor won't reopen.
There are plans to make this easier in the future. If you have any more questions, please let us know.
Thank you. I have a follow up question to determine if it is a bug. Any way to fix deselecting cell / row after click on another table? It works when click not on any table.
- click on cell in table 1
- click on cell in table 2
in step 2) the selected cell in 1) should disappear, but it doesn't.
It works for:
- click on cell in table 1
- click outside of the table 1, but not on table 2
https://snippet.svar.dev/svelte/ukCPjBAGE
To summary this up, I want to:
- deselect cell after click anywhere outside of the table
- deselect row after click anywhere outside of the table
Hello,
I was able to identify the issue. It was introduced in the previous workaround. The fix is to defer the "open-editor" call by wrapping it into a setTimeout:
setTimeout(() => api.exec("open-editor", { id: row, column }));
Updated Code Snippet
<script>
import { Willow, Grid } from "@svar-ui/svelte-grid";
import { data, columns } from "./data.js"
function init(api) {
let editorCell = null;
let pendingClose = null;
const { editor } = api.getReactiveState();
editor.subscribe(v => {
if (v) editorCell = { id: v.id, column: v.column };
});
api.on("close-editor", () => {
pendingClose = editorCell;
setTimeout(() => (pendingClose = null));
});
api.on("focus-cell", ({ row, column, eventSource }) => {
if (eventSource !== "click" || !row || !column) return;
if (
pendingClose &&
pendingClose.id === row &&
pendingClose.column === column
) {
pendingClose = null;
return;
}
setTimeout(() => api.exec("open-editor", { id: row, column }));
});
}
</script>
<div>
Table 1
<Willow>
<Grid {init} {data} {columns} />
</Willow>
Table 2
<Willow>
<Grid {init} {data} {columns} />
</Willow>
</div>
Demo: https://snippet.svar.dev/svelte/5mCW4aM1y
It was caused by the fact that "open-editor" swapped cell's DOM node during the click event's bubble phase, which made the click-outside handler to fail.
Note that row selection (highlighted rows) staying in the grid after clicking away is not a bug. You can also turn selection off entirely by specifying select={false} on a grid, or modify its appearance using CSS variables (--wx-table-select-* variables).
@segfault44 Thank you ❤️
It seems to work. It will be great to have it as configuration instead of workaround. To summary this up for settings:
- 1 click vs 2 click
- keep selection after lost focus or not for row
- keep selection after lost focus or not for cell
If there is no plan, then it is worth to consider to add the example into official doc. I mean, I can be wrong, but probably many people have the same concern. Workaround is simple, but not straight forward without deep knowledge about Svar. AI didn't figure out this too :)
@segfault44 Is there any workaround trick for deselect row when click outside the table? I can't figure out this.
I did something like this:
let interestContainer: HTMLElement | undefined = $state();
function initInterest(api: any) {
interestApi = api;
}
function clearInterestSelection() {
const selectedId = interestApi?.getState().selectedRows?.[0];
if (selectedId !== undefined) {
interestApi.exec('select-row', { id: selectedId, toggle: true });
}
}
function handleWindowClick(event: MouseEvent) {
if (interestContainer && !interestContainer.contains(event.target as Node)) {
clearInterestSelection();
}
}
onMount(() => {
window.addEventListener('click', handleWindowClick);
return () => window.removeEventListener('click', handleWindowClick);
});
it works, but it is too complex.
Hello @kwladyka,
The SVAR Grid library comes with a lib-dom helper library, that contains a clickOutside function, which can be used as a Svelte action (the use: directive). As the name suggests, it fires when the user clicks outside of a container it's used on.
Here's the revised example:
Code
<script>
import { Willow, Grid } from "@svar-ui/svelte-grid";
import { clickOutside } from "@svar-ui/lib-dom";
import { data, columns } from "./data.js";
function init(api) {
let editorCell = null;
let pendingClose = null;
const { editor } = api.getReactiveState();
editor.subscribe(v => {
if (v) editorCell = { id: v.id, column: v.column };
});
api.on("close-editor", () => {
pendingClose = editorCell;
setTimeout(() => (pendingClose = null));
});
api.on("focus-cell", ({ row, column, eventSource }) => {
if (eventSource !== "click" || !row || !column) return;
if (
pendingClose &&
pendingClose.id === row &&
pendingClose.column === column
) {
pendingClose = null;
return;
}
setTimeout(() => api.exec("open-editor", { id: row, column }));
});
}
function deselect(api) {
return () => {
if (!api) return;
[...api.getState().selectedRows].forEach(id =>
api.exec("select-row", { id, toggle: true })
);
};
}
let api1 = $state();
let api2 = $state();
</script>
<div>
Table 1
<Willow>
<div style:width="max-content" use:clickOutside={deselect(api1)}>
<Grid bind:this={api1} {init} {data} {columns} />
</div>
</Willow>
Table 2
<Willow>
<div style:width="max-content" use:clickOutside={deselect(api2)}>
<Grid bind:this={api2} {init} {data} {columns} />
</div>
</Willow>
</div>
Demo: https://snippet.svar.dev/svelte/0SggiSQ6r
And here is a diff to make the changes easier to spot:
Code Diff
@@ -1,5 +1,6 @@
<script>
import { Willow, Grid } from "@svar-ui/svelte-grid";
+ import { clickOutside } from "@svar-ui/lib-dom";
import { data, columns } from "./data.js";
function init(api) {
@@ -31,18 +32,34 @@
setTimeout(() => api.exec("open-editor", { id: row, column }));
});
}
+
+ function deselect(api) {
+ return () => {
+ if (!api) return;
+ [...api.getState().selectedRows].forEach(id =>
+ api.exec("select-row", { id, toggle: true })
+ );
+ };
+ }
+
+ let api1 = $state();
+ let api2 = $state();
</script>
<div>
Table 1
<Willow>
- <Grid {init} {data} {columns} />
+ <div style:width="max-content" use:clickOutside={deselect(api1)}>
+ <Grid bind:this={api1} {init} {data} {columns} />
+ </div>
</Willow>
Table 2
<Willow>
- <Grid {init} {data} {columns} />
+ <div style:width="max-content" use:clickOutside={deselect(api2)}>
+ <Grid bind:this={api2} {init} {data} {columns} />
+ </div>
</Willow>
</div>
You can get the API from a SVAR widget by binding to it (e.g. bind:this={api}). The clickOutside action accepts a callback, so deselect() returns a function, that toggles selection on every selected row in the provided API.
If you need any further assistance, please let us know, we'll be happy to help.