The DataTable is a control which allows you to add dynamic table to your forms.
You can set up how many columns the table has and their type, and the users will be able to add entries to this table.
Most configuration for DataTable can be done by editing individual column settings. To add a new column, simply click on the plus symbol:
This page contains a detailed description of the control properties and JavaScript samples, which you can use with this control.
Here you can find properties specifically related to the DataTable control.
A unique identifier for the control.
JavaScript
The Name property allows to work with the control via JavaScript code, like this:
fd.spRendered(function(){
//can access the control using its Name,
fd.control('Control1').value = [{Product: "White T-shirt", Amount: 3, ExpectedDate: new Date(), Delivered: false}];
});
Allows to select where the new line will be added — at the Top or at the Bottom of the table.
The property defines the width of the control in pixels.
If left blank, the control takes up the entire available width in the current grid cell.
Select Multiline Plain Text field in the current SharePoint List to save Data Table data to. It will automatically render control in List View.
Alternatively create a new hidden field in editor. You can delete hidden fields by selecting “🖉 Manage” option in the dropdown.
The property contains a JSON object that defines how the data is displayed in the list view. You can change the order of the columns, remove columns from the Lis View, or change their titles.
Here you can find properties specifically related to the DataTable columns.
A unique identifier for the column.
Important
Do not change a column’s name after the form has been in use, or you might lose saved data or break your automation.
Here you can find properties specifically related to the Number type columns.
Only available for the Number column type.
Specify the minimum and maximum values that a user can enter:
When a user enters a value outside the range, the value changes to the nearest valid value:
Only available for the Number column type.
Specify the number of decimal places to which the number will be rounded. The default property value is 0.
Only available for the Number column type.
Specify the number format that is applied when a user leaves the control. The default property value is n0.
The number format is made up of format specifier and the number of decimals places. E.g. n3, c2.
Avaliable format specifiers:
‘n’ — Renders a number.
‘c’ — Renders a currency value.
‘p’ — Renders a percentage (number is multiplied by 100).
‘e’ — Renders exponential values.
Here you can find properties specifically related to the Dropdown type columns.
Only available for the Dropdown column type.
Add a list of options to the Items property. Each option should be on a new line.
Defines whether a user is allowed to select a single option or multiple options from the drop-down list:
Single Selection allows a user to select only one option from the drop-down list:
Multiple Selection allows a user to select multiple options from a drop-down list:
Note
In single selection mode the value is stored as a string.
In multiple selection mode the value is stored as an array of strings.
In this section, you can find basic examples of how to work with the control using JavaScript.
If you are not familiar with the JavaScript framework, get started with the JavaScript basics.
Note
The control is only accessible once the form is rendered, so all calls to the control must be inside fd.spRendered event:
fd.spRendered(function(){
//hide the control
fd.control('Control1').hidden = true;
//show the control
fd.control('Control1').hidden = false;
});
Get or set the DataTable control value. The control value is stored as an array of JavaScript objects, with properties for each column.
fd.control('Control1').value; // returns an array // set value with array of objects: var records = [{Date: new Date(), Text: "Item A", Cost: 100 }, {Date: new Date(), Text: "Item B", Cost: 200 }]; fd.control('Control1').value = records; // add new record to the DataTable using columns' InternalNames: var record = {Date: new Date(), Text: "Item C", Cost: 300 }; fd.control('Control1').value.push(record);
Access HTML element inside the control in order to modify it, hide it, or do something else.
//access control's HTML var htmlControl = fd.control('Control1').$el;
Hide a control from a user. The control value can still be accessed and changed with JavaScript.
//hide control fd.control('Control1').hidden = true; //show control fd.control('Control1').hidden = false;
You can access the widget used by the control. The widget is based on Kendo UI Grid.
// get the widget fd.control('Control1').widget; // change the widget's configuration fd.control('Control1').widgetOptions = { sortable: false };
widgetOptions is the same as widget.setOptions({}) but can be defined before widget initialization.
Sometimes, you just need to ensure that at least one record is added to the DataTable.
This can be achieved with a simple validator:
//make at least one record required
fd.spRendered(function(){
fd.control('Control1').addValidator({
name: 'Control1 validator',
error: 'Enter at least one record into the table',
validate: function(value) {
//change this number to make more records required
if (value.length < 1) {
return false;
}
return true;
}
});
});
Note
You can adjust the number in the code to make more records required or add other conditions for a more complex validation.
To prepopulate DataTable control with rows of data, use the following code:
fd.spRendered(function() {
//select the DataTable control to prepopulate
var dt = fd.control('Control1');
//specify information for rows using columns' Name property
var dtRows = [
{Product: "Forms for SharePoint Online", Price: 599, Subscription: true, Date: new Date()},
{Product: "Charts for Office365", Price: 399, Subscription: false, Date: new Date()}
];
//assign rows to DataTable
dt.value = dtRows;
});
To prepopulate column values for each new row in a DataTable control, use the following code:
fd.spRendered(function() {
//select the DataTable control to automatically prepopulate new rows
var dt = fd.control('Control1');
dt.widget.bind('beforeEdit', function(e) {
var model = e.model;
if (model.isNew()) {
model.set('Name', 'John Smith');
model.set('Email', '[email protected]');
}
});
});
To populate dropdown column of a DataTable control dynamically, use the code:
fd.spRendered(function() {
fd.control('Control1').$on('edit', function(e) {
console.log(e)
if (e.column.field === "Column1") {
//pass widget + current column value
console.log(e.model);
populateColumn(e.widget, e.model.Column1);
}
})
});
function populateColumn(widget, value) {
widget.setDataSource({
data: ['Category A', 'Category B', 'Category C']
});
//set value if one was select
widget.value(value);
}
Sometimes, you might want to set fields automatically, without direct user input:
For example, the product prices are not something a user should be able to change. Instead, they can be set dynamically, depending on the selected product.
We’ll use a simple JS Object to store product prices, and automatically set unit price on product selection. Here is the code:
//we store prices in JS object
var merch = {};
//use bracket notation to accurately copy dropdown values
merch['Baseball cap'] = 9.99;
merch['T-shirt'] = 19.99;
merch['Key chain'] = 4.99;
fd.spRendered(function(){
//set unit price column (2nd column) to be non-editable, as we set it with code
fd.control('Control1').columns[1].editable = function(){return false};
fd.control('Control1').$on('change', function(value) {
var modifiedValue = null;
if(value) {
for (var i = 0; i < value.length; i++) {
if (value[i].UnitPrice !== merch[value[i].Product]) {
if (!modifiedValue) {
modifiedValue = Object.assign({}, value);
}
modifiedValue[i].UnitPrice = merch[value[i].Product];
}
}
}
if (modifiedValue) {
fd.control('Control1').value = value;
fd.control('Control1').widget.refresh();
}
});
});
Finally, we’re going to calculate total for a row, and for the whole DataTable.
Here is the code:
fd.spRendered(function() {
//Disable OrderTotal field
fd.field('OrderTotal').disabled = true;
//Make LineTotal column noneditable
fd.control('Control1').columns[3].editable = function(){return false};
fd.control('Control1').$on('change', function(value) {
//variable to count Order Total
var orderTotal = 0.0;
//if there are records in the table
var modifiedValue = null;
if(value){
//go through each one by one
for (var i = 0; i < value.length; i++){
//if this record has Amount and UnitPrice
if(value[i].Amount && value[i].UnitPrice){
//set LineTotal to their product
var cost = value[i].Amount * value[i].UnitPrice;
if (value[i].LineTotal !== cost) {
if (!modifiedValue) {
modifiedValue = Object.assign({}, value);
}
modifiedValue[i].LineTotal = cost;
}
}
//add Total to the Order Total
orderTotal += parseFloat(value[i].LineTotal);
console.log(orderTotal);
}
}
//here we refresh the table
if (modifiedValue) {
fd.control('Control1').value = value;
fd.control('Control1').widget.refresh();
}
//we set Order Total field to sum of Totals
fd.field('OrderTotal').value = orderTotal;
});
});
If you’re getting an incorrect value in one of your fields, for example, in OrderTotal, make sure that the format is correctly configured and an appropriate number of decimals is selected:
You can add a button to DataTable rows, which will allow you to duplicate them, like this:
Use the following code:
fd.spRendered(function() {
//select the DataTable control to add new column to
var dt = fd.control('Control1');
var columns = dt.widget.options.columns;
var customRowDataItem = null;
var isCustomAdd = false;
//specify what the column will be like
columns.push({
command: {
text: "Copy row",
iconClass:"k-icon k-i-copy",
click: function(e) {
e.preventDefault();
customRowDataItem = this.dataItem($(e.currentTarget).closest("tr"));
isCustomAdd = true;
this.addRow();
}
}
});
dt.widget.setOptions({
columns: columns
});
dt.widget.bind('edit', function(e) {
if (isCustomAdd && e.model.isNew()) {
isCustomAdd = false;
for (var i = 0; i < columns.length; i++) {
var field = columns[i].field;
if (field) {
e.model.set(field, customRowDataItem[field]);
}
}
e.sender.closeCell(e.container);
}
});
});