Skip to content

Commit 81b25a9

Browse files
Yann Lojewskiy4nnL
authored andcommitted
feat(datagrid): add pagination-mode option
Signed-off-by: Yann Lojewski <[email protected]>
1 parent cdb47be commit 81b25a9

File tree

11 files changed

+136
-18
lines changed

11 files changed

+136
-18
lines changed

packages/apps/workshop/stories/design-system/components/datagrid/datagrid-webcomponent.stories.js

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import Datagrid from '@ovh-ux/ui-kit.datagrid';
77

88
import { action } from '@storybook/addon-actions';
99
import { forModule } from 'storybook-addon-angularjs';
10+
import { select } from '@storybook/addon-knobs';
1011

1112
import readme from '@ovh-ux/ui-kit.datagrid/README.md';
1213
import { compileTemplate } from '../../../../src/utils';
@@ -459,4 +460,36 @@ export const RemoteData = forModule(moduleName).createElement(
459460
),
460461
);
461462

462-
RemoteData.storyName = 'Remote data';
463+
const paginationMode = {
464+
label: 'Mode',
465+
options: {
466+
Button: 'button',
467+
Select: 'select',
468+
Input: 'input',
469+
Arrows: 'arrows',
470+
},
471+
};
472+
paginationMode.default = paginationMode.options.Arrows;
473+
474+
475+
export const PaginationMode = forModule(moduleName).createElement(
476+
() => compileTemplate(
477+
`
478+
<oui-datagrid
479+
page-size="5"
480+
rows="$ctrl.data"
481+
pagination-mode="${select(paginationMode.label, paginationMode.options, paginationMode.default)}">
482+
<oui-datagrid-column title="'First name'" property="firstName"></oui-datagrid-column>
483+
<oui-datagrid-column title="'Last name'" property="lastName"></oui-datagrid-column>
484+
<oui-datagrid-column title="'Email'" property="email"></oui-datagrid-column>
485+
<oui-datagrid-column title="'Phone'" property="phone"></oui-datagrid-column>
486+
</oui-datagrid>`,
487+
{
488+
$ctrl: {
489+
data: data.slice(0, 30),
490+
},
491+
},
492+
),
493+
);
494+
495+
PaginationMode.storyName = 'Pagination Mode';

packages/components/datagrid/README.md

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,21 @@ angular.module('myModule', ['oui.datagrid'])
2525

2626
## Component `oui-datagrid`
2727

28-
| Attribute | Type | Binding | One-time binding | Values | Default | Description
29-
| ---- | ---- | ---- | ---- | ---- | ---- | ----
30-
| `id` | string | @? | no | n/a | n/a | id of the datagrid
31-
| `page-size` | number | @? | no | n/a | `25` | maximum number of rows to show on each pages
32-
`page-size-max` | number | @? | no | n/a | n/a | max page size of the page sizes list
33-
| `page` | number | @? | no | n/a | `1` | page to display
34-
| `rows` | array | <? | yes | n/a | n/a | local rows to load in the datagrid
35-
| `criteria` | array | <? | yes | n/a | n/a | default filter criteria to apply
36-
| `empty-placeholder` | string | @? | yes | n/a | n/a | custom placeholder text when there is no data
37-
| `selectable-rows` | boolean | <? | no | `true`, `false` | `false` | allow rows to be selected. Create a sticky column a the start of the datagrid.
38-
| `on-criteria-change` | function | & | no | n/a | n/a | triggered when criteria changed. Use `$criteria` in your callback to get the result
39-
| `on-page-change` | function | & | no | n/a | n/a | triggered when pagination is changed
40-
| `on-row-select` | function | & | no | n/a | n/a | triggered when a row is selected
41-
| `on-sort-change` | function | & | no | n/a | n/a | triggered when sort is triggered. Use `$sort` in your callback to get the result
28+
| Attribute | Type | Binding | One-time binding | Values | Default | Description
29+
| ---- | ---- | ---- | ---- | ---- | ---- | ----
30+
| `id` | string | @? | no | n/a | n/a | id of the datagrid
31+
| `page-size` | number | @? | no | n/a | `25` | maximum number of rows to show on each pages
32+
`page-size-max` | number | @? | no | n/a | n/a | max page size of the page sizes list
33+
| `page` | number | @? | no | n/a | `1` | page to display
34+
| `rows` | array | <? | yes | n/a | n/a | local rows to load in the datagrid
35+
| `criteria` | array | <? | yes | n/a | n/a | default filter criteria to apply
36+
| `empty-placeholder` | string | @? | yes | n/a | n/a | custom placeholder text when there is no data
37+
| `selectable-rows` | boolean | <? | no | `true`, `false` | `false` | allow rows to be selected. Create a sticky column a the start of the datagrid.
38+
| `on-criteria-change` | function | & | no | n/a | n/a | triggered when criteria changed. Use `$criteria` in your callback to get the result
39+
| `on-page-change` | function | & | no | n/a | n/a | triggered when pagination is changed
40+
| `on-row-select` | function | & | no | n/a | n/a | triggered when a row is selected
41+
| `on-sort-change` | function | & | no | n/a | n/a | triggered when sort is triggered. Use `$sort` in your callback to get the result
42+
| `pagination-mode` | string | @? | no | `button`, `select`, `input`, `arrows` | `input` | Set the pagination mode
4243

