Select and Calculate
A WireBootstrap DataTable allows rows of data to be selected, aggregated, and transformed.
Select
The following example selects two columns, product
and price
, from the data table and creates a new data table using the table
function at the end of the method chain.
Format
Columns have a number of attributes that can be set in the select
method chain. In order to set these attributes, reference an individual column using the column
method and then use any number of the column attribute methods to set it's values.
Use the as
function to change the name of a field. In the example below, the field quantity
will be named number_of_skus
in the new table.
Title
Use the title
function to give any column a descriptive title. This will be set into the Title
property for the column in the resulting table.
A data table needs a descriptive Title
value for any column. If one is not supplied, it will automatically create one. In doing so, it will parse camelCase or PascalCase field names and intelligently capitalize and space accordingly to come up with a friendly title.
In the example below, a title for the numberOfSkus
column is not supplied. In the new table, the title will automatically be Number Of Skus
.
Number Formatting
To format numeric columns, use the format method with a format specification. Valid values are C[N], P[N], N[N] where [N] is the number of decimal places.
The example below sets the formatting for the price
column to currency one decimal place using the C1
parameter in the format
function. This value will be set into the Format
property for the column in the resulting table.
Custom Formatting
The format method can also accept a custom function in order to format data in the column.
The following example formats a date field called date_field
using the third party library moment.js.
Calculated Columns
Data tables allow calculated columns to be created on the fly.
The following creates two columns in a new data table that are the aggregates of columns in an existing data table. In the new table, quantity
will be aggregated using sum
. It will have the same name and be formatted to numeric one decimal place using format
N1
. The price
column will be averaged using avg
and it's name changed to avg_price
. It will be formatted to currency one decimal place using format
C1
.
Aggregates can also be used with implicit grouping by selecting other columns to be included in the result. In the example below, the order
column is included in the new data table, so the calculated column quantity
is broken out by order
.
Calculated columns can also be created using custom formulas using the calc
method.
In the example below, a new column, more_quantity
, is calculated as the sum
of the quantity
column times 10
and returned in a new table. This is done using a custom function and a delegate row
. The function will also have the index position and all rows in the data table available as it is being evaluated.
Last updated