DataTables can be filtered using methods on the DataTable class. The following example filters a DataTable to rows where product is equal to apples or oranges and then just apples.


 
var data = [ 
    {product: "apples", order: "PRN001", quantity: 10},
    {product: "oranges", order: "PRN002", quantity: 15},
    {product: "apples", order: "PRN003", quantity: 20}
    
];
 
var table = new eb.data.DataTable(data);
 
// create a new table from the existing one applying a filter for apple or orange products
table = table.where().eq("product", ["apples", "oranges"]).table();
 
...
 
// create a new table from the previous one applying a filter for just apple products
table = table.where().eq("product", "apples").table();
 
...
 
// create a new table from the previous one applying a filter that excludes apple products
table = table.where().ne("product", "apples").table();
 
...
 
// create a new table from the previous one applying a filter for products that contain the letter "a"
table = table.where().contains("product", "a").table();


Below is the same table filtered to products not equal to apples.


// create a new table from the existing one applying a filter for non apple products
table = table.where().ne("product", "apples").table();


In the example above, a new DataTable is created from the filter. Filter results can be returned in other formats as illustrated in the following examples.



// return an array of objects
table.where().eq("product", "apples").rows();
 
...
 
// return the first row that meets the filter criteria; return null if no rows meet the filter criteria
table.where().eq("product", "apples").first();
 
...
 
// return an empty object instead of null if no rows meet the filter criteria
table.where().eq("product", "bananas").firstOrDefault();


Custom Filters


Custom filters are supported using the calc function. This method holds custom JavaScript that can operate on a local user-defined parameter that represents each row of data in an implicit loop as the expression is evaluated. It uses a Boolean evaluation looking for a return value of true for rows to be included in the filter.



// return table rows where quantity is greater then 10.
table.where().calc(function(row) {
    return row["quantity"] > 10
}).rows();


Use the nonEmpty and distinct clauses with or without the where clause.


// remove duplicate rows
table.where().eq("product", "apples").distinct().rows();
    
...
  
// remove rows with all null values
table.where().eq("product", "apples").nonEmpty().rows();



Order By / Top N


Ordering and returning top N rows can be achieved as illustrated below.


// order rows by product
table.where().eq("product", "apples").orderBy("product").rows();
    
...
 
// order rows by product descending
table.where().eq("product", "apples").orderBy("product", true).rows();
 
...
  
// return the first 2 rows sorted by product
table.where().orderBy("product").top(2).rows();