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.
1
let data = [
2
{ product: "apples", order: "PRN001", quantity: 10, price: 5.5 },
3
{ product: "oranges", order: "PRN002", quantity: 15, price: 6 },
4
{ product: "bananas", order: "PRN002", quantity: 20, price: 7.5 }
5
];
6
7
let table = new wire.data.DataTable(data);
8
9
table = table.select("product", "price").table();
Copied!

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.
1
...
2
3
table = table.select().column("quantity").as("number_of_skus").table();
Copied!

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.
1
...
2
3
table = table.select().column("product").title("Product Title").table();
Copied!
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.
1
...
2
3
table = table.select().column("quantity").as("numberOfSkus").table();
Copied!

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.
1
...
2
3
table = table.select("product").column("price").format("C1").table();
Copied!

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.
1
...
2
3
table = table.select().column("date_field").format((row) => {
4
return moment(row["date_field"]).format("MM-DD-YYY");
5
}).table();
Copied!

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.
1
...
2
3
table = table.select()
4
.sum("quantity").format("N1")
5
.avg("price").as("avg_price").format("C1")
6
.table();
Copied!
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.
1
...
2
3
table = table.select("order").sum("quantity").table();
Copied!
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.
1
...
2
3
table = table.select()
4
.column("more_quantity")
5
.sum().calc((row, index, rows) => {
6
return row["quantity"] * 10;
7
}) .table();
Copied!