Editor example Compound database primary key

Editor's PHP, .NET and NodeJS server-side libraries support the use of compound keys (as of v1.6). On the client-side no special configuration is required, while on the server-side you simply need to use an array as the optional third parameter for the Editor class constructor with the column names that make up the primary key.

It is important to note that when designing a form that uses a compound key, the data that makes up the primary key must either be included in the form, or generated in the server-side script. It cannot currently use data generated by the database.

In this example the staff member id and the date make up the compound primary key for the users_vists database table.

Name Site Visit date
Name Site Visit date

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: '../../controllers/compoundKey.php', table: '#example', fields: [ { label: 'Staff member:', name: 'users_visits.user_id', type: 'select' }, { label: 'Visiting site:', name: 'users_visits.site_id', type: 'select' }, { label: 'Date:', name: 'users_visits.visit_date', type: 'datetime' } ] } ); $('#example').DataTable( { dom: 'Bfrtip', ajax: { url: '../../controllers/compoundKey.php', type: 'POST' }, columns: [ { data: null, render: function ( data, type, row ) { return data.users.first_name+' '+data.users.last_name; } }, { data: 'sites.name' }, { data: 'users_visits.visit_date' } ], 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.