Skip to content
This repository was archived by the owner on Aug 7, 2020. It is now read-only.

Commit b094289

Browse files
marie-jAxelPeter
authored andcommitted
feat(oui-datagrid): add page change event (#389)
Emit an event instead of directly scrolling the view as it is not the component concerns
1 parent 9553202 commit b094289

File tree

5 files changed

+46
-19
lines changed

5 files changed

+46
-19
lines changed

packages/oui-datagrid/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,17 @@ Or you can use the `page-size` property. It takes precedence over value configur
281281
</oui-datagrid>
282282
```
283283

284+
You can also add custom behaviour on pagination changes
285+
286+
```html:preview
287+
<p>Page size : {{ $ctrl.pageSize }}</p>
288+
<p>Offset : {{ $ctrl.offset }}</p>
289+
<oui-datagrid rows="$ctrl.data" on-page-change="$ctrl.onPageChange($pagination)" page-size="5">
290+
<oui-column title="'firstName'" property="firstName"></oui-column>
291+
<oui-column title="$ctrl.lastNameText" property="lastName"></oui-column>
292+
</oui-datagrid>
293+
```
294+
284295
### Custom cell templates
285296

286297
```html
@@ -718,6 +729,7 @@ call `rows-loader` and then a `row-loader` call for each line.
718729
| `columns-parameters` | array | <? | no | n/a | `undefined` | columns parameters (see below)
719730
| `on-columns-parameters-change` | function | & | no | n/a | n/a | triggered on column parameter change when datagrid is customizable
720731
| `on-row-select` | function | & | no | n/a | n/a | triggered when a row is selected
732+
| `on-page-change` | function | & | no | n/a | n/a | triggered when pagination is changed
721733

722734
`columns-parameters` is an array describing all basic parameters of each column.
723735

packages/oui-datagrid/src/datagrid.controller.js

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -246,25 +246,23 @@ export default class DatagridController {
246246
this.refreshData(() => {
247247
this.paging.setOffset(1);
248248
this.paging.setCriteria(criteria);
249-
}, false, false);
249+
}, false);
250250
}
251251

252-
onPaginationChange ($event) {
252+
onPaginationChange ({ offset, pageSize }) {
253253
this.refreshData(() => {
254-
this.paging.setOffset($event.offset);
255-
this.paging.setPageSize($event.pageSize);
256-
}, true, true);
257-
}
258-
259-
scrollToTop () {
260-
// Small delay otherwise rows could not be rendered
261-
// yet and position would be wrong
262-
this.$timeout(() => {
263-
this.$element[0].scrollIntoView(true);
264-
});
254+
this.paging.setOffset(offset);
255+
this.paging.setPageSize(pageSize);
256+
this.onPageChange({
257+
$pagination: {
258+
offset,
259+
pageSize
260+
}
261+
});
262+
}, true);
265263
}
266264

267-
refreshData (callback, skipSortAndFilter, requireScrollToTop, hideLoader, forceLoadRows) {
265+
refreshData (callback, skipSortAndFilter, hideLoader, forceLoadRows) {
268266
if (this.loading) {
269267
return this.$q.when();
270268
}
@@ -281,13 +279,11 @@ export default class DatagridController {
281279
.then(() => this.paging.loadData(skipSortAndFilter, forceLoadRows))
282280
.then(result => {
283281
this.displayedRows = result.data;
284-
if (requireScrollToTop) {
285-
this.scrollToTop();
286-
}
287282
if (this.hasActionMenu) {
288283
setTimeout(() => this.checkScroll(), checkScrollOnRefreshDataDelay);
289284
}
290285
})
286+
.catch(error => console.log(error))
291287
.finally(() => {
292288
this.loading = false;
293289
this.firstLoading = false;

packages/oui-datagrid/src/datagrid.directive.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ export default () => {
1818
rowLoader: "&?",
1919
emptyPlaceholder: "@?",
2020
onColumnsParametersChange: "&",
21-
onRowSelect: "&"
21+
onRowSelect: "&",
22+
onPageChange: "&"
2223
},
2324
compile: elm => {
2425
// Transclude can't be used here otherwise transcluded

packages/oui-datagrid/src/datagrid.service.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export default class DatagridService {
2828
const datagridController = this.datagrids[datagridId];
2929

3030
if (datagridController) {
31-
datagridController.refreshData(false, false, false, !showSpinner, true);
31+
datagridController.refreshData(false, false, !showSpinner, true);
3232
}
3333
}
3434
}

packages/oui-datagrid/src/index.spec.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1270,6 +1270,24 @@ describe("ouiDatagrid", () => {
12701270
expect(getCell($secondRow, 1).children().html()).toBe(fakeData[3].lastName);
12711271
});
12721272

1273+
it("should execute callback when pagination changes", () => {
1274+
const onPageChangeSpy = jasmine.createSpy("onPaginationChangeSpy");
1275+
const element = TestUtils.compileTemplate(`
1276+
<oui-datagrid rows="$ctrl.rows" page-size="2" on-page-change="$ctrl.onPageChange($pagination)">
1277+
<oui-column property="firstName"></oui-column>
1278+
<oui-column property="lastName"></oui-column>
1279+
</oui-datagrid>
1280+
`, {
1281+
rows: fakeData.slice(0, 5),
1282+
onPageChange: onPageChangeSpy
1283+
}
1284+
);
1285+
1286+
getNextPagePaginationButton(element).triggerHandler("click");
1287+
1288+
expect(onPageChangeSpy).toHaveBeenCalled();
1289+
});
1290+
12731291
it("should support action-menu as column", () => {
12741292
const element = TestUtils.compileTemplate(`
12751293
<oui-datagrid rows="$ctrl.rows">

0 commit comments

Comments
 (0)