Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SortState type, getCurrentSortState and setCurrentSortState #27

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
22 changes: 21 additions & 1 deletion src/lib/DataTable.svelte.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ type TableConfig<T> = {
initialFilters?: { [id: string]: any[] };
};

type StoreState = {
columnId: string | null;
direction: SortDirection
}

/**
* Represents a data table with sorting, filtering, and pagination capabilities.
* @template T The type of data items in the table.
Expand All @@ -33,7 +38,7 @@ export class DataTable<T> {

#originalData = $state<T[]>([]);
#currentPage = $state(1);
#sortState = $state<{ columnId: string | null; direction: SortDirection }>({
#sortState = $state<StoreState>({
columnId: null,
direction: null
});
Expand Down Expand Up @@ -157,6 +162,21 @@ export class DataTable<T> {
}
this.#isSortDirty = false;
}
/**
* Sets the current sort state for the table.
*/
setCurrentSortState(value: StoreState) {
this.#sortState = value;
this.#applySort();
}

/**
* Gets the current sort state for the table.
* @returns {StoreState} The current sort state.
*/
getCurrentSortState(): StoreState {
return this.#sortState
}

/**
* Gets or sets the base data rows without any filtering or sorting applied.
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"sourceMap": true,
"strict": true,
"module": "NodeNext",
"moduleResolution": "NodeNext"
"moduleResolution": "NodeNext",
"useDefineForClassFields": true
}
}