Skip to content

Commit ec0f752

Browse files
authored
Merge pull request #384 from breakone/before-events
add "before" events
2 parents 7d867b3 + fe7ed82 commit ec0f752

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

docs/documentation/Events.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,17 @@ dataTable.on('datatable.page', function(page) {
2222
});
2323
```
2424

25+
### `datatable.page:before`
26+
Fires before page change.
27+
28+
A single argument is available which returns the page number:
29+
30+
```javascript
31+
dataTable.on('datatable.page:before', function(page) {
32+
//
33+
});
34+
```
35+
2536
### `datatable.perpage`
2637
Fires when the perPage option is changed with the dropdown. A single argument returns the per-page value:
2738

@@ -31,9 +42,21 @@ dataTable.on('datatable.perpage', function(perpage) {
3142
});
3243
```
3344

45+
### `datatable.perpage:before`
46+
Fires before the perPage option is changed with the dropdown. A single argument returns the per-page value:
47+
48+
```javascript
49+
dataTable.on('datatable.perpage:before', function(perpage) {
50+
//
51+
});
52+
```
53+
3454
### `datatable.refresh`
3555
Fires when the `.refresh()` method is called.
3656

57+
### `datatable.refresh:before`
58+
Fires before the `.refresh()` method is called.
59+
3760
### `datatable.search`
3861
Fires on keyup during a search.
3962

@@ -45,6 +68,17 @@ dataTable.on('datatable.search', function(query, matched) {
4568
});
4669
```
4770

71+
### `datatable.search:before`
72+
Fires on keyup before a search.
73+
74+
Two arguments are available: `query` which returns the query string entered and `matched` which returns an array of rows containing the matched string:
75+
76+
```javascript
77+
dataTable.on('datatable.search:before', function(query, matched) {
78+
//
79+
});
80+
```
81+
4882
### `datatable.selectrow`
4983
Fires when user selects a row - either by mouse click on a row or using `Space`/`Enter` during keyboard based navigation (requires option [[rowNavigation]]).
5084

@@ -72,3 +106,6 @@ dataTable.on('datatable.sort', function(column, direction) {
72106

73107
### `datatable.update`
74108
Fires when the `.update()` method is called.
109+
110+
### `datatable.update:before`
111+
Fires before the `.update()` method is called.

src/datatable.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,7 @@ export class DataTable {
499499
if (selector && selector instanceof HTMLSelectElement) {
500500
// Change per page
501501
selector.addEventListener("change", () => {
502+
this.emit("datatable.perpage:before", this.options.perPage)
502503
this.options.perPage = parseInt(selector.value, 10)
503504
this.update()
504505

@@ -698,6 +699,8 @@ export class DataTable {
698699
* @return {Void}
699700
*/
700701
update(measureWidths = false) {
702+
this.emit("datatable.update:before")
703+
701704
if (measureWidths) {
702705
this.columns._measureWidths()
703706
this.hasRows = Boolean(this.data.data.length)
@@ -778,6 +781,7 @@ export class DataTable {
778781
* Perform a simple search of the data set
779782
*/
780783
search(term: string, columns: (number[] | undefined ) = undefined) {
784+
this.emit("datatable.search:before", term, this._searchData)
781785

782786
if (!term.length) {
783787
this._currentPage = 1
@@ -813,6 +817,8 @@ export class DataTable {
813817
})).filter(query => query.terms.length
814818
)
815819

820+
this.emit("datatable.multisearch:before", queries, this._searchData)
821+
816822
this._searchQueries = queries
817823

818824
if (!queries.length) {
@@ -891,6 +897,8 @@ export class DataTable {
891897
* Change page
892898
*/
893899
page(page: number, lastRowCursor = false) {
900+
this.emit("datatable.page:before", page)
901+
894902
// We don't want to load the current page again.
895903
if (page === this._currentPage) {
896904
return false
@@ -973,6 +981,8 @@ export class DataTable {
973981
* Refresh the instance
974982
*/
975983
refresh() {
984+
this.emit("datatable.refresh:before")
985+
976986
if (this.options.searchable) {
977987
const inputSelector = classNamesToSelector(this.options.classes.input)
978988
const inputs: HTMLInputElement[] = Array.from(this.wrapperDOM.querySelectorAll(inputSelector))

0 commit comments

Comments
 (0)