Working with categories and sub-categories is a very common action in data intensive applications. It can be useful both for the programmer (i.e. you!) in terms
of data manipulation and for the end user's conceptual model of the data. Categories are easy with Editor's select
option - simply populate it with data and the end
user can select from it. Sub-categories require a little more interaction since the options available depend on the value the user has selected for the main
category.
This example demonstrates how Editor's dependent()
method can be used to retrieve values for the select
element based on the selection of another field. A blog post
explains in detail how this example is constructed.
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 | 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/cascadingLists.php" , table: "#example" , fields: [ { label: "Name:" , name: "team.name" }, { label: "Continent:" , name: "team.continent" , type: "select" }, { label: "Country:" , name: "team.country" , type: "select" } ] } ); editor.dependent( 'team.continent' , '../../controllers/countries.php' ); $( '#example' ).DataTable( { dom: "Bfrtip" , ajax: { url: "../../controllers/cascadingLists.php" , type: 'POST' }, columns: [ { data: "team.name" }, { data: "continent.name" }, { data: "country.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.
// 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.
// No data yet received