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.

NameSiteVisit date
Loading...
NameSiteVisit date
Showing 0 to 0 of 0 entries

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
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:

Editor submits and retrieves information by Ajax requests. The two blocks below show the data that Editor submits and receives, to and from the server. This is updated live as you interact with Editor so you can see what is submitted.

Submitted data:

The following shows the data that has been submitted to the server when a request is made to add, edit or delete data from the table.

// No data yet submitted

Server response:

The following shows the data that has been returned by the server in response to the data submitted on the left and is then acted upon.

// No data yet received