Skip to content

A continuation of HomeBox the inventory and organization system built for the Home User

feat: implement selectable view + sortable table (#264)

Haydenbd933af87409
diff --git a/frontend/components/Item/View/Table.vue b/frontend/components/Item/View/Table.vue
index f339a17a..3cc73512 100644
--- a/frontend/components/Item/View/Table.vue
+++ b/frontend/components/Item/View/Table.vue
@@ -6,20 +6,31 @@
<th
v-for="h in headers"
:key="h.value"
- class="text-no-transform text-sm bg-neutral text-neutral-content"
- :class="{
- 'text-center': h.align === 'center',
- 'text-right': h.align === 'right',
- 'text-left': h.align === 'left',
- }"
+ class="text-no-transform text-sm bg-neutral text-neutral-content cursor-pointer"
+ @click="sortBy(h.value)"
>
- <template v-if="typeof h === 'string'">{{ h }}</template>
- <template v-else>{{ h.text }}</template>
+ <div
+ class="flex items-center gap-1"
+ :class="{
+ 'justify-center': h.align === 'center',
+ 'justify-start': h.align === 'right',
+ 'justify-end': h.align === 'left',
+ }"
+ >
+ <template v-if="typeof h === 'string'">{{ h }}</template>
+ <template v-else>{{ h.text }}</template>
+ <div :class="`inline-flex ${sortByProperty === h.value ? '' : 'opacity-0'}`">
+ <span class="swap swap-rotate" :class="{ 'swap-active': pagination.descending }">
+ <Icon name="mdi-arrow-down" class="swap-on h-5 w-5" />
+ <Icon name="mdi-arrow-up" class="swap-off h-5 w-5" />
+ </span>
+ </div>
+ </div>
</th>
</tr>
</thead>
<tbody>
- <tr v-for="(d, i) in data" :key="i" class="hover cursor-pointer" @click="navigateTo(`/item/${d.id}`)">
+ <tr v-for="(d, i) in data" :key="d.id" class="hover cursor-pointer" @click="navigateTo(`/item/${d.id}`)">
<td
v-for="h in headers"
:key="`${h.value}-${i}`"
@@ -49,7 +60,7 @@
</tr>
</tbody>
</table>
- <div class="border-t p-3 justify-end flex">
+ <div v-if="hasPrev || hasNext" class="border-t p-3 justify-end flex">
<div class="btn-group">
<button :disabled="!hasPrev" class="btn btn-sm" @click="prev()">«</button>
<button class="btn btn-sm">Page {{ pagination.page }}</button>
@@ -80,7 +91,6 @@
});
const pagination = reactive({
- sortBy: sortByProperty.value,
descending: false,
page: 1,
rowsPerPage: 10,
@@ -97,20 +107,50 @@
return pagination.page > 1;
});
- const data = computed<TableData[]>(() => {
- // sort by property
- let data = [...props.items].sort((a, b) => {
- const aLower = a[sortByProperty.value]?.toLowerCase();
- const bLower = b[sortByProperty.value]?.toLowerCase();
+ function sortBy(property: keyof ItemSummary) {
+ if (sortByProperty.value === property) {
+ pagination.descending = !pagination.descending;
+ } else {
+ pagination.descending = false;
+ }
+ sortByProperty.value = property;
+ }
- if (aLower < bLower) {
- return -1;
+ 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;
}
- if (aLower > bLower) {
- return 1;
- }
- return 0;
- });
+
+ 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<TableData[]>(() => {
+ // sort by property
+ 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 372c0a9d..5ce3f0dc 100644
--- a/frontend/pages/home/index.vue
+++ b/frontend/pages/home/index.vue
@@ -82,20 +82,7 @@
<Subtitle> Recently Added </Subtitle>
<BaseCard v-if="breakpoints.lg">
- <Table :headers="itemTable.headers" :data="itemTable.items">
- <template #cell-warranty="{ item }">
- <Icon v-if="item.warranty" name="mdi-check" class="text-green-500 h-5 w-5" />
- <Icon v-else name="mdi-close" class="text-red-500 h-5 w-5" />
- </template>
- <template #cell-purchasePrice="{ item }">
- <Currency :amount="item.purchasePrice" />
- </template>
- <template #cell-location_Name="{ item }">
- <NuxtLink class="badge badge-sm badge-primary p-3" :to="`/location/${item.location.id}`">
- {{ item.location?.name }}
- </NuxtLink>
- </template>
- </Table>
+ <ItemViewTable :items="itemTable.items" />
</BaseCard>
<div v-else class="grid grid-cols-1 md:grid-cols-2 gap-4">
<ItemCard v-for="item in itemTable.items" :key="item.id" :item="item" />
diff --git a/frontend/pages/home/table.ts b/frontend/pages/home/table.ts
index e198bb44..127ecbb6 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 f530566b..052c6bec 100644
--- a/frontend/pages/item/[id]/index.vue
+++ b/frontend/pages/item/[id]/index.vue
@@ -518,11 +518,8 @@
</div>
</section>
- <section v-if="!hasNested" class="my-6">
- <BaseSectionHeader v-if="item && item.children && item.children.length > 0"> Child Items </BaseSectionHeader>
- <div class="grid grid-cols-1 sm:grid-cols-2 gap-4">
- <ItemCard v-for="child in item.children" :key="child.id" :item="child" />
- </div>
+ <section v-if="!hasNested && item.children.length > 0" class="my-6">
+ <ItemViewSelectable :items="item.children" />
</section>
</BaseContainer>
</template>
diff --git a/frontend/pages/label/[id].vue b/frontend/pages/label/[id].vue
index c9ac8ef3..de87a4d0 100644
--- a/frontend/pages/label/[id].vue
+++ b/frontend/pages/label/[id].vue
@@ -156,11 +156,8 @@
<DetailsSection :details="details" />
</BaseCard>
- <section v-if="label">
- <BaseSectionHeader class="mb-5"> Items </BaseSectionHeader>
- <div class="grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 gap-4">
- <ItemCard v-for="item in label.items" :key="item.id" :item="item" />
- </div>
+ <section v-if="label && label.items && label.items.length > 0">
+ <ItemViewSelectable :items="label.items" />
</section>
</BaseContainer>
</template>