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
  • Javascript
  • HTML
  • CSS
  • Ajax
  • Server-side script
  • Comments

The Javascript shown below is used to initialise the table shown in this example:

var editor = new DataTable.Editor({ ajax: '../php/compoundKey.php', 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' } ], table: '#example' }); $('#example').DataTable({ ajax: { url: '../php/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' } ], layout: { topStart: { buttons: [ { extend: 'create', editor: editor }, { extend: 'edit', editor: editor }, { extend: 'remove', editor: editor } ] } }, select: true });
const editor = new DataTable.Editor({ ajax: '../php/compoundKey.php', 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' } ], table: '#example' }); new DataTable('#example', { ajax: { url: '../php/compoundKey.php', type: 'POST' }, columns: [ { data: null, render: (data) => data.users.first_name + ' ' + data.users.last_name }, { data: 'sites.name' }, { data: 'users_visits.visit_date' } ], layout: { topStart: { buttons: [ { extend: 'create', editor: editor }, { extend: 'edit', editor: editor }, { extend: 'remove', editor: editor } ] } }, select: true });

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 interaction for this demo is shown below. This server uses PHP, so the PHP script is shown, however our download packages include the equivalent script for other platforms, including .NET and Node.js. Server-side scripts can be written in any language, using the protocol described in the Editor documentation.

      Other examples