Standalone collection editor

While Editor is primarily used as an Editor for DataTables when considering multiple records, its standalone mode also supports the ability to edit individual items in a collection that have been displayed on the page without using DataTables. Consider the example below where data is read from the database about staff members and each record is shown in a panel. While DataTables could of course be used here, this approach adds the ability to show the data in ways that is not possible in a table.

The key to this approach is that each panel uses the data attribute data-editor-id to specify the unique identifier that each panel belongs to. The data-editor-field attribute is then used for the elements in the panel to specify which field each element should represent.

The create(), edit() and remove() API methods are all used to provide full CRUD options for this collection editor.

For additional information about using Editor without DataTables, please refer to the Editor standalone manual.

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

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

// Template function to display the information panels. Editor will // automatically keep the values up-to-date with any changes due to the use of // the `data-editor-field` attribute. It knows which panel to update for each // record through the use of `data-editor-id` in the container element. function createPanel(data) { var id = data.DT_RowId; $( '<div class="panel" data-editor-id="' + id + '">' + '<i class="edit fa fa-pencil" data-id="' + id + '"></i>' + '<i class="remove fa fa-times" data-id="' + id + '"></i>' + '<dl>' + '<dt>Name:</dt>' + '<dd>' + '<span data-editor-field="first_name">' + data.first_name + '</span> ' + '<span data-editor-field="last_name">' + data.last_name + '</span>' + '</dd>' + '<dt>Position:</dt>' + '<dd data-editor-field="position">' + data.position + '</dd>' + '<dt>Office:</dt>' + '<dd data-editor-field="office">' + data.office + '</dd>' + '</dl>' + '</div>' ).appendTo('#panels'); } var editor = new DataTable.Editor({ ajax: '../php/staff.php', fields: [ { label: 'First name:', name: 'first_name' }, { label: 'Last name:', name: 'last_name' }, { label: 'Position:', name: 'position' }, { label: 'Office:', name: 'office' } ] }); // Create record - on create we insert a new panel editor.on('postCreate', function (e, json) { createPanel(json.data[0]); }); $('button.create').on('click', function () { editor .title('Create new record') .buttons('Create') .create(); }); // Edit $('#panels').on('click', 'i.edit', function () { editor .title('Edit record') .buttons('Save changes') .edit($(this).data('id')); }); // Remove $('#panels').on('click', 'i.remove', function () { editor .title('Delete record') .buttons('Delete') .message('Are you sure you wish to delete this record?') .remove($(this).data('id')); }); // Load the initial data and display in panels $.ajax({ url: '../php/staff.php', dataType: 'json', success: function (json) { for (var i = 0, ien = json.data.length; i < ien; i++) { createPanel(json.data[i]); } } });

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.create, div.panel { position: relative; box-sizing: border-box; float: left; width: 23%; border: 1px solid #ccc; border-radius: 3px; background-color: #eee; min-height: 105px; padding: 5px; margin: 1em 2% 0 0; } button.create:hover { background-color: #ddd; } div.panel i.edit, div.panel i.remove { position: absolute; bottom: 5px; right: 5px; color: #999; } div.panel i.remove { right: 25px; } div.panel i.edit:hover, div.panel i.remove:hover { color: black; } div.panel dl { margin: 0; } div.panel dt { clear: both; float: left; width: 33%; padding-left: 2%; margin: 0; color: #999; } div.panel dd { float: left; width: 65%; margin: 0; } html.dark button.create, html.dark div.panel { border: 1px solid rgba(255, 255, 255, 0.15); border-radius: 3px; background-color: var(--dt-html-background); } html.dark button.create { color: white; } html.dark button.create:hover { background-color: rgba(255, 255, 255, 0.05); } html.dark div.panel i.edit:hover, html.dark div.panel i.remove:hover { color: white; }

    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 interaction for this demo is shown below. This server uses PHP, so the PHP script is shown, however our download packages include the equivalent script for other platforms, including .NET and Node.js. Server-side scripts can be written in any language, using the protocol described in the Editor documentation.

      Other examples