# Editor example Custom field type plug-ins

Although Editor comes with a number of field types built in, you might find that the built in controls don't do exactly what you would like, or you want to present a completely custom control. For these eventualities Editor's field types are designed to be completely extensible and you can create plug-ins to provide your custom controls, or use some of the ones already available.

This example shows how you might create a binary switch using `button` elements, effectively implementing a custom radio control. This might be useful for styling, as shown in this example.

For more detailed information on how to create plug-ins for Editor, please refer to the Editor documentation.

Priority | Item | Status
--- | --- | ---

- Javascript
- HTML
- CSS
- Ajax
- Server-side script

The Javascript shown below is used to initialise the table shown in this example:

`// // Plug-in code // (function ($, DataTable) { if (!DataTable.ext.editorFields) { DataTable.ext.editorFields = {}; } var Editor = DataTable.Editor; var _fieldTypes = DataTable.ext.editorFields; _fieldTypes.todo = { create: function (conf) { var that = this; conf._enabled = true; // Create the elements to use for the input conf._input = $( '<div id="' + Editor.safeId(conf.id) + '">' + '<button type="button" class="inputButton" value="0">To do</button>' + '<button type="button" class="inputButton" value="1">Done</button>' + '</div>' ); // Use the fact that we are called in the scope of the Editor instance to call // the API method for setting the value when needed $('button.inputButton', conf._input).click(function () { if (conf._enabled) { that.set(conf.name, $(this).attr('value')); } return false; }); return conf._input; }, get: function (conf) { return $('button.selected', conf._input).attr('value'); }, set: function (conf, val) { $('button.selected', conf._input).removeClass('selected'); $('button.inputButton[value=' + val + ']', conf._input).addClass('selected'); }, enable: function (conf) { conf._enabled = true; $(conf._input).removeClass('disabled'); }, disable: function (conf) { conf._enabled = false; $(conf._input).addClass('disabled'); } }; })(jQuery, DataTable); // // Initialisation code // var editor = new DataTable.Editor({ ajax: '../../controllers/todo.php', table: '#example', fields: [ { label: 'Item:', name: 'item' }, { label: 'Status:', name: 'done', type: 'todo', // Using the custom field type def: 0 }, { label: 'Priority:', name: 'priority', type: 'select', options: [ { label: '1 (highest)', value: '1' }, { label: '2', value: '2' }, { label: '3', value: '3' }, { label: '4', value: '4' }, { label: '5 (lowest)', value: '5' } ] } ] }); $('#example').DataTable({ ajax: '../../controllers/todo.php', buttons: [ { extend: 'create', editor: editor }, { extend: 'edit', editor: editor }, { extend: 'remove', editor: editor } ], columns: [ { data: 'priority', className: 'center' }, { data: 'item' }, { className: 'center', data: 'done', render: function (data, type, row) { if (type === 'display' || type === 'filter') { // Filtering and display get the rendered string return data == 0 ? 'To do' : 'Done'; } // Otherwise just give the original data return data; } } ], dom: 'Bfrtip', select: true });`
`// // Plug-in code // (function (DataTable) { var Editor = DataTable.Editor; var _fieldTypes = DataTable.ext.editorFields; _fieldTypes.todo = { create: function (conf) { conf._enabled = true; // Create the elements to use for the input conf._input = document.createElement('div'); conf._input.id = Editor.safeId(conf.id); // Build button 1 (To do) and its event listener let button1 = document.createElement('button'); button1.type = 'button'; button1.value = 0; button1.classList.add('inputButton'); button1.textContent = 'To do'; conf._input.appendChild(button1); button1.addEventListener('click', () => { if (conf._enabled) { this.set(conf.name, button1.value); } }); // Build button 2 (Done) and its event listener let button2 = document.createElement('button'); button2.type = 'button'; button2.value = 1; button2.classList.add('inputButton'); button2.textContent = 'Done'; conf._input.appendChild(button2); button2.addEventListener('click', () => { if (conf._enabled) { this.set(conf.name, button2.value); } }); return conf._input; }, get: function (conf) { return conf._input.querySelector('button.selected').value; }, set: function (conf, val) { let current = conf._input.querySelector('button.selected'); if (current) { current.classList.remove('selected'); } conf._input .querySelector('button.inputButton[value="' + val + '"]') .classList.add('selected'); }, enable: function (conf) { conf._enabled = true; conf._input.classList.remove('disabled'); }, disable: function (conf) { conf._enabled = false; conf._input.classList.remove('add'); } }; })(DataTable); // // Initialisation code // const editor = new DataTable.Editor({ ajax: '../../controllers/todo.php', table: '#example', fields: [ { label: 'Item:', name: 'item' }, { label: 'Status:', name: 'done', type: 'todo', // Using the custom field type def: 0 }, { label: 'Priority:', name: 'priority', type: 'select', options: [ { label: '1 (highest)', value: '1' }, { label: '2', value: '2' }, { label: '3', value: '3' }, { label: '4', value: '4' }, { label: '5 (lowest)', value: '5' } ] } ] }); new DataTable('#example', { ajax: '../../controllers/todo.php', buttons: [ { extend: 'create', editor }, { extend: 'edit', editor }, { extend: 'remove', editor } ], columns: [ { data: 'priority', className: 'center' }, { data: 'item' }, { className: 'center', data: 'done', render: (data, type, row) => { if (type === 'display' || type === 'filter') { // Filtering and display get the rendered string return data == 0 ? 'To do' : 'Done'; } // Otherwise just give the original data return data; } } ], dom: 'Bfrtip', select: true });`

In addition to the above code, the following Javascript library files are loaded for use in this example:

- https://code.jquery.com/jquery-3.7.0.js
- https://cdn.datatables.net/1.13.5/js/jquery.dataTables.min.js
- https://cdn.datatables.net/buttons/2.4.1/js/dataTables.buttons.min.js
- https://cdn.datatables.net/select/1.7.0/js/dataTables.select.min.js
- https://cdn.datatables.net/datetime/1.5.1/js/dataTables.dateTime.min.js
- ../../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:

`button.inputButton { float: left; text-align: center; display: block; cursor: pointer; margin: 0 20px 10px 0; padding: 6px 0; background: #F8F8F8; background: -webkit-gradient(linear, center bottom, center top, from(#CCC), to(white)); background: -moz-linear-gradient(top, white, #CCC); background: linear-gradient(to bottom, white, #CCC); text-shadow: 0 1px 0 white; border: 1px solid #999; border-radius: 3px; -moz-border-radius: 3px; -webkit-border-radius: 3px; box-shadow: 0px 0px 2px #999; -moz-box-shadow: 0px 0px 2px #999; -webkit-box-shadow: 0px 0px 2px #999; width: 80px; } button.inputButton.selected { font-weight: normal; background: #02475A; background: -webkit-gradient(linear, center bottom, center top, from(#777), to(#333)); background: -moz-linear-gradient(top, #333, #777); background: linear-gradient(to bottom, #333, #777); box-shadow: inset 0px 0px 2px #222; -moz-box-shadow: inset 0px 0px 2px #222; -webkit-box-shadow: inset 0px 0px 2px #222; color: white; text-shadow: none; }`

The following CSS library files are loaded for use in this example to provide the styling of the table:

- https://cdn.datatables.net/1.13.5/css/jquery.dataTables.min.css
- https://cdn.datatables.net/buttons/2.4.1/css/buttons.dataTables.min.css
- https://cdn.datatables.net/select/1.7.0/css/select.dataTables.min.css
- https://cdn.datatables.net/datetime/1.5.1/css/dataTables.dateTime.min.css
- ../../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.