Skip to content

Commit 41c7ba9

Browse files
BigrounoursDaBadBunny
authored andcommitted
feat: ✨ Ajoute la possibilité de mettre des valeurs par défaut dans la pagination
1 parent 2c64774 commit 41c7ba9

File tree

2 files changed

+40
-11
lines changed

2 files changed

+40
-11
lines changed

src/components/DsfrTable/DsfrTable.stories.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@ export default {
4444
action: 'clicked on row',
4545
description: 'Fonction pour montrer le clic sur une ligne (Ici seulement la 2e ligne)',
4646
},
47+
defaultCurrentPage: {
48+
control: 'number',
49+
description: 'Le numéro de la page dans la pagination',
50+
},
51+
defaultOptionSelected: {
52+
control: 'number',
53+
description: 'La sélection du nombre d\'enregistrements par page',
54+
},
4755
},
4856
}
4957

@@ -53,7 +61,7 @@ const rows = [
5361
[
5462
'EGAUD',
5563
'Pierre-Louis',
56-
'pierre.egaud@gmail.com',
64+
'pierre.egaud@castor.fr',
5765
'02 04 06 08 10',
5866
'06 05 04 03 02',
5967
{
@@ -70,7 +78,7 @@ const rows = [
7078
rowData: [
7179
'DEBROIZE',
7280
'Clément',
73-
'clement.debroize@gmail.com',
81+
'clement.debroize@exile.com',
7482
'02 44 66 55 99',
7583
'07 88 77 22 33',
7684
{
@@ -245,6 +253,8 @@ const rows = [
245253
]
246254
const noCaption = false
247255
const pagination = true
256+
const defaultCurrentPage = 2
257+
const defaultOptionSelected = 5
248258

249259
export const TableauEntier = (args) => ({
250260
components: {
@@ -270,6 +280,8 @@ export const TableauEntier = (args) => ({
270280
:rows="rows"
271281
:no-caption="noCaption"
272282
:pagination="pagination"
283+
:defaultCurrentPage="defaultCurrentPage"
284+
:defaultOptionSelected="defaultOptionSelected"
273285
/>
274286
`,
275287

@@ -280,4 +292,6 @@ TableauEntier.args = {
280292
rows,
281293
noCaption,
282294
pagination,
295+
defaultCurrentPage,
296+
defaultOptionSelected,
283297
}

src/components/DsfrTable/DsfrTable.vue

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<script lang="ts" setup>
2-
import { ref, watch, computed, onMounted } from 'vue'
2+
import { ref, watch } from 'vue'
33
import DsfrTableRow, { type DsfrTableRowProps } from './DsfrTableRow.vue'
44
import DsfrTableHeaders from './DsfrTableHeaders.vue'
55
import { type DsfrTableHeaderProps } from './DsfrTableHeader.vue'
@@ -10,19 +10,23 @@ const props = withDefaults(defineProps<{
1010
rows?: (DsfrTableRowProps | string)[]
1111
noCaption?: boolean
1212
pagination?: boolean
13+
defaultCurrentPage?: number
14+
defaultOptionSelected?: number
1315
}>(), {
1416
title: undefined,
1517
headers: () => [],
1618
rows: () => [],
19+
defaultCurrentPage: 1,
20+
defaultOptionSelected: 10,
1721
})
1822
1923
const getRowData = (row: (DsfrTableRowProps | string | ({component: string} & Record<string, any>))) => {
2024
// @ts-ignore TODO: find a way to improve types here
2125
return row.rowData || row
2226
}
2327
24-
const currentPage = ref(1)
25-
const optionSelected = ref(10)
28+
const currentPage = ref(props.defaultCurrentPage)
29+
const optionSelected = ref(props.defaultOptionSelected)
2630
const pageCount = ref(props.rows.length > optionSelected.value ? Math.ceil(props.rows.length / optionSelected.value) : 1)
2731
const paginationOptions = [5, 10, 25, 50, 100]
2832
const returnLowestLimit = () => currentPage.value * optionSelected.value - optionSelected.value
@@ -39,9 +43,17 @@ watch(() => currentPage.value, (newVal, OldVal) => {
3943
})
4044
4145
const goFirstPage = () => { currentPage.value = 1 }
42-
const goPreviousPage = () => { currentPage.value > 1 ? currentPage.value -= 1 : currentPage.value }
43-
const goNextPage = () => { currentPage.value < pageCount.value ? currentPage.value += 1 : currentPage.value }
44-
const goLastPage = () => { currentPage.value = pageCount.value }
46+
const goPreviousPage = () => {
47+
if (currentPage.value > 1) {
48+
currentPage.value -= 1
49+
}
50+
}
51+
const goNextPage = () => {
52+
if (currentPage.value < pageCount.value) {
53+
currentPage.value += 1
54+
}
55+
}
56+
const goLastPage = () => { currentPage.value = pageCount.value }
4557
4658
</script>
4759

@@ -87,17 +99,20 @@ const goLastPage = () => { currentPage.value = pageCount.value }
8799
<option
88100
v-for="(option, idx) in paginationOptions"
89101
:key="idx"
90-
:value="option">
102+
:value="option"
103+
>
91104
{{ option }}
92105
</option>
93106
</select>
94107
</div>
95-
<div class="flex ml-1"><span class="self-center">Page {{ currentPage }} sur {{ pageCount }}</span></div>
108+
<div class="flex ml-1">
109+
<span class="self-center">Page {{ currentPage }} sur {{ pageCount }}</span>
110+
</div>
96111
<div class="flex ml-1">
97112
<button
98113
class="fr-icon-arrow-left-s-first-line"
99114
@click="goFirstPage()"
100-
/>
115+
/>
101116
<button
102117
class="fr-icon-arrow-left-s-line-double"
103118
@click="goPreviousPage()"

0 commit comments

Comments
 (0)