Editor example Join tables - link table

Data in relational databases is often stored over multiple tables, partitioned by the data type and then joined together using SQL JOIN queries. In one form of joined table, a 'link' table is used linking records between two different types of data.

In this example, which extends the simple join example, the users and dept tables are linked by the user_dept table. When the data is read, we read from all three tables (plus the sites table to show multiple joins working together). The returned data structure for each row in this example is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
{
    "DT_RowId": "row_1",
    "users": {
        "first_name": "Quynn",
        "last_name": "Contreras",
        "site": "1"
    },
    "sites": {
        "name": "Edinburgh"
    },
    "user_dept": {
        "dept_id": "1"
    },
    "dept": {
        "name": "IT"
    }
}

For editing we want to write to three users fields and the user_dept field. The Editor instance is configured with these four fields using the fields.name option and the Editor server-side libraries perform the required SQL operations for us on create, edit and delete.

See the join documentation (PHP | .NET | NodeJS) for further information about joins with Editor's framework libraries.

First nameLast nameLocationDepartment
Loading...
First nameLast nameLocationDepartment
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
44
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/joinLinkTable.php",
        table: "#example",
        fields: [ {
                label: "First name:",
                name:  "users.first_name"
            }, {
                label: "Last name:",
                name:  "users.last_name"
            }, {
                label: "Site:",
                name:  "users.site",
                type:  "select"
            }, {
                label: "Department:",
                name:  "user_dept.dept_id",
                type:  "select"
            }
        ]
    } );
 
    $('#example').DataTable( {
        dom: "Bfrtip",
        ajax: {
            url: "../../controllers/joinLinkTable.php",
            type: 'POST'
        },
        columns: [
            { data: "users.first_name" },
            { data: "users.last_name" },
            { data: "sites.name" },
            { data: "dept.name" }
        ],
        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