From cc23a627e931ba69aa5dff38570a7f17c93e6aa0 Mon Sep 17 00:00:00 2001 From: Ortwin Date: Mon, 1 Jan 2024 14:10:23 +0100 Subject: [PATCH] global table search --- src/components/table/SongTable.svelte | 1 + src/components/table/Table.svelte | 73 +++++++++++++++++---------- src/data/lang/de.json | 3 +- src/data/lang/en.json | 3 +- 4 files changed, 52 insertions(+), 28 deletions(-) diff --git a/src/components/table/SongTable.svelte b/src/components/table/SongTable.svelte index 77db607..546c4cf 100644 --- a/src/components/table/SongTable.svelte +++ b/src/components/table/SongTable.svelte @@ -99,6 +99,7 @@ data={songs} idField='id' placeholder='No songs added.' + placeholderSearch={ $t('table.search') } exportTitle='My Song List' persistenceID='songs' groupHeader={format.groupBy} diff --git a/src/components/table/Table.svelte b/src/components/table/Table.svelte index c801260..ce79299 100644 --- a/src/components/table/Table.svelte +++ b/src/components/table/Table.svelte @@ -23,7 +23,7 @@ ValidateModule } from 'tabulator-tables'; import { default as ResponsiveLayoutModule, type CollapsedCellData } from './tabulator/modules/ResponsiveLayout'; - import type { ColumnDefinition, GroupComponent, ColumnComponent, Options, RowComponent } from 'tabulator-tables'; + import type { ColumnDefinition, GroupComponent, ColumnComponent, Options, RowComponent, FilterType } from 'tabulator-tables'; import { onMount, createEventDispatcher, onDestroy } from 'svelte'; import { Observable, fromEvent, map, take } from 'rxjs'; import { toggleVisibilityItem } from './templates/menu.helper'; @@ -68,11 +68,14 @@ export let data: Observable; export let groupBy: string[] = undefined; export let placeholder = ''; + export let placeholderSearch = ''; export let exportTitle = 'export'; export let groupHeader: GroupFormatter = undefined; export let detailFormatter: (data: CollapsedCellData[]) => HTMLElement = undefined; export const isGroupedBy = (field: string) => field in rowGroups; export let persistenceID = ''; + let useResponsiveLayout = false; + let searchTerm = ''; let table: Observable; let tableContainer: HTMLElement; let endOrientation = () => {}; @@ -129,7 +132,7 @@ function createTable(orientation: Orientation) { $table?.destroy(); - const useResponsiveLayout = orientation === 'portrait'; + useResponsiveLayout = orientation === 'portrait'; const tableInstance = new Tabulator(tableContainer, { ...options, headerVisible: !useResponsiveLayout, @@ -206,21 +209,15 @@ } } - export async function addRow(data: T): Promise { - try { - return $table.addRow(data); - } catch (error) { - showError(`Tabulator is unable to add a new row!\n${error.message}`); - } - } - - export async function focusField(row: RowComponent, field: string): Promise { - await row.scrollTo(); - if (field) { - const cell = row.getCell(field); - if (cell.getColumn().isVisible()) { - cell.edit(); - } + function search() { + if (searchTerm) { + // 2nd level array enforces OR comparison + $table.setFilter([columns + .filter(c => c.sorter === 'string') + .map(c => ({ field: c.field, type: 'like', value: searchTerm })) + ]); + } else { + $table.clearFilter(true); } } @@ -244,23 +241,47 @@ } + +
+ +
+
+
- \ No newline at end of file diff --git a/src/data/lang/de.json b/src/data/lang/de.json index c741646..5cb9f9a 100644 --- a/src/data/lang/de.json +++ b/src/data/lang/de.json @@ -95,7 +95,8 @@ }, "table": { "selectColumns": "Spalten auswählen", - "groupBy": "Gruppieren nach {title}" + "groupBy": "Gruppieren nach {title}", + "search": "Suchen nach etwas..." }, "blog": { "title": "Alles über das Musizieren" diff --git a/src/data/lang/en.json b/src/data/lang/en.json index 66cad41..5bfe097 100644 --- a/src/data/lang/en.json +++ b/src/data/lang/en.json @@ -95,7 +95,8 @@ }, "table": { "selectColumns": "Choose columns", - "groupBy": "Group by {title}" + "groupBy": "Group by {title}", + "search": "Search for anything..." }, "blog": { "title": "Everthing about making music"