# 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.

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

- 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) { 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: '../php/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: '../php/todo.php', columns: [ { 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; } }, { data: 'priority' } ], layout: { topStart: { buttons: [ { extend: 'create', editor: editor }, { extend: 'edit', editor: editor }, { extend: 'remove', editor: editor } ] } }, 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:

`button.inputButton { float: left; text-align: center; display: block; cursor: pointer; margin: 0 20px 10px 0; padding: 6px 0; background: #F8F8F8; background: linear-gradient(to bottom, white, #CCC); text-shadow: 0 1px 0 white; border: 1px solid #999; border-radius: 3px; box-shadow: 0px 0px 2px #999; width: 80px; } button.inputButton.selected { font-weight: normal; background: #02475A; background: linear-gradient(to bottom, #333, #777); 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:

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.