pagination for game list

This commit is contained in:
jmosrael@gmail.com
2024-05-05 16:11:23 -07:00
parent 28d06df844
commit aab717da4d
2 changed files with 31 additions and 12 deletions

View File

@@ -85,9 +85,6 @@ app.post('/api/character', jsonParser, async (req, res) => {
}) })
const pageCount = Math.ceil(await Character.count() / count) const pageCount = Math.ceil(await Character.count() / count)
console.log(characterData)
console.log(pageCount)
res.setHeader('Content-Type', 'application/json') res.setHeader('Content-Type', 'application/json')
res.json({characterData, pageCount}) res.json({characterData, pageCount})
}) })
@@ -101,8 +98,14 @@ app.post('/api/game', jsonParser, async (req, res) => {
const orderBy = req.body.orderBy const orderBy = req.body.orderBy
const count = req.body.count const count = req.body.count
const gameData = await Game.findAll({
offset: page * count,
limit: count
})
const pageCount = Math.ceil(await Character.count() / count)
res.setHeader('Content-Type', 'application/json') res.setHeader('Content-Type', 'application/json')
res.send(await Game.findAll({ offset: page * count, limit: count })) res.send({gameData, pageCount})
}) })
app.get('/api/game/:gameId', async (req, res) => { app.get('/api/game/:gameId', async (req, res) => {

View File

@@ -8,7 +8,7 @@
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
<tr v-for="game in games.data"> <tr v-for="game in games">
<td> <td>
<a v-bind:href="`/#/games/${game.id}`">{{ game.id }}</a> <a v-bind:href="`/#/games/${game.id}`">{{ game.id }}</a>
</td> </td>
@@ -19,8 +19,7 @@
</tr> </tr>
</tbody> </tbody>
</v-table> </v-table>
<button @click="previousPage()">pp</button> <v-pagination :length="pageCount" @prev="previousPage()" @next="nextPage()" @update:modelValue="setPage($event)"></v-pagination>
<button @click="nextPage()">np</button>
</template> </template>
<style></style> <style></style>
@@ -31,18 +30,35 @@ import axios from 'axios'
let page = 1 let page = 1
let count = 10 let count = 10
const games = ref({}) const games = ref({})
const pageCount = ref({})
onMounted(async () => { async function loadData() {
games.value = await axios.post('/api/game', { page: `${page}`, count: '10', orderBy: 'id' }) const response = await axios.post('/api/game', {
}) page: `${page}`,
count: '10',
orderBy: 'id'
})
games.value = response.data.gameData
pageCount.value = response.data.pageCount
}
async function nextPage() { async function nextPage() {
page++ page++
games.value = await axios.post('/api/game', { page: `${page}`, count: '10', orderBy: 'id' }) loadData()
} }
async function previousPage() { async function previousPage() {
page-- page--
games.value = await axios.post('/api/game', { page: `${page}`, count: '10', orderBy: 'id' }) loadData()
} }
function setPage(targetPage:number) {
this.page=targetPage - 1
loadData()
}
onMounted(async () => {
loadData()
})
</script> </script>