When using joined database tables, there are times when you might wish to use a one-to-many relationship for your data, and display that as an editable interface for your system's users.
On the client-side, Editor achieved this quite simply by making use of DataTables' powerful columns.data
and columns.render
options to display the
data and the fields.name
option
in Editor. These options all provide the ability to use array based data through Javascript square bracket notation ([]
) - see the DataTables columns.data
documentation for detailed
information on the syntax that can be used for these options.
In this case each record in our users
database table can have different access permissions granted. The one-to-many link is provided by a join
table on the database side.
For the editing interface Editor presents this as a checkbox list of options which the editor can use to select the options they want to assign to a particular user.
On the server-side the Editor PHP, .NET and NodeJS libraries all provide a Join
class that can be used to construct complex CRUD queries for
one-to-many data manipulation. For further information about using the libraries to build an editable DataTable with JOINed tables, please see the join
documentation (PHP | .NET | NodeJS).
First name | Last name | Location | Permissions |
---|---|---|---|
First name | Last name | Location | Permissions |
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/joinArray.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" : "Permissions:" , "name" : "permission[].id" , "type" : "checkbox" } ] } ); $( '#example' ).DataTable( { dom: "Bfrtip" , ajax: { url: "../php/joinArray.php" , type: 'POST' }, columns: [ { data: "users.first_name" }, { data: "users.last_name" }, { data: "sites.name" }, { data: "permission" , render: "[, ].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.
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 |