Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(paginator): add initialPage prop for page initialization #7079

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion apps/showcase/doc/common/apidoc/index.json
Original file line number Diff line number Diff line change
Expand Up @@ -46804,6 +46804,14 @@
"default": "true",
"description": "Whether to show the paginator even there is only one page."
},
{
"name": "initialPage",
"optional": true,
"readonly": false,
"type": "number",
"default": "1",
"description": "Number of the initial page to display"
},
{
"name": "dt",
"optional": true,
Expand Down Expand Up @@ -100576,4 +100584,4 @@
}
}
}
}
}
4 changes: 4 additions & 0 deletions packages/primevue/src/paginator/BasePaginator.vue
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ export default {
alwaysShow: {
type: Boolean,
default: true
},
initialPage: {
type: Number,
default: 0
}
},
style: PaginatorStyle,
Expand Down
5 changes: 5 additions & 0 deletions packages/primevue/src/paginator/Paginator.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,11 @@ export interface PaginatorProps {
* @defaultValue true
*/
alwaysShow?: boolean | undefined;
/**
* Number of the initial page to display.
* @defaultValue 1
*/
initialPage?: number | undefined;
/**
* It generates scoped CSS variables using design tokens for the component.
*/
Expand Down
28 changes: 28 additions & 0 deletions packages/primevue/src/paginator/Paginator.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,32 @@ describe('Paginator.vue', () => {

expect(wrapper.find('.p-select-label').text()).toBe('20');
});

it('should initialize on the correct page when initialPage is set to a value greater than 1', async () => {
await wrapper.setProps({ initialPage: 2 });

expect(wrapper.vm.page).toBe(1);
expect(wrapper.findAll('.p-paginator-page')[1].classes()).toContain('p-paginator-page-selected');
});

it('should initialize on page 0 when initialPage is 0', async () => {
await wrapper.setProps({ initialPage: 0 });

expect(wrapper.vm.page).toBe(0);
expect(wrapper.findAll('.p-paginator-page')[0].classes()).toContain('p-paginator-page-selected');
});

it('should initialize on page 0 when initialPage is 1', async () => {
await wrapper.setProps({ initialPage: 1 });

expect(wrapper.vm.page).toBe(0);
expect(wrapper.findAll('.p-paginator-page')[0].classes()).toContain('p-paginator-page-selected');
});

it('should not change page when initialPage is negative', async () => {
await wrapper.setProps({ initialPage: -1 });

expect(wrapper.vm.page).toBe(0);
expect(wrapper.findAll('.p-paginator-page')[0].classes()).toContain('p-paginator-page-selected');
});
});
10 changes: 9 additions & 1 deletion packages/primevue/src/paginator/Paginator.vue
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,15 @@ export default {
if (this.page > 0 && newValue && this.d_first >= newValue) {
this.changePage(this.pageCount - 1);
}
}
},
initialPage(newPage) {
const pageIndex = newPage - 1;

if (pageIndex >= 0 && pageIndex < this.pageCount && pageIndex !== this.page) {
this.d_first = pageIndex * this.d_rows;
this.changePage(pageIndex);
}
},
},
mounted() {
this.createStyle();
Expand Down
Loading