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
- Comments
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:
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:
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)
- DOM positioning
- Flexible table width
- State saving
- Alternative pagination
- Data rendering
- Scroll - vertical
- Scroll - vertical, dynamic height
- Scroll - horizontal
- Scroll - horizontal and vertical
- Language - Comma decimal place
Advanced initialisation
- DOM / jQuery events
- DataTables events
- Column rendering
- Enter Key to Search
- Page length options
- Multiple table control elements
- 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
- Custom toolbar elements
- Order direction sequence control
- Example of stocks results
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)
- 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