# DataTables example 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

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.