DataTable as a single select
This example is exactly the same as the simple join with the single exception that a DataTable is used as the site
selector input rather than a select
element. This is done by specifying the field as using the datatable
field type.
By default, as shown in this example, it will use the same array of label
/value
objects that the select
input uses, but also presents the end user
with a complete DataTable, allowing paging, filtering and sorting of the data.
First name | Last name | Phone # | Location |
---|---|---|---|
First name | Last name | Phone # | Location |
- Javascript
- HTML
- CSS
- Ajax
- Server-side script
- Comments
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/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: "datatable",
}
]
} );
$('#example').DataTable( {
dom: "Bfrtip",
ajax: {
url: "../php/join.php",
type: 'POST'
},
columns: [
{ data: "users.first_name" },
{ data: "users.last_name" },
{ data: "users.phone" },
{ data: "sites.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:
- https://code.jquery.com/jquery-3.5.1.js
- https://cdn.datatables.net/1.10.24/js/jquery.dataTables.min.js
- https://cdn.datatables.net/buttons/1.7.0/js/dataTables.buttons.min.js
- https://cdn.datatables.net/select/1.3.3/js/dataTables.select.min.js
- https://cdn.datatables.net/datetime/1.0.2/js/dataTables.dateTime.min.js
- ../../extensions/Editor/js/dataTables.editor.min.js
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:
- https://cdn.datatables.net/1.10.24/css/jquery.dataTables.min.css
- https://cdn.datatables.net/buttons/1.7.0/css/buttons.dataTables.min.css
- https://cdn.datatables.net/select/1.3.3/css/select.dataTables.min.css
- https://cdn.datatables.net/datetime/1.0.2/css/dataTables.dateTime.min.css
- ../../extensions/Editor/css/editor.dataTables.min.css
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.
Other examples
Simple initialisation
- Basic initialisation
- Multi-row editing
- Field types
- Setting defaults
- Local table editing
- Internationalisation (local)
- Internationalisation (from JSON file)
- In table form controls
- Server-side processing
- Custom form layout / templates (attributes)
- Custom form layout / templates (tags)
- Join tables - working with multiple SQL tables
Advanced initialisation
- Data shown only in the form
- Data shown in table only
- Multi-item editing (rows, columns, cells)
- REST interface
- Complex (nested) JSON data source
- Ajax override - using localStorage for the data source
- Row ID source specification
- Compound database primary key
- SQL VIEW
- DOM sourced table
- Join tables - self referencing join
- Join tables - link table
- Join tables - one-to-many join
- Parent child editor
- File upload
- File upload (many)
Inline editing
- Simple inline editing
- Tab between columns
- Editing options - submit on blur
- Editing options - submit full row data
- Inline editing with a submit button
- Edit icon
- Joined tables
- Selected columns only
- Responsive integration
- FixedColumns integration
- Server-side processing
- Whole row - any cell acivation
- Whole row - icon controls
- Whole row - inline create
- Whole row - blur submit