Although many of the Editor examples show the data for the table being loaded by Ajax (ajax
) this is by no means mandatory. Editor will work with any data source that DataTables can use, with the
only additional requirement that each row has a unique ID (allowing the server to identify which rows to update, delete, etc).
This example shows Editor being applied to a plain HTML table (generated from the database, although it could come from absolutely anywhere). Ajax requests are still used for create, edit and remove actions, but not for loading the initial data.
This example uses DataTables ability to read DOM source tables into a JSON object data store, with the keys specified by columns.data
. This makes working with the
data in the table very easy since it is named object parameters rather than arrays where indexes must be used. Please note that this feature requires DataTables 1.10.8 or newer.
First name | Last name | Position | Office | Salary |
---|---|---|---|---|
Tiger | Nixon | System Architect | Edinburgh | $320,800 |
Garrett | Winters | Accountant | Tokyo | $170,750 |
Ashton | Cox | Junior Technical Author | San Francisco | $86,000 |
Cedric | Kelly | Senior Javascript Developer | Edinburgh | $433,060 |
Airi | Satou | Accountant | Tokyo | $162,700 |
Brielle | Williamson | Integration Specialist | New York | $372,000 |
Herrod | Chandler | Sales Assistant | San Francisco | $137,500 |
Rhona | Davidson | Integration Specialist | Tokyo | $327,900 |
Colleen | Hurst | Javascript Developer | San Francisco | $205,500 |
Sonya | Frost | Software Engineer | Edinburgh | $103,600 |
Jena | Gaines | Office Manager | London | $90,560 |
Quinn | Flynn | Support Lead | Edinburgh | $342,000 |
Charde | Marshall | Regional Director | San Francisco | $470,600 |
Haley | Kennedy | Senior Marketing Designer | London | $313,500 |
Tatyana | Fitzpatrick | Regional Director | London | $385,750 |
Michael | Silva | Marketing Designer | London | $198,500 |
Paul | Byrd | Chief Financial Officer (CFO) | New York | $725,000 |
Gloria | Little | Systems Administrator | New York | $237,500 |
Bradley | Greer | Software Engineer | London | $132,000 |
Dai | Rios | Personnel Lead | Edinburgh | $217,500 |
Jenette | Caldwell | Development Lead | New York | $345,000 |
Yuri | Berry | Chief Marketing Officer (CMO) | New York | $675,000 |
Caesar | Vance | Pre-Sales Support | New York | $106,450 |
Doris | Wilder | Sales Assistant | Sydney | $85,600 |
Angelica | Ramos | Chief Executive Officer (CEO) | London | $1,200,000 |
Gavin | Joyce | Developer | Edinburgh | $92,575 |
Jennifer | Chang | Regional Director | Singapore | $357,650 |
Brenden | Wagner | Software Engineer | San Francisco | $206,850 |
Fiona | Green | Chief Operating Officer (COO) | San Francisco | $850,000 |
Shou | Itou | Regional Marketing | Tokyo | $163,000 |
Michelle | House | Integration Specialist | Sydney | $95,400 |
Suki | Burks | Developer | London | $114,500 |
Prescott | Bartlett | Technical Author | London | $145,000 |
Gavin | Cortez | Team Leader | San Francisco | $235,500 |
Martena | Mccray | Post-Sales support | Edinburgh | $324,050 |
Unity | Butler | Marketing Designer | San Francisco | $85,675 |
Howard | Hatfield | Office Manager | San Francisco | $164,500 |
Hope | Fuentes | Secretary | San Francisco | $109,850 |
Vivian | Harrell | Financial Controller | San Francisco | $452,500 |
Timothy | Mooney | Office Manager | London | $136,200 |
Jackson | Bradshaw | Director | New York | $645,750 |
Olivia | Liang | Support Engineer | Singapore | $234,500 |
Bruno | Nash | Software Engineer | London | $163,500 |
Sakura | Yamamoto | Support Engineer | Tokyo | $139,575 |
Thor | Walton | Developer | New York | $98,540 |
Finn | Camacho | Support Engineer | San Francisco | $87,500 |
Serge | Baldwin | Data Coordinator | Singapore | $138,575 |
Zenaida | Frank | Software Engineer | New York | $125,250 |
Zorita | Serrano | Software Engineer | San Francisco | $115,000 |
Jennifer | Acosta | Junior Javascript Developer | Edinburgh | $75,650 |
Cara | Stevens | Sales Assistant | New York | $145,600 |
Hermione | Butler | Regional Director | London | $356,250 |
Lael | Greer | Systems Administrator | London | $103,500 |
Jonas | Alexander | Developer | San Francisco | $86,500 |
Shad | Decker | Regional Director | Edinburgh | $183,000 |
Michael | Bruce | Javascript Developer | Singapore | $183,000 |
Donna | Snider | Customer Support | New York | $112,000 |
The Javascript shown below is used to initialise the table shown in this example:
var editor; // use a global for the submit and return data rendering in the examples
$(document).ready(function() {
editor = new $.fn.dataTable.Editor( {
ajax: '/api/staff-html',
table: '#example',
fields: [ {
label: "First name:",
name: "first_name"
}, {
label: "Last name:",
name: "last_name"
}, {
label: "Position:",
name: "position"
}, {
label: "Office:",
name: "office"
}, {
label: "Salary:",
name: "salary",
data: function (row, type, val) {
// Strip the formatting from the salary number
if ( type === 'set' ) {
row.salary = val;
}
return row.salary.replace(/[^0-9]/g, '');
}
}
]
} );
$('#example').DataTable( {
dom: 'Bfrtip',
columns: [
{ data: "first_name" },
{ data: "last_name" },
{ data: "position" },
{ data: "office" },
{ data: "salary" }
],
select: true,
buttons: [
{ extend: "create", editor: editor },
{ extend: "edit", editor: editor },
{ extend: "remove", editor: editor }
]
} );
} );
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.