4344
### Custom cell templates
4445

packages/components/datagrid/src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,5 @@ angular
3737
.service('ouiDatagridService', DatagridService)
3838
.component('ouiDatagridParameters', DatagridParameters);
3939

40+
export { PAGINATION_MODES } from './js/datagrid.controller';
4041
export default moduleName;

packages/components/datagrid/src/js/datagrid.controller.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { addBooleanParameter, addDefaultParameter } from '@ovh-ux/ui-kit.core/src/js/component-utils';
2+
import { MODES as PAGINATION_MODES } from '@ovh-ux/ui-kit.pagination';
23
import find from 'lodash/find';
34
import hasIn from 'lodash/hasIn';
45

@@ -11,6 +12,8 @@ const iconSortable = 'oui-icon-sort-inactive';
1112
const iconSortableAsc = 'oui-icon-sort-up';
1213
const iconSortableDesc = 'oui-icon-sort-down';
1314

15+
export { PAGINATION_MODES };
16+
1417
export default class DatagridController {
1518
constructor($attrs, $compile, $element, $transclude, $q, $scope, $window, $timeout,
1619
ouiDatagridPaging, ouiDatagridColumnBuilder, ouiDatagridConfiguration,
@@ -48,6 +51,7 @@ export default class DatagridController {
4851
this.criteria = this.criteria || [];
4952
this.page = parseInt(this.page, 10) || 1;
5053
this.offset = (this.page - 1) * this.pageSize + 1;
54+
this.paginationMode = (PAGINATION_MODES.includes(this.paginationMode) && this.paginationMode) || '';
5155

5256
addBooleanParameter(this, 'selectableRows');
5357
addDefaultParameter(this, 'emptyPlaceholder', this.config.translations.emptyPlaceholder);
@@ -88,6 +92,7 @@ export default class DatagridController {
8892
this.pageSizeMax,
8993
this.rowLoader,
9094
this.rowsLoader,
95+
this.paginationMode,
9196
);
9297
this.refreshData(() => {
9398
this.paging.setOffset(this.offset);
@@ -103,6 +108,7 @@ export default class DatagridController {
103108
this.pageSizeMax,
104109
this.rowLoader,
105110
this.rows,
111+
this.paginationMode,
106112
);
107113

108114
if (this.rows) {
@@ -125,6 +131,17 @@ export default class DatagridController {
125131
if (changes.columnsParameters && !changes.columnsParameters.isFirstChange()) {
126132
this.buildColumns();
127133
}
134+
135+
if (changes.paginationMode && !changes.paginationMode.isFirstChange()) {
136+
const { currentValue: paginationMode } = changes.paginationMode;
137+
138+
if (!PAGINATION_MODES.includes(paginationMode)) {
139+
return;
140+
}
141+
142+
this.paginationMode = paginationMode;
143+
this.paging.paginationMode = paginationMode;
144+
}
128145
}
129146

130147
$doCheck() {

packages/components/datagrid/src/js/datagrid.directive.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export default () => {
2525
onPageChange: '&',
2626
onRowSelect: '&',
2727
onSortChange: '&',
28+
paginationMode: '@?',
2829
},
2930
compile: (elm) => {
3031
// Transclude can't be used here otherwise transcluded

packages/components/datagrid/src/js/datagrid.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@
204204
page-size="$ctrl.paging.pageSize"
205205
page-size-max="$ctrl.paging.pageSizeMax"
206206
total-items="$ctrl.paging.totalCount"
207-
on-change="$ctrl.onPaginationChange($event)">
207+
on-change="$ctrl.onPaginationChange($event)"
208+
mode="{{ $ctrl.paging.paginationMode }}">
208209
</oui-pagination>
209210
</div>

packages/components/datagrid/src/js/datagrid.spec.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1+
import { last } from 'lodash';
2+
13
import '@ovh-ux/ui-kit.action-menu';
24

5+
import { PAGINATION_MODES } from './datagrid.controller';
36
import columnsData from './columns.spec.data.json';
47
import originalFakeData from './datagrid.spec.data.json';
58

9+
610
describe('ouiDatagrid', () => {
711
let TestUtils;
812
let $rootScope;
@@ -18,6 +22,7 @@ describe('ouiDatagrid', () => {
1822
const getFooterCell = (element, columnNumber) => angular.element(element[0].querySelectorAll('.oui-datagrid__footer')[columnNumber]);
1923
const getCell = (element, columnNumber) => angular.element(element[0].querySelectorAll('.oui-datagrid__cell')[columnNumber]);
2024
const getNextPagePaginationButton = (element) => angular.element(element[0].querySelector('.oui-pagination-nav__next'));
25+
const getPaginationNav = (element) => angular.element(element[0].querySelector('.oui-pagination-nav'));
2126
const isSortableCell = (element) => element.hasClass('oui-datagrid__header_sortable');
2227
const isSortableAscCell = (element) => {
2328
const isSortable = isSortableCell(element);
@@ -1395,6 +1400,33 @@ describe('ouiDatagrid', () => {
13951400
expect(onPageChangeSpy).toHaveBeenCalled();
13961401
});
13971402

1403+
it('should pass through the pagination mode', () => {
1404+
const element = TestUtils.compileTemplate(`
1405+
<oui-datagrid rows="$ctrl.rows" page-size="2" pagination-mode="{{ $ctrl.paginationMode }}">
1406+
<oui-column property="firstName"></oui-column>
1407+
</oui-datagrid>
1408+
`, {
1409+
rows: fakeData.slice(0, 5),
1410+
});
1411+
1412+
const setMode = (mode) => {
1413+
TestUtils.getElementController(element).paginationMode = mode;
1414+
element.scope().$apply();
1415+
};
1416+
const controller = element.controller('ouiDatagrid');
1417+
const paginationController = getPaginationNav(element).controller('ouiPagination');
1418+
1419+
PAGINATION_MODES.forEach((paginationMode) => {
1420+
setMode(paginationMode);
1421+
expect(controller.paging.paginationMode).toBe(paginationMode);
1422+
expect(paginationController.mode).toBe(paginationMode);
1423+
});
1424+
1425+
setMode('unknown');
1426+
expect(controller.paging.paginationMode).toBe(last(PAGINATION_MODES));
1427+
expect(paginationController.mode).toBe(last(PAGINATION_MODES));
1428+
});
1429+
13981430
it('should support action-menu as column', () => {
13991431
const element = TestUtils.compileTemplate(`
14001432
<oui-datagrid rows="$ctrl.rows">

packages/components/datagrid/src/js/paging/datagrid-local-paging.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export default class DatagridLocalPaging extends DatagridPagingAbstract {
1212
rowLoader,
1313
pagingService,
1414
rows,
15+
paginationMode,
1516
) {
1617
super(
1718
columns,
@@ -22,6 +23,7 @@ export default class DatagridLocalPaging extends DatagridPagingAbstract {
2223
pageSizeMax,
2324
rowLoader,
2425
pagingService,
26+
paginationMode,
2527
);
2628

2729
this.setRows(rows);

packages/components/datagrid/src/js/paging/datagrid-paging-abstract.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export default class DatagridPagingAbstract {
88
pageSizeMax,
99
rowLoader,
1010
pagingService,
11+
paginationMode,
1112
) {
1213
this.columns = columns;
1314
this.currentSorting = currentSorting;
@@ -16,6 +17,7 @@ export default class DatagridPagingAbstract {
1617
this.pageSizeMax = pageSizeMax;
1718
this.offset = offset;
1819
this.rowLoader = rowLoader;
20+
this.paginationMode = paginationMode;
1921

2022
this.$q = pagingService.$q;
2123
this.$timeout = pagingService.$timeout;

packages/components/datagrid/src/js/paging/datagrid-paging.service.js

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,17 @@ export default class {
1010
this.orderByFilter = orderByFilter;
1111
}
1212

13-
createLocal(columns, criteria, sorting, offset, pageSize, pageSizeMax, rowLoader, rows) {
13+
createLocal(
14+
columns,
15+
criteria,
16+
sorting,
17+
offset,
18+
pageSize,
19+
pageSizeMax,
20+
rowLoader,
21+
rows,
22+
paginationMode,
23+
) {
1424
return new DatagridLocalPaging(
1525
columns,
1626
criteria,
@@ -21,10 +31,21 @@ export default class {
2131
rowLoader,
2232
this,
2333
rows,
34+
paginationMode,
2435
);
2536
}
2637

27-
createRemote(columns, criteria, sorting, offset, pageSize, pageSizeMax, rowLoader, rowsLoader) {
38+
createRemote(
39+
columns,
40+
criteria,
41+
sorting,
42+
offset,
43+
pageSize,
44+
pageSizeMax,
45+
rowLoader,
46+
rowsLoader,
47+
paginationMode,
48+
) {
2849
return new DatagridRemotePaging(
2950
columns,
3051
criteria,
@@ -35,6 +56,7 @@ export default class {
3556
rowLoader,
3657
this,
3758
rowsLoader,
59+
paginationMode,
3860
);
3961
}
4062
}

0 commit comments

Comments
 (0)