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:
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 |
---|---|---|---|---|
ACME Gadgets | AGDTS | 2.59 | 0.01 | |
Sole Goodman | SGMAN | 16.19 | -0.27 | |
Spry Media Productions | SPMP | 1.08 | -0.03 | |
Stanler Bits and Bobs | SBIBO | 83.72 | 0.43 | |
Widget Emporium | WDEMP | 3.49 | 0.01 | |
Name | Symbol | Price | Difference | Last |
The Javascript shown below is used to initialise the table shown in this example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | $(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: