Article pagination frontend implementation

This commit is contained in:
Florian Sylvain
2023-01-19 02:30:01 +01:00
parent 94b42d7e34
commit a4ee8f90ff
4 changed files with 40 additions and 13 deletions
+4 -1
View File
@@ -2,6 +2,7 @@ package articles
import ( import (
"fmt" "fmt"
"math"
"net/http" "net/http"
"os" "os"
"strconv" "strconv"
@@ -30,9 +31,10 @@ func getBuiltGetResponse(articles []Article, skip uint64, take uint64) map[strin
} }
slicedArticles := articles[skip:normTake] slicedArticles := articles[skip:normTake]
total := len(slicedArticles)
return map[string]any{ return map[string]any{
"content": slicedArticles, "content": slicedArticles,
"total": len(slicedArticles), "total": total,
"pagination": map[string]any{ "pagination": map[string]any{
"skip": skip, "skip": skip,
"take": take, "take": take,
@@ -41,6 +43,7 @@ func getBuiltGetResponse(articles []Article, skip uint64, take uint64) map[strin
"previous": getArticleSkipTakeFullUrl(skip-take, take), "previous": getArticleSkipTakeFullUrl(skip-take, take),
}, },
}, },
"last_page": math.Ceil(float64(articlesCap) / float64(take)),
} }
} }
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
+36 -9
View File
@@ -1,20 +1,29 @@
<script setup lang="ts"> <script setup lang="ts">
import { getArticles, type Article } from '@/utils/database'; import { onMounted, ref, type Ref } from 'vue'
import { onMounted, ref, type Ref } from 'vue';
import { RouterLink } from 'vue-router' import { RouterLink } from 'vue-router'
import { TabulatorFull as Tabulator } from 'tabulator-tables' import { TabulatorFull as Tabulator } from 'tabulator-tables'
import { baseApiUrl } from '@/utils/api'
const articles: Ref<Array<Article>> = ref([])
const table = ref<HTMLInputElement | string>('') const table = ref<HTMLInputElement | string>('')
const tabulator: Ref<Tabulator | undefined> = ref(undefined) const tabulator: Ref<Tabulator | undefined> = ref(undefined)
onMounted(async () => { onMounted(async () => {
articles.value = await (await getArticles('')).content
tabulator.value = new Tabulator(table.value, { tabulator.value = new Tabulator(table.value, {
data: articles.value,
reactiveData: true,
layout: 'fitColumns', layout: 'fitColumns',
locale: 'fr-FR',
reactiveData: true,
pagination: true,
paginationSizeSelector: true,
paginationMode: 'remote',
paginationSize: 10,
ajaxURL: `${baseApiUrl}/articles`,
ajaxURLGenerator: function (url, config, params) {
config.credentials = 'include'
return url + `?skip=${params.size * (params.page - 1)}&take=${params.size}`
},
dataReceiveParams: {
"data": "content"
},
columns: [ columns: [
{ {
title: 'Titre', title: 'Titre',
@@ -63,7 +72,23 @@ onMounted(async () => {
}, },
headerSort: false headerSort: false
} }
] ],
langs: {
"fr-FR": {
"pagination": {
"first": "Premier",
"first_title": "Première Page",
"last": "Dernier",
"last_title": "Dernière Page",
"prev": "Précédent",
"prev_title": "Page Précédente",
"next": "Suivant",
"next_title": "Page Suivante",
"all": "Toute",
"page_size": "Nombre d'éléments"
},
},
}
}) })
}) })
</script> </script>
@@ -81,6 +106,8 @@ main {
flex-direction: column; flex-direction: column;
gap: 16px; gap: 16px;
height: 100%;
padding: 32px; padding: 32px;
} }
@@ -90,7 +117,7 @@ main>a {
#table { #table {
width: 100%; width: 100%;
height: 100%;
margin: auto; margin: auto;
} }