pagination for character list

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

View File

@@ -79,8 +79,17 @@ app.post('/api/character', 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 characterData = await Character.findAll({
offset: page * count,
limit: 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.send(await Character.findAll({ offset: page * count, limit: count })) res.json({characterData, pageCount})
}) })
app.get('/api/game/:characterId', async (req, res) => { app.get('/api/game/:characterId', async (req, res) => {

View File

@@ -11,7 +11,7 @@
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
<tr v-for="character in characters.data"> <tr v-for="character in characters">
<td> <td>
<a v-bind:href="`/#/characters/${character.id}`">{{ character.id }}</a> <a v-bind:href="`/#/characters/${character.id}`">{{ character.id }}</a>
</td> </td>
@@ -26,6 +26,7 @@
</tr> </tr>
</tbody> </tbody>
</v-table> </v-table>
<v-pagination :length="pageCount" @prev="previousPage()" @next="nextPage()" @update:modelValue="setPage($event)"></v-pagination>
<button @click="previousPage()">pp</button> <button @click="previousPage()">pp</button>
<button @click="nextPage()">np</button> <button @click="nextPage()">np</button>
</template> </template>
@@ -39,19 +40,23 @@ import axios from 'axios'
let page = 0 let page = 0
let count = 10 let count = 10
const characters = ref({ const characters = ref({})
data: [ const pageCount = ref({})
{ id: 1, name: 'test', player: 'test player', status: 'active', lastGame: 0 },
{ id: 1, name: 'test', player: 'test player', status: 'active', lastGame: 0 }
]
})
async function loadData() { async function loadData() {
characters.value = await axios.post('/api/character', { const response = await axios.post('/api/character', {
page: `${page}`, page: `${page}`,
count: `${count}`, count: `${count}`,
orderBy: 'id' orderBy: 'id'
}) })
console.log(response)
characters.value = response.data.characterData
pageCount.value = response.data.pageCount
console.log(characters.value)
console.log(pageCount.value)
} }
function previousPage() { function previousPage() {
@@ -64,6 +69,11 @@ function nextPage() {
loadData() loadData()
} }
function setPage(targetPage:number) {
this.page=targetPage - 1
loadData()
}
onMounted(async () => { onMounted(async () => {
loadData() loadData()
}) })