Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/docs/async-table.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import '../static/c/async-table.css';
| showArrangingColumns | | `boolean` | should the button for arranging columns be shown |
| showExport | | `boolean` | should the button for export be shown |
| dropdownMenuExport | | `boolean` | should the button for export download csv or can you have more download options |
| showResetToDefault | | `boolean` | should the button for reseting to default be shown |
<br></br>
****

Expand Down
1 change: 1 addition & 0 deletions packages/demo/src/OtherDemos/AsyncTableDemo.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@
asyncTable.height = '500px';
asyncTable.freezeFirstColumn = true;
asyncTable.freezeLastColumn = true;
asyncTable.showResetToDefault = true;
el.appendChild(asyncTable);
});

Expand Down
25 changes: 20 additions & 5 deletions packages/lib/src/async-table/async-table.wc.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
LOADING: 'Loading',
LOAD_MORE: 'Load more',
PAGE_SIZE: 'Page size',
SAVE: 'Save'
SAVE: 'Save',
RESET: 'Reset'
};

export let allowArrangeColumns = true;
Expand All @@ -41,9 +42,11 @@
export let service: TableService;
export let id: string;
export let height: string | null = null;
export let showResetToDefault = true;

let additionalExportTypes = [];
let activeHeaders: TableHeader[] = [];
const defaultHeaders = headers;

let isOpen = false;
let resolved: string[] = [];
Expand Down Expand Up @@ -134,7 +137,7 @@
const { key, fallback, pipes } = header;

let value: any;

try {
value = get(row, key);
} catch {
Expand Down Expand Up @@ -489,11 +492,23 @@
exportDataGeneric(option);
isOpen = false;
};

function handleReset() {
headers = defaultHeaders;
headers.forEach((header) => {
header.disabled = false;
});
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This actually isn't correct. The default state of a table might be to have some columns disabled initially.

if (service.getColumnOrder) {
const pulledHeaders = await service.getColumnOrder(id);
if (pulledHeaders) {
headers = headers
.map((header) => {
header.disabled = !pulledHeaders.find((it) => it.key === header.key);
return header;
})
.sort((a, b) => {
const aIndex = pulledHeaders.findIndex((it) => it.key === a.key);
const bIndex = pulledHeaders.findIndex((it) => it.key === b.key);
return aIndex - bIndex;
});
}
}

This is where we pull column order from the database and override the default. You need to save a reference of the default and in set it as the current headers in handleReset()

</script>

<div class="jp-async-table">
{#if showArrangingColumns || showImport || showExport}
<div class="jp-async-table-header">
{#if showResetToDefault}
<button on:click={handleReset} class="jp-async-table-button">
{wording.RESET}
</button>
{/if}
{#if showArrangingColumns}
&nbsp;
<button type="button" on:click={arrangeColumns} class="jp-async-table-button">
Expand Down Expand Up @@ -566,7 +581,7 @@
class:jp-async-table-sticky-last={index === headers.length - 1 && freezeLastColumn}
class:jp-async-table-no-cursor={header.disableOrganize}
class:jp-async-table-hover-over={hoveringOverColumnIndex === index}
class={`jp-async-table-header-cell-${header.key.slice(1).replace(/\//g, '(?!^)\/')}`}
class={`jp-async-table-header-cell-${header.key.slice(1).replace(/\//g, '(?!^)/')}`}
draggable={allowArrangeColumns && !header.disableOrganize}
on:click={() => adjustSort(header)}
on:dragstart={(e) => {
Expand Down Expand Up @@ -615,7 +630,7 @@
class:jp-async-table-sticky-last={columnIndex === headers.length - 1 &&
freezeLastColumn}
class:jp-async-table-hover-over={hoveringOverColumnIndex === columnIndex}
class={`jp-async-table-cell-${header.key.slice(1).replace(/\//g, '(?!^)\/')}`}
class={`jp-async-table-cell-${header.key.slice(1).replace(/\//g, '(?!^)/')}`}
on:click={(e) => rowClick(row, rowIndex, header, e)}
>
{#await handleColumn(header, row, rowIndex) then val}
Expand Down Expand Up @@ -699,7 +714,7 @@
if (!column.disableOrganize) drop(e, index);
}}
>
{@html dragHandleIcon}
{@html dragHandleIcon}
</span><input type="checkbox" value={true} bind:checked={column.enabled} />
<span>{@html column.label}</span>
</label>{/if}
Expand Down