# Editor example Tiny MCE

Name | Position | Office | Extn. | Start date | Salary
--- | --- | --- | --- | --- | ---
Name | Position | Office | Extn. | Start date | Salary

- JavaScript
- HTML
- CSS
- Ajax
- Server-side script

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

`(function (factory) { if (typeof define === 'function' && define.amd) { // AMD define(['jquery', 'datatables', 'datatables-editor'], factory); } else if (typeof exports === 'object') { // Node / CommonJS module.exports = function ($, dt) { if (!$) { $ = require('jquery'); } factory($, dt || DataTable || require('datatables')); }; } else if (jQuery) { // Browser standard factory(jQuery, jQuery.fn.dataTable); } })(function ($, DataTable) { 'use strict'; if (!DataTable.ext.editorFields) { DataTable.ext.editorFields = {}; } var _fieldTypes = DataTable.Editor ? DataTable.Editor.fieldTypes : DataTable.ext.editorFields; _fieldTypes.tinymce = { _reinit: function (conf, host) { if (conf._doingInit) { return true; } // Destroy any existing editor var editor = tinymce.get(conf._safeId); if ($('#' + conf._safeId).length === 0) { return; } if (editor) { editor.destroy(); } conf._doingInit = true; setTimeout(function () { tinymce.init( $.extend( true, {}, { selector: '#' + conf._safeId, setup: function (editor) { editor.on('init', function (e) { if (conf._initSetVal) { editor.setContent(conf._initSetVal); conf._initSetVal = null; } conf._doingInit = false; }); host.bubblePosition(); } }, conf.opts ) ); }, 250); }, _getAndSet: function (conf, host) { if ($('body').find(conf._input).length) { _fieldTypes.tinymce._reinit(conf, host); } else { host.one('open', function () { _fieldTypes.tinymce._reinit(conf, host); }); } }, create: function (conf) { var that = this; conf._safeId = DataTable.Editor.safeId(conf.id); conf._input = $( '<div><textarea id="' + conf._safeId + '"></textarea></div>' ); // Because tinyMCE uses an editable iframe, we need to destroy and // recreate it on every display of the input this.on('initCreate.tinymceInit-' + conf._safeId, function () { _fieldTypes.tinymce._getAndSet(conf, that); }) .on('initEdit.tinymceInit-' + conf._safeId, function () { _fieldTypes.tinymce._getAndSet(conf, that); }) .on('close.tinymceInit-' + conf._safeId, function () { var editor = tinymce.get(conf._safeId); if (editor) { editor.destroy(); } conf._initSetVal = null; conf._input.find('textarea').val(''); }); return conf._input; }, get: function (conf) { var editor = tinymce.get(conf._safeId); if (!editor) { return conf._initSetVal; } return editor.getContent(); }, set: function (conf, val) { var editor = tinymce.get(conf._safeId); // If not ready, then store the value to use when the `open` event fires conf._initSetVal = val; if (!editor) { return; } editor.setContent(val); }, enable: function (conf) {}, // not supported in TinyMCE disable: function (conf) {}, // not supported in TinyMCE destroy: function (conf) { var id = DataTable.Editor.safeId(conf.id); this.off('open.tinymceInit-' + id); this.off('close.tinymceInit-' + id); this.off('displayOrder.tinymceInit-' + id); }, owns: function (conf, node) { return $(node).closest('div.DTE_Field_Type_tinymce').length ? true : false; }, // Get the TinyMCE instance - note that this is only available after the // first onOpen event occurs tinymce: function (conf) { return tinymce.get(conf._safeId); } }; }); $(document).ready(function () { var editor = new DataTable.Editor({ ajax: '../php/staff.php', formOptions: { main: { onComplete: 'none' } }, table: '#example', fields: [ { label: 'First name:', name: 'first_name' }, { label: 'Last name:', name: 'last_name' }, { label: 'Position:', name: 'position', type: 'tinymce' }, { label: 'Office:', name: 'office', type: 'tinymce' }, { label: 'Extension:', name: 'extn' }, { label: 'Start date:', name: 'start_date' }, { label: 'Salary:', name: 'salary' } ] }); $('#example').on('click', 'tbody td:eq(1)', function () { editor.bubble(this); }); $('#example').on('click', 'tbody td:eq(2)', function () { editor.bubble(this); }); var table = $('#example').DataTable({ dom: 'Bfrtip', ajax: '../php/staff.php', columns: [ { data: null, render: function (data, type, row) { // Combine the first and last names into a single table field return data.first_name + ' ' + data.last_name; } }, { data: 'position' }, { data: 'office' }, { data: 'extn' }, { data: 'start_date' }, { data: 'salary', render: DataTable.render.number(null, null, 0, '$') } ], 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:

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.