Editor example Join tables - self referencing join

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:

{
	"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) for further information about joins with Editor's framework libraries.

First name Last name Manager
First name Last name Manager

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: "../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:

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.