DOM element return from renderer
DataTable's orthogonal data abilities can be extremely useful to allow ordering, search and
display data to be tweaked for each operation. More than that though, it can also be used to return a DOM element for the display
data type, which
will then be displayed in the DataTable (this is a new feature in DataTables 2).
This is important for integration with frameworks such as Vue and React which can expect to return DOM elements that they control (including event listeners).
The example shown here dynamically creates an svg
sparkline (using
this excellent library) and returns the svg
element from the rendering function.
Name | Symbol | Price | Difference | Last |
---|---|---|---|---|
Name | Symbol | Price | Difference | Last |
- Javascript
- HTML
- CSS
- Ajax
- Server-side script
- Comments
The Javascript shown below is used to initialise the table shown in this example:
var stock_data = [
{
name: 'ACME Gadgets',
symbol: 'AGDTS',
values: [ 42, 20, 30, 35, 9, 48, 12, 5]
},
{
name: 'Spry Media Productions',
symbol: 'SPMP',
values: [40, 37, 16, 43, 13, 18, 19]
},
{
name: 'Widget Emporium',
symbol: 'WDEMP',
values: [35, 40, 6, 15, 48, 16, 44, 49]
},
{
name: 'Sole Goodman',
symbol: 'SGMAN',
values: [25, 27, 4, 40, 7, 8, 18, 16]
},
{
name: 'Stanler Bits and Bobs',
symbol: 'SBIBO',
values: [13, 25, 13, 38, 40, 34, 40, 47]
}
];
var table = $('#example').DataTable({
data: stock_data,
columns: [
{ data: 'name' },
{ data: 'symbol' },
{
data: 'values',
render: function (data) {
return data[data.length - 1].toFixed(2);
}
},
{
data: 'values',
render: function (data, type) {
// Calculate the different between the last two values
var val = (
data[data.length - 1] -
data[data.length - 2]
).toFixed(2);
var colour = val < 0 ? 'red' : 'green';
return type === 'display'
? '<span style="color:' + colour + '">' + val + '</span>'
: val;
}
},
{
data: 'values',
render: function (data, type) {
if (type === 'display') {
// Create the SVG element the spark line will live in (needs the correct name space)
var svg = document.createElementNS("http://www.w3.org/2000/svg", "svg");
$(svg)
.addClass('sparkline')
.attr({
width: 250,
height: 17,
'stroke-width': 3
});
// Use the plug-in to create the lines
sparkline.default(svg, data);
return svg;
}
// Otherwise give the last value
return data[data.length-1];
}
}
]
});
var stock_data = [
{
name: 'ACME Gadgets',
symbol: 'AGDTS',
values: [ 42, 20, 30, 35, 9, 48, 12, 5]
},
{
name: 'Spry Media Productions',
symbol: 'SPMP',
values: [40, 37, 16, 43, 13, 18, 19]
},
{
name: 'Widget Emporium',
symbol: 'WDEMP',
values: [35, 40, 6, 15, 48, 16, 44, 49]
},
{
name: 'Sole Goodman',
symbol: 'SGMAN',
values: [25, 27, 4, 40, 7, 8, 18, 16]
},
{
name: 'Stanler Bits and Bobs',
symbol: 'SBIBO',
values: [13, 25, 13, 38, 40, 34, 40, 47]
}
];
let table = new DataTable('#example', {
data: stock_data,
columns: [
{ data: 'name' },
{ data: 'symbol' },
{
data: 'values',
render: data => data[data.length - 1].toFixed(2)
},
{
data: 'values',
render: function (data, type) {
// Calculate the different between the last two values
let val = (
data[data.length - 1] -
data[data.length - 2]
).toFixed(2);
let colour = val < 0 ? 'red' : 'green';
return type === 'display'
? '<span style="color:' + colour + '">' + val + '</span>'
: val;
}
},
{
data: 'values',
render: function (data, type) {
if (type === 'display') {
// Create the SVG element the spark line will live in (needs the correct name space)
let svg = document.createElementNS("http://www.w3.org/2000/svg", "svg");
svg.setAttribute('class', 'sparkline');
svg.setAttribute('width', 250);
svg.setAttribute('height', 17);
svg.setAttribute('stroke-width', 3);
// Use the plug-in to create the lines
sparkline.default(svg, data);
return svg;
}
// Otherwise give the last value
return data[data.length-1];
}
}
]
});
In addition to the above code, the following Javascript library files are loaded for use in this example:
The HTML shown below is the raw HTML table element, before it has been enhanced by DataTables:
This example uses a little bit of additional CSS beyond what is loaded from the library files (below), in order to correctly display the table. The additional CSS used is shown below:
.sparkline {
stroke: blue;
fill: none;
}
The following CSS library files are loaded for use in this example to provide the styling of the table:
This table loads data by Ajax. The latest data that has been loaded is shown below. This data will update automatically as any additional data is loaded.
The script used to perform the server-side processing for this table is shown below. Please note that this is just an example script using PHP. Server-side processing scripts can be written in any language, using the protocol described in the DataTables documentation.
Other examples
Basic initialisation
- Zero configuration
- Feature enable / disable
- Default ordering (sorting)
- Multi-column ordering
- Multiple tables
- Hidden columns
- Complex headers (rowspan and colspan)
- Flexible table width
- State saving
- Alternative pagination
- Data rendering
- Scroll - vertical
- Scroll - vertical, dynamic height
- Scroll - horizontal
- Scroll - horizontal and vertical
Advanced initialisation
- DOM / jQuery events
- DataTables events
- Column rendering
- Enter Key to Search
- Page length options
- Complex headers with column visibility
- Read HTML to data objects
- HTML5 data-* attributes - cell data
- HTML5 data-* attributes - table options
- Setting defaults
- Row created callback
- Row grouping
- Footer callback
- Order direction sequence control
- DOM element return from renderer
Styling
- Base style
- Base style - no styling classes
- Base style - cell borders
- Base style - compact
- Base style - hover
- Base style - order-column
- Base style - row borders
- Base style - stripe
- Bootstrap 3
- Bootstrap 4
- Bootstrap 5
- Foundation
- Fomantic-UI (formally Semantic-UI)
- Bulma
- jQuery UI ThemeRoller
- Material Design (Tech. preview)
- Tailwind CSS (Tech. preview)
- UIKit 3 (Tech. preview)
API
- Add rows
- Individual column searching (text inputs)
- Individual column searching (select inputs)
- Highlighting rows and columns
- Child rows (show extra / detailed information)
- Child rows with StateSave
- Row selection (multiple rows)
- Row selection and deletion (single row)
- Form inputs
- Index column
- Show / hide columns dynamically
- Using API in callbacks
- Scrolling and Bootstrap tabs
- Search API (regular expressions)
- HighCharts Integration