update game list to use sortable columns

This commit is contained in:
iamBadgers
2024-06-11 16:35:48 -07:00
parent 2db71661f0
commit 976e12d91a
4 changed files with 67 additions and 42 deletions

View File

@@ -6,30 +6,30 @@
v-model="filtervalue"
></v-text-field>
</div>
<v-table>
<thead>
<tr>
<th class="text-left">ID</th>
<th class="text-left">Title</th>
<th class="text-left">Staus</th>
<th class="text-left">Post Date</th>
</tr>
</thead>
<tbody>
<tr v-for="game in games">
<td>
<a v-bind:href="`/#/games/${game.id}`">{{ game.id }}</a>
</td>
<td>
<a v-bind:href="`/#/games/${game.id}`">{{ game.title }}</a>
</td>
<td>{{ game.status }}</td>
<td>
{{ new Date(game.postdate * 1000).toISOString().split('T')[0] }}
</td>
</tr>
</tbody></v-table
<v-data-table-server
:headers="headers"
v-model:items-per-page="resltsPerPage"
:items="games"
:items-length="count"
v-model:page="page"
@update:options="loadData"
>
<template v-slot:item.id="{ item }">
<a v-bind:href="`/#/games/${item.id}`">{{ item.id }}</a>
</template>
<template v-slot:item.title="{ item }">
<a v-bind:href="`/#/games/${item.id}`">{{ item.title }}</a>
</template>
<template v-slot:item.postdate="{ item }">
{{ new Date(item.postdate * 1000).toISOString().split('T')[0] }}
</template>
<template #bottom></template>
</v-data-table-server>
<v-pagination
:model-value="page"
:length="pageCount"
@@ -53,27 +53,54 @@ import { onMounted, watch, ref } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import axios from 'axios'
const headers = [
{ title: 'ID', align: 'start', sortable: true, key: 'id' },
{ title: 'Title', align: 'start', sortable: true, key: 'title' },
{ title: 'Status', align: 'start', sortable: true, key: 'status' },
{ title: 'Post Date', align: 'start', sortable: true, key: 'postdate' }
]
const route = useRoute()
const router = useRouter()
const games = ref<Game[]>([])
let page = ref(1)
const pageCount = ref(1)
let page = ref(1)
const resultePerPage = ref(10)
let count = 10
let count = ref(10)
const filtervalue = ref<string>('')
async function loadData() {
const response = await axios.post('/api/game', {
async function loadData({ page, itemsPerPage, sortBy }) {
let sortString = 'id'
if (sortBy[0]) {
console.log(sortBy[0].key)
console.log(sortBy[0].order)
sortString = `${sortBy[0].key} ${sortBy[0].order}`
}
console.log(page)
console.log(itemsPerPage)
console.log(sortBy)
console.log({
page: `${page.value - 1}`,
count: `${count}`,
count: `${itemsPerPage}`,
orderBy: sortString,
filter: filtervalue.value ? `title:"${filtervalue.value}"` : '',
})
const response = await axios.post('/api/game', {
page: `${page - 1}`,
count: `${itemsPerPage}`,
orderBy: sortString,
filter: filtervalue.value ? `title:"${filtervalue.value}"` : '',
orderBy: 'id'
})
games.value = response.data.gameData
pageCount.value = response.data.pageCount
count.value = response.data.totalCount
}
function setPage(targetPage: number) {
@@ -91,7 +118,7 @@ watch(route, (newValue, oldValue) => {
if (route.query.filter) {
filtervalue.value = route.query.filter.toString()
}
loadData()
loadData({page: page.value, itemsPerPage: resultePerPage.value, sortBy:[]})
})
let debounce: ReturnType<typeof setTimeout>
@@ -112,6 +139,5 @@ onMounted(async () => {
if (route.query.filter) {
filtervalue.value = route.query.filter.toString()
}
loadData()
})
</script>