The data shown in a DataTable does not always need match 1:1 to the fields shown in the Editor form (as shown by the table only and form only example), something which is frequently used in joined
table - e.g. the table shows the information from the joined table, while the Editor modifies the reference for the record that is being joined to. In this example
the Location comes from the sites.name
parameter, while Editor will modify users.site
.
When using inline editing Editor will attempt to automatically determine the field to be edited based on the DataTables configuration, but this will only work
if columns.data
matches the
fields.name
. In the
Location case here it does not. This would result is a Unable to automatically determine field from source
error being emitted by Editor if
it wasn't resolved.
It is possible to use inline()
to specify the
field to be edited, but that requires an additional event handler to be used for the special column.
To resolve this, Editor supports an additional property on the DataTables columns called editField
which tells Editor which field to edit when
inline or bubble editing this field. In this example editField: "users.site"
is used for the Location column.
First name | Last name | Phone # | Location | |
---|---|---|---|---|
First name | Last name | Phone # | Location | |
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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | 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/join.php" , table: "#example" , fields: [ { label: "First name:" , name: "users.first_name" }, { label: "Last name:" , name: "users.last_name" }, { label: "Phone #:" , name: "users.phone" }, { label: "Site:" , name: "users.site" , type: "select" } ] } ); // Activate an inline edit on click of a table cell $( '#example' ).on( 'click' , 'tbody td:not(:first-child)' , function (e) { editor.inline( this , { onBlur: 'submit' } ); } ); $( '#example' ).DataTable( { dom: "Bfrtip" , ajax: { url: "../php/join.php" , type: 'POST' }, columns: [ { data: null , defaultContent: '' , className: 'select-checkbox' , orderable: false }, { data: "users.first_name" }, { data: "users.last_name" }, { data: "users.phone" }, { data: "sites.name" , editField: "users.site" } ], order: [ 1, 'asc' ], select: { style: 'os' , selector: 'td:first-child' }, 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 |