Columns
The columns in a WireBootstrap data table contain meta data about the object field names in the data tables array. A data table contains methods for maintaining these columns. Columns can be accessed directly via the Columns
property on a data table.
Add Columns
Use the addColumn
method on a data table to add a new column.
In the example below, a new column named owner
is added and its value is set to Amy Peters
for all rows.
Calculated Columns
The example above sets a static column value for all rows in the data table.
To add calculated columns use the calc
function with a delegate. In the example below, the new column moreQuantity
is set to 10% more than quantity
.
Index
Use the index method to add a new column that contains the index position for each row.
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 productOwner
column is not supplied. In the new table, the title will automatically be Product Owner
.
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 following example formats data in the quantity
column to a number with two decimal places using format
N2
.
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.
HTML Columns
Use the type method to specify that the column contains html markup.
The following example formats an html button column that calls a JavaScript function called detail
and passes in the product
in that row.
Maintenance
Use the getColumn
method to get an existing column from a data table.
The following example gets the order
column from a data table and sets it's Title
property.
Use the removeColumn
method to remove the column from the data table. This operation will remove the column from the Columns
collection and all the data for that column in each row.
Use the renameColumn
method to change the name of a column in a data table. This operation will change the name of the column in the Columns
collection and also the same property name on each object in every row in the data array.
The following example changes the name of the column order
to orderNumber
.
For more on adding and maintaining columns in a data table, visit the DataTables reference page.
Last updated