Editor example CKEditor

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

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.ckeditor = { create: function (conf) { var that = this; var id = DataTable.Editor.safeId(conf.id); conf._input = $('<div><textarea id="' + id + '"></textarea></div>'); // CKEditor needs to be initialised on each open call this.on('open.ckEditInit-' + id, function () { if ($('#' + id).length) { conf._editor = CKEDITOR.replace(id, conf.opts); conf._editor.on('instanceReady', function () { // If shown in a bubble, there is a good chance we'll need to // move it once CKEditor is shown, since it is large! if (conf._input.parents('div.DTE_Bubble').length) { that.bubblePosition(); } }); if (conf._initSetVal) { conf._editor.setData(conf._initSetVal); conf._initSetVal = null; } else { conf._editor.setData(''); } } }); // And destroyed on each close, so it can be re-initialised on reopen this.on('preClose.ckEditInit-' + id, function () { if (conf._editor) { conf._editor.destroy(); conf._editor = null; } }); return conf._input; }, get: function (conf) { if (!conf._editor) { return conf._initSetVal; } return conf._editor.getData(); }, set: function (conf, val) { var id = DataTable.Editor.safeId(conf.id); // If not ready, then store the value to use when the onOpen event fires if (!conf._editor || !$('#' + id).length) { conf._initSetVal = val; return; } conf._editor.setData(val); }, enable: function (conf) {}, // not supported in CKEditor disable: function (conf) {}, // not supported in CKEditor destroy: function (conf) { var id = DataTable.Editor.safeId(conf.id); this.off('open.ckEditInit-' + id); this.off('preClose.ckEditInit-' + id); } }; }); $(document).ready(function () { var editor = new DataTable.Editor({ ajax: '../php/staff.php', table: '#example', fields: [ { label: 'First name:', name: 'first_name' }, { label: 'Last name:', name: 'last_name' }, { label: 'Position:', name: 'position', type: 'ckeditor' }, { label: 'Office:', name: 'office' }, { label: 'Extension:', name: 'extn' }, { label: 'Start date:', name: 'start_date' }, { label: 'Salary:', name: 'salary' } ] }); $('#example').on('click', 'tbody td', function () { editor.bubble(this); }); $('#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.