Editor example proseMirror

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; const mySchema = ProseMirror.basicSchema; var plugins = ProseMirror.exampleSetup({ schema: mySchema }); var view; _fieldTypes.proseMirror = { create: function (conf) { conf._input = $('<div class="editor" style="height:auto;"> </div>').attr( $.extend( { id: DataTable.Editor.safeId(conf.id), type: 'text' }, conf.attr || {} ) ); var state = ProseMirror.EditorState.create({ doc: ProseMirror.DOMParser.fromSchema(mySchema).parse(conf._input[0]), plugins: plugins }); view = new ProseMirror.EditorView(conf._input[0], { state: state }); return conf._input[0]; }, get: function (conf) { return JSON.stringify(view.state); }, set: function (conf, val) { var loadState; try { loadState = JSON.parse(val); loadState = ProseMirror.EditorState.fromJSON( { schema: mySchema, plugins: plugins }, loadState ); } catch (err) { var newDiv = document.createElement('div'); var newContent = document.createTextNode(val); newDiv.appendChild(newContent); loadState = ProseMirror.EditorState.create({ doc: ProseMirror.DOMParser.fromSchema(mySchema).parse(newDiv), plugins: plugins }); } view.updateState(loadState); } }; }); $(document).ready(function () { 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: 'proseMirror' }, { label: 'Office:', name: 'office' }, { label: 'Extension:', name: 'extn' }, { label: 'Start date:', name: 'start_date' }, { label: 'Salary', name: 'salary' } ] }); var tempConf = $('<div class="tempEditor"/>')[0]; var div = $('<div class ="div"/>'); const mySchema = ProseMirror.basicSchema; var plugins = ProseMirror.exampleSetup({ schema: mySchema }); var state = ProseMirror.EditorState.create({ doc: ProseMirror.DOMParser.fromSchema(mySchema).parse(tempConf), plugins: plugins }); var view = new ProseMirror.EditorView(tempConf, { state: state }); $('#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', render: function (data, type, row) { try { var domSer = ProseMirror.DOMSerializer.fromSchema(mySchema); var parData = JSON.parse(data); parData = ProseMirror.EditorState.fromJSON( { schema: mySchema, plugins: plugins }, parData ); var dom = domSer.serializeFragment(parData.doc.content); div.empty().append(dom); return div.html(); } catch (err) { return 'This is invalid JSON ' + data; } } }, { data: 'office' }, { data: 'extn' }, { data: 'start_date' }, { data: 'salary' } ], 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.