# DataTables example Example of stocks results

Data within DataTables can be easily rendered to add graphics or colour to your tables, as demonstrated in the example on this page. These examples make use of `columns.render` and `drawCallback` to customise the cells in three ways:

- the colour of the cell is determine by the relative price of the stock
- a 'sparkline' class is added to the numeric array in the 'last' column
- the jQuery Sparklines plugin is called to turn that array into a line graph

See the data rendering manual page for more details on how to use data renderers. Also, this example uses Ajax to load the data. This articifially cycles through some pre-canned numbers, but if you have access to a financial Ajax feed, you could create a DataTable to display that. More Ajax examples are available.

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:

`$(document).ready(function () { var stock_data = [ { name: 'ACME Gadgets', symbol: 'AGDTS', last: [2.57, 2.54, 2.54, 2.56, 2.57, 2.58, 2.59], }, { name: 'Spry Media Productions', symbol: 'SPMP', last: [1.12, 1.11, 1.08, 1.08, 1.09, 1.11, 1.08], }, { name: 'Widget Emporium', symbol: 'WDEMP', last: [3.4, 3.39, 3.46, 3.51, 3.5, 3.48, 3.49], }, { name: 'Sole Goodman', symbol: 'SGMAN', last: [16.2, 16.4, 16.36, 16.35, 16.61, 16.46, 16.19], }, { name: 'Stanler Bits and Bobs', symbol: 'SBIBO', last: [82.51, 83.47, 83.4, 83.68, 83.81, 83.29, 83.72], }, ]; let table = $('#example').DataTable({ ajax: function (dataSent, callback, settings) { let data = this.api().ajax.json(); if (data == undefined) { data = stock_data; } else { data = data.data; for (i = 0; i < data.length; i++) { data[i].last.push(data[i].last.shift()); } } callback({ data: data }); }, paging: false, initComplete: function () { let api = this.api(); setInterval(function () { api.ajax.reload(); }, 5000); }, drawCallback: function () { $('.sparkline') .map(function () { return $('canvas', this).length ? null : this; }) .sparkline('html', { type: 'line', width: '250px', }); }, columns: [ { data: 'name', }, { data: 'symbol', }, { data: null, render: function (data, type, row, meta) { return row.last[row.last.length - 1].toFixed(2); }, }, { data: null, render: function (data, type, row, meta) { var val = (row.last[row.last.length - 1] - row.last[row.last.length - 2]).toFixed(2); var colour = val < 0 ? 'red' : 'green'; return type === 'display' ? '<span style="color:' + colour + '">' + val + '</span>' : val; }, }, { data: 'last', render: function (data, type, row, meta) { return type === 'display' ? '<span class="sparkline">' + data.toString() + '</span>' : data; }, }, ], }); });`

In addition to the above code, the following Javascript library files are loaded for use in this example:

- https://code.jquery.com/jquery-3.5.1.js
- ../../media/js/jquery.dataTables.js
- https://cdnjs.cloudflare.com/ajax/libs/jquery-sparklines/2.1.2/jquery.sparkline.min.js

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:

``

The following CSS library files are loaded for use in this example to provide the styling of the table:

- ../../media/css/jquery.dataTables.css

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.