tinker with the page query for game list

This commit is contained in:
iamBadgers
2024-05-18 18:47:09 -07:00
parent 97aab587df
commit 8025424f7b
3 changed files with 49 additions and 56 deletions

View File

@@ -49,9 +49,11 @@ app.post('/api/game', jsonParser, async (req, res) => {
order: orderBy, order: orderBy,
where: filter where: filter
}) })
const pageCount = Math.ceil((await Game.count({ const pageCount = Math.ceil(
(await Game.count({
where: filter where: filter
})) / count) })) / count
)
res.setHeader('Content-Type', 'application/json') res.setHeader('Content-Type', 'application/json')
res.send({ gameData, pageCount }) res.send({ gameData, pageCount })

View File

@@ -51,7 +51,7 @@ let count = 10
async function loadData() { async function loadData() {
const response = await axios.post('/api/character', { const response = await axios.post('/api/character', {
page: `${page.value - 1}`, page: `${page.value - 1}`,
count: `${count}`, count: `10`,
orderBy: 'id' orderBy: 'id'
}) })
@@ -60,7 +60,6 @@ async function loadData() {
} }
function setPage(targetPage: number) { function setPage(targetPage: number) {
console.log(targetPage)
router.replace({ query: { page: targetPage } }) router.replace({ query: { page: targetPage } })
} }

View File

@@ -1,94 +1,86 @@
<template> <template>
<div class="d-flex flex-row"> <div class="d-flex flex-row">
<input class="filter flex-1-0" placeholder="Filter by Name" v-model="filtervalue" /> <v-text-field
class="filter flex-1-0"
placeholder="Filter by Name"
v-model="filtervalue"
></v-text-field>
</div> </div>
<v-table> <game-table :gameList="games"></game-table>
<thead>
<tr>
<th class="text-left">ID</th>
<th class="text-left">Title</th>
<th class="text-left">Staus</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>
</tr>
</tbody>
</v-table>
<v-pagination <v-pagination
:model-value="page"
:length="pageCount" :length="pageCount"
@prev="previousPage()"
@next="nextPage()"
@update:modelValue="setPage($event)" @update:modelValue="setPage($event)"
></v-pagination> ></v-pagination>
</template> </template>
<style> <style>
label.filter { .filter {
margin: 10px; margin-top: 10px;
margin-left: 5px;
margin-right: 5px;
margin-bottom: 0px; margin-bottom: 0px;
} }
input.filter {
margin: 10px;
background: darkslategray;
border-color: lightgray;
}
</style> </style>
<script setup lang="ts"> <script setup lang="ts">
import GameTable from './GameTable.vue'
import { onMounted, watch, ref } from 'vue' import { onMounted, watch, ref } from 'vue'
import axios from 'axios' import axios from 'axios'
let page = 0 const route = useRoute()
let count = 10 const router = useRouter()
const games = ref({}) const games = ref({})
let page = ref(1)
const pageCount = ref(1) const pageCount = ref(1)
const filtervalue = ref("") let count = 10
const filtervalue = ref('')
async function loadData() { async function loadData() {
const response = await axios.post('/api/game', { const response = await axios.post('/api/game', {
page: `${page}`, page: `${page.value - 1}`,
count: '10', count: `10`,
filter: filtervalue.value ? `title:${filtervalue.value}` : "" filter: filtervalue.value ? `title:${filtervalue.value}` : '',
orderBy: 'id'
}) })
games.value = response.data.gameData games.value = response.data.gameData
pageCount.value = response.data.pageCount pageCount.value = response.data.pageCount
} }
async function nextPage() {
page++
loadData()
}
async function previousPage() {
page--
loadData()
}
function setPage(targetPage: number) { function setPage(targetPage: number) {
this.page = targetPage - 1 router.replace({ query: { page: targetPage } })
loadData()
} }
onMounted(async () => { watch(route, (newValue, oldValue) => {
if (!route.query.page) {
router.replace({ query: { page: 1 } })
page.value = 1
} else {
page.value = Number(route.query.page)
}
loadData() loadData()
}) })
let debounce = null
let debounce = null;
watch(filtervalue, (oldFilter, newFilter) => { watch(filtervalue, (oldFilter, newFilter) => {
debounce = clearTimeout(debounce) debounce = clearTimeout(debounce)
debounce = setTimeout(() => { debounce = setTimeout(() => {
loadData() loadData()
}, 500) }, 500)
}) })
onMounted(async () => {
if (!route.query.page) {
router.replace({ query: { page: 1 } })
page.value = 1
} else {
page.value = Number(route.query.page)
}
console.log(page.value)
loadData()
})
</script> </script>