From bd933af8740921b7aa56614743affc5f3612e79d Mon Sep 17 00:00:00 2001 From: Hayden <64056131+hay-kot@users.noreply.github.com> Date: Sun, 5 Feb 2023 14:00:33 -0900 Subject: [PATCH] feat: implement selectable view + sortable table (#264) --- frontend/components/Item/View/Table.vue | 86 ++++++++++++++++++------- frontend/pages/home/index.vue | 15 +---- frontend/pages/home/table.ts | 25 ------- frontend/pages/item/[id]/index.vue | 7 +- frontend/pages/label/[id].vue | 7 +- 5 files changed, 68 insertions(+), 72 deletions(-) diff --git a/frontend/components/Item/View/Table.vue b/frontend/components/Item/View/Table.vue index f339a17..3cc7351 100644 --- a/frontend/components/Item/View/Table.vue +++ b/frontend/components/Item/View/Table.vue @@ -6,20 +6,31 @@ - - +
+ + +
+ + + + +
+
- + -
+
@@ -80,7 +91,6 @@ }); const pagination = reactive({ - sortBy: sortByProperty.value, descending: false, page: 1, rowsPerPage: 10, @@ -97,20 +107,50 @@ return pagination.page > 1; }); + function sortBy(property: keyof ItemSummary) { + if (sortByProperty.value === property) { + pagination.descending = !pagination.descending; + } else { + pagination.descending = false; + } + sortByProperty.value = property; + } + + function extractSortable(item: ItemSummary, property: keyof ItemSummary): string | number | boolean { + const value = item[property]; + if (typeof value === "string") { + // Try parse float + const parsed = parseFloat(value); + if (!isNaN(parsed)) { + return parsed; + } + + return value.toLowerCase(); + } + + if (typeof value !== "number" && typeof value !== "boolean") { + return ""; + } + + return value; + } + + function itemSort(a: ItemSummary, b: ItemSummary) { + const aLower = extractSortable(a, sortByProperty.value); + const bLower = extractSortable(b, sortByProperty.value); + + if (aLower < bLower) { + return -1; + } + if (aLower > bLower) { + return 1; + } + return 0; + } + const data = computed(() => { // sort by property - let data = [...props.items].sort((a, b) => { - const aLower = a[sortByProperty.value]?.toLowerCase(); - const bLower = b[sortByProperty.value]?.toLowerCase(); - - if (aLower < bLower) { - return -1; - } - if (aLower > bLower) { - return 1; - } - return 0; - }); + let data = [...props.items].sort(itemSort); // sort descending if (pagination.descending) { diff --git a/frontend/pages/home/index.vue b/frontend/pages/home/index.vue index 372c0a9..5ce3f0d 100644 --- a/frontend/pages/home/index.vue +++ b/frontend/pages/home/index.vue @@ -82,20 +82,7 @@ Recently Added - - - - -
+
diff --git a/frontend/pages/home/table.ts b/frontend/pages/home/table.ts index e198bb4..127ecbb 100644 --- a/frontend/pages/home/table.ts +++ b/frontend/pages/home/table.ts @@ -1,5 +1,3 @@ -import { TableHeader } from "~~/components/global/Table.types"; - import { UserClient } from "~~/lib/api/user"; export function itemsTable(api: UserClient) { @@ -11,31 +9,8 @@ export function itemsTable(api: UserClient) { return data.items; }); - const headers = [ - { - text: "Name", - sortable: true, - value: "name", - }, - { - text: "Location", - value: "location.name", - }, - { - text: "Warranty", - value: "warranty", - align: "center", - }, - { - text: "Price", - value: "purchasePrice", - align: "center", - }, - ] as TableHeader[]; - return computed(() => { return { - headers, items: items.value || [], }; }); diff --git a/frontend/pages/item/[id]/index.vue b/frontend/pages/item/[id]/index.vue index f530566..052c6be 100644 --- a/frontend/pages/item/[id]/index.vue +++ b/frontend/pages/item/[id]/index.vue @@ -518,11 +518,8 @@
-
- Child Items -
- -
+
+
diff --git a/frontend/pages/label/[id].vue b/frontend/pages/label/[id].vue index c9ac8ef..de87a4d 100644 --- a/frontend/pages/label/[id].vue +++ b/frontend/pages/label/[id].vue @@ -156,11 +156,8 @@ -
- Items -
- -
+
+