Extending the simple join example, here we show how a self referencing SQL table can be used with Editor. Self referencing tables can be very useful when there is a hierarchy of like objects in the database.
In this particular example each staff member has a manager, who is also a member of staff. Using a self referencing join we can display each staff member and their manager in the table, while Editor allows these fields to be easily editable.
In SQL terms, the users
table has a column manager
which references the id
column in its own table. In an SQL statement
we use the as
keyword to alias the joined table, which is exactly how the Editor server-side libraries also operate.
The data structure returned by the server for each row in this table is:
123456789101112{
"DT_RowId"
:
"row_1"
,
"users"
: {
"first_name"
:
"Quynn"
,
"last_name"
:
"Contreras"
,
"manager"
:
"1"
},
"manager"
: {
"first_name"
:
"Quynn"
,
"last_name"
:
"Contreras"
}
}
The three fields we want to edit are in the users
object, but we also use the manager
properties for display in the table (through the
use of columns.data
and columns.render
).
See the join documentation (PHP | .NET | NodeJS) for further information about joins with Editor's framework libraries.
First name | Last name | Manager |
---|---|---|
First name | Last name | Manager |
Loading... |
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: "../php/joinSelf.php" , table: "#example" , fields: [ { label: "First name:" , name: "users.first_name" }, { label: "Last name:" , name: "users.last_name" }, { label: "Manager:" , name: "users.manager" , type: "select" } ] } ); $( '#example' ).DataTable( { dom: "Bfrtip" , ajax: "../php/joinSelf.php" , columns: [ { data: "users.first_name" }, { data: "users.last_name" }, { data: "manager" , render: function ( val, type, row ) { return val.first_name ? val.first_name + ' ' + val.last_name : '' ; }, defaultContent: "" } ], 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.
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.
1 | // No data yet submitted |
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.
1 | // No data yet received |