One of ColumnControl's search features, is the ability to show a list of options, from which the end user can select from. This is provided through the searchList content type, and can be populated from
the Editor server-side libraries.
This example demonstrates the use of searchDropdown which shows the search input in a dropdown button, and will automatically switch between text input and a list,
depending on if options are returned from the server. Some data (i.e. repeating information) will lend itself to a list based search, while other data is benefits
from text input. In this case, Last name and Site have options populated from the server-side, while the other two columns have a text based
input.
| First name | Last name | Phone number | Site |
|---|
The JavaScript shown below is used to initialise the table shown in this example:
var editor = new DataTable.Editor({
ajax: '../../controllers/columnControlList.php',
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',
placeholder: 'Select a location'
}
],
table: '#example'
});
$('#example').DataTable({
ajax: {
url: '../../controllers/columnControlList.php',
type: 'POST'
},
columnControl: ['order', 'searchDropdown'],
columns: [
{ data: 'users.first_name' },
{ data: 'users.last_name' },
{ data: 'users.phone' },
{ data: 'sites.name' }
],
layout: {
topStart: {
buttons: [
{ extend: 'create', editor: editor },
{ extend: 'edit', editor: editor },
{ extend: 'remove', editor: editor }
]
}
},
ordering: {
indicators: false,
handler: false
},
select: true,
serverSide: true
});
const editor = new DataTable.Editor({
ajax: '../../controllers/columnControlList.php',
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',
placeholder: 'Select a location'
}
],
table: '#example'
});
new DataTable('#example', {
ajax: {
url: '../../controllers/columnControlList.php',
type: 'POST'
},
columnControl: ['order', 'searchDropdown'],
columns: [
{ data: 'users.first_name' },
{ data: 'users.last_name' },
{ data: 'users.phone' },
{ data: 'sites.name' }
],
layout: {
topStart: {
buttons: [
{ extend: 'create', editor: editor },
{ extend: 'edit', editor: editor },
{ extend: 'remove', editor: editor }
]
}
},
ordering: {
indicators: false,
handler: false
},
select: true,
serverSide: 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:
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.