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

add "before" events #384

Merged
merged 1 commit into from
Aug 21, 2024
Merged
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
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