Filter and Sort
A WireBootstrap DataTable contains methods that allow its data to be filtered.

Basic Filters

In the example below, the row containing the user pdougherty is pulled out of the data table. The first method at the end of the filter chain evaluates the filter and returns the first row that meets the filter's criteria.
1
let users = [
2
{ UserName: "jkratz", FullName: "Jamie Kratz", Active: true },
3
{ UserName: "pdougherty", FullName: "Pat Dougherty" Active: true },
4
{ UserName: "mchermela", FullName: "Mike Chermela", Active: false }
5
];
6
7
let userTable = new wire.data.DataTable(rows);
8
9
const pat = userTable.where().eq("UserName", "pdougherty").first();
Copied!
In the example below, all user rows are pulled except for the one containing the user pdougherty. This is done using the ne method instead of the eq method used in the previous example. The rows method at the end of the filter chain evaluates the filter and returns all rows that meets the filter's criteria.
1
...
2
3
const users = userTable.where().ne("UserName", "pdougherty").rows();
Copied!
Filters can be combined in a data table. The filter operations can also be broken at any point in the method chain to build conditional filters.
In the example below, users are filtered to those that are active and further filtered to jkratz but only if the variable jamieOnly is true.
1
...
2
3
userTable.where().eq("Actve", true);
4
5
if(jamieOnly)
6
userTable.eq("UserName", "jkratz");
7
8
const users = userTable.rows();
Copied!
Note, to clear any any previous filters, use the where method. This method starts a filter chain over again.

Custom Filters

Custom filters are supported on data tables using the calc function. Use this boolean method with a delegate to determine whether a given row should be included in the filter.
In the example below, the filter returns users that are active or not mchermela.
1
...
2
3
const users = userTable.where().calc((user) => {
4
return user.Active || user.UserName != "mchermela";
5
}).rows();
Copied!

Group By

Group by is implicit. Adding columns to a select automatically groups any aggregates by those columns.
The example below totals Sales by Product.
1
const sales = [
2
{ Product: "apples", Sales: 11 },
3
{ Product: "apples", Sales: 15 },
4
{ Product: "oranges", Sales: 44 }
5
];
6
7
const salesTable = new wire.data.DataTable(sales);
8
9
const salesAgg = salesTable.select("Product").sum("Sales").rows();
10
11
/*
12
salesAgg - >
13
14
[
15
{Product: "apples", Sales: 26 }.
16
{Product: "oranges", Sales: 44 }
17
]
18
19
/*
Copied!

Order By

Ordering and returning top N rows can be achieved as illustrated below.
The following example filters to active users and orders the rows by FullName.
1
...
2
3
const users = userTable.where().eq("Active", true)
4
.orderBy("FullName")
5
.rows();
Copied!
Sorting using orderBy defaults to ascending order. To sort descending, set the second parameter to true.
1
...
2
3
const users = userTable.where().eq("Active", true)
4
.orderBy("FullName", true)
5
.rows();
Copied!
To filter the top N rows, use the top method.
1
...
2
3
const users = userTable
4
.top(2)
5
.where().eq("Active", true)
6
.orderBy("FullName")
7
.rows();
Copied!
For more on data table filters, visit the DataTables reference page.