This example demonstrates Editor's ability to do multi-row selection in the datatable
field, while also allowing editing of the nested data. It is based on the Multiple selection
example, but adds editing abilities to the nested data.
The row selection of the nested table indicates the value of the field, so we need a way to allow editing and deletion of data without modifying the value. For this we draw on the in table form controls example to have edit and delete buttons for each row, which do not effect the host field's value. A "New" button is provided to add new permissions, similar to the single selection example.
First name | Last name | Location | Permissions |
---|---|---|---|
First name | Last name | Location | Permissions |
The Javascript shown below is used to initialise the table shown in this example:
var permissionsEditor = new DataTable.Editor({
ajax: {
url: '../../controllers/permissions.php'
},
fields: [
{
label: 'Name:',
name: 'name'
}
]
});
var usersEditor = new DataTable.Editor({
ajax: '../../controllers/joinArray.php',
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: 'datatable',
fieldInfo: 'To select multiple permissions, use ctrl-click, or shift-click to range select',
editor: permissionsEditor,
multiple: true,
optionsPair: {
value: 'id'
},
config: {
ajax: '../../controllers/permissions.php',
buttons: [
{
extend: 'create',
editor: permissionsEditor
}
],
columns: [
{
title: 'Name',
data: 'name'
},
{
data: null,
className: 'dt-right',
defaultContent: '<i class="fa fa-pencil"></i> <i class="fa fa-trash"></i>',
orderable: false
}
],
paging: false,
scrollY: 300,
scrollCollapse: true,
select: {
selector: 'td:first-child'
}
}
}
],
table: '#example'
});
// Event handlers for in table controls
var permissionsTable = usersEditor.field('permission[].id').dt();
permissionsTable.on('click', '.fa-pencil', function () {
permissionsEditor.edit(this.closest('tr'), {
buttons: 'Save',
nest: true,
title: 'Edit permission'
});
});
permissionsTable.on('click', '.fa-trash', function () {
var name = permissionsTable.row(this.closest('tr')).data().name();
permissionsEditor.remove(this.closest('tr'), {
buttons: 'Delete',
message: 'Are you sure you wish to delete the permission <strong>' + name + '</strong>? It will be removed from all users who are currently assigned this permission.',
nest: true,
title: 'Delete permission',
});
});
$('#example').DataTable({
ajax: {
url: '../../controllers/joinArray.php',
type: 'POST'
},
columns: [
{data: 'users.first_name'},
{data: 'users.last_name'},
{data: 'sites.name'},
{data: 'permission', render: '[, ].name'}
],
layout: {
topStart: {
buttons: [
{extend: 'create', editor: usersEditor},
{extend: 'edit', editor: usersEditor},
{extend: 'remove', editor: usersEditor}
]
}
},
select: true
});
const permissionsEditor = new DataTable.Editor({
ajax: {
url: '../../controllers/permissions.php'
},
fields: [
{
label: 'Name:',
name: 'name'
}
]
});
const usersEditor = new DataTable.Editor({
ajax: '../../controllers/joinArray.php',
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: 'datatable',
fieldInfo: 'To select multiple permissions, use ctrl-click, or shift-click to range select',
editor: permissionsEditor,
multiple: true,
optionsPair: {
value: 'id'
},
config: {
ajax: '../../controllers/permissions.php',
buttons: [
{
extend: 'create',
editor: permissionsEditor
}
],
columns: [
{
title: 'Name',
data: 'name'
},
{
data: null,
className: 'dt-right',
defaultContent: '<i class="fa fa-pencil"></i> <i class="fa fa-trash"></i>',
orderable: false
}
],
paging: false,
scrollY: 300,
scrollCollapse: true,
select: {
selector: 'td:first-child'
}
}
}
],
table: '#example'
});
// Event handlers for in table controls
const permissionsTable = usersEditor.field('permission[].id').dt();
permissionsTable.on('click', '.fa-pencil', function () {
permissionsEditor.edit(this.closest('tr'), {
buttons: 'Save',
nest: true,
title: 'Edit permission'
});
});
permissionsTable.on('click', '.fa-trash', function () {
var name = permissionsTable.row(this.closest('tr')).data().name;
permissionsEditor.remove(this.closest('tr'), {
buttons: 'Delete',
message: 'Are you sure you wish to delete the permission <strong>' + name + '</strong>?<br/>It will be removed from all users who are currently assigned this permission.',
nest: true,
title: 'Delete permission',
});
});
new DataTable('#example', {
ajax: {
url: '../../controllers/joinArray.php',
type: 'POST'
},
columns: [
{data: 'users.first_name'},
{data: 'users.last_name'},
{data: 'sites.name'},
{data: 'permission', render: '[, ].name'}
],
layout: {
topStart: {
buttons: [
{extend: 'create', editor: usersEditor},
{extend: 'edit', editor: usersEditor},
{extend: 'remove', editor: usersEditor}
]
}
},
select: true
});
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:
table i.fa {
cursor: pointer;
margin-right: 0.5em;
}
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.