Skip to content

Commit

Permalink
add "before" events
Browse files Browse the repository at this point in the history
  • Loading branch information
breakone committed Jun 3, 2024
1 parent fd8986f commit fe7ed82
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
37 changes: 37 additions & 0 deletions docs/documentation/Events.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@ dataTable.on('datatable.page', function(page) {
});
```

### `datatable.page:before`
Fires before page change.

A single argument is available which returns the page number:

```javascript
dataTable.on('datatable.page:before', function(page) {
//
});
```

### `datatable.perpage`
Fires when the perPage option is changed with the dropdown. A single argument returns the per-page value:

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

### `datatable.perpage:before`
Fires before the perPage option is changed with the dropdown. A single argument returns the per-page value:

```javascript
dataTable.on('datatable.perpage:before', function(perpage) {
//
});
```

### `datatable.refresh`
Fires when the `.refresh()` method is called.

### `datatable.refresh:before`
Fires before the `.refresh()` method is called.

### `datatable.search`
Fires on keyup during a search.

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

### `datatable.search:before`
Fires on keyup before a search.

Two arguments are available: `query` which returns the query string entered and `matched` which returns an array of rows containing the matched string:

```javascript
dataTable.on('datatable.search:before', function(query, matched) {
//
});
```

### `datatable.selectrow`
Fires when user selects a row - either by mouse click on a row or using `Space`/`Enter` during keyboard based navigation (requires option [[rowNavigation]]).

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

### `datatable.update`
Fires when the `.update()` method is called.

### `datatable.update:before`
Fires before the `.update()` method is called.
10 changes: 10 additions & 0 deletions src/datatable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,7 @@ export class DataTable {
if (selector && selector instanceof HTMLSelectElement) {
// Change per page
selector.addEventListener("change", () => {
this.emit("datatable.perpage:before", this.options.perPage)
this.options.perPage = parseInt(selector.value, 10)
this.update()

Expand Down Expand Up @@ -696,6 +697,8 @@ export class DataTable {
* @return {Void}
*/
update(measureWidths = false) {
this.emit("datatable.update:before")

if (measureWidths) {
this.columns._measureWidths()
this.hasRows = Boolean(this.data.data.length)
Expand Down Expand Up @@ -776,6 +779,7 @@ export class DataTable {
* Perform a simple search of the data set
*/
search(term: string, columns: (number[] | undefined ) = undefined) {
this.emit("datatable.search:before", term, this._searchData)

if (!term.length) {
this._currentPage = 1
Expand Down Expand Up @@ -811,6 +815,8 @@ export class DataTable {
})).filter(query => query.terms.length
)

this.emit("datatable.multisearch:before", queries, this._searchData)

this._searchQueries = queries

if (!queries.length) {
Expand Down Expand Up @@ -889,6 +895,8 @@ export class DataTable {
* Change page
*/
page(page: number, lastRowCursor = false) {
this.emit("datatable.page:before", page)

// We don't want to load the current page again.
if (page === this._currentPage) {
return false
Expand Down Expand Up @@ -971,6 +979,8 @@ export class DataTable {
* Refresh the instance
*/
refresh() {
this.emit("datatable.refresh:before")

if (this.options.searchable) {
const inputSelector = classNamesToSelector(this.options.classes.input)
const inputs: HTMLInputElement[] = Array.from(this.wrapperDOM.querySelectorAll(inputSelector))
Expand Down

0 comments on commit fe7ed82

Please sign in to comment.