# Editor example Ajax override - using localStorage for the data source

This example shows how the `ajaxE` initialisation option can be used to replace the default Ajax call that Editor makes and instead use the browser's `localStorage` abilities to save the state of the table locally on the browser. This means that the user effectively has persistent storage, but it is available only to them on their current browser.

The code in this example shows the `ajaxE` option as a function that effectively implements everything that is required by Editor for data storage and retrieval. The 'create', 'edit' and 'remove' actions are each handled by manipulating the JSON data array that is stored in the browser's `localStorage`.

Although this particular use case is fairly limited, it does show how Editor's `ajaxE` option can be used to intercept and manage the data requests that Editor makes so almost any data storage system could be used from Firebase to WebSockets.

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

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

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

`var editor; // use a global for the submit and return data rendering in the examples $(document).ready(function() { // Create or update the todo localStorage entry if ( !localStorage.getItem('datatable_todo') ) { localStorage.setItem('datatable_todo', '[]'); } else { // Loop over the array, removing any nulls from previous deletes on init var a = JSON.parse( localStorage.getItem('datatable_todo') ); for ( var i=a.length-1 ; i>=0 ; i-- ) { if ( a[i] === null ) { a.splice( i, 1 ); } } localStorage.setItem('datatable_todo', JSON.stringify(a)); } // Set up the editor editor = new $.fn.dataTable.Editor( { table: "#example", fields: [ { label: "Item:", name: "item" }, { label: "Status:", name: "status", type: "radio", def: "To do", ipOpts: [ { label: "To do", value: "To do" }, { label: "Done", value: "Done" } ] } ], ajax: function ( method, url, data, successCallback, errorCallback ) { var id = null; var store = JSON.parse( localStorage.getItem('datatable_todo') ); if ( data.action === 'create' ) { store.push( { "DT_RowId": 'row_'+store.length, "item": data.data.item, "status": data.data.status } ); id = 'row_'+(store.length-1); } else if ( data.action === 'edit' ) { var index = findIndex( store, data.id ); store[index].item = data.data.item; store[index].status = data.data.status; id = data.id; } else if ( data.action === 'remove' ) { for ( var i=0, iLen=data.id.length ; i<iLen ; i++ ) { var index = findIndex( store, data.id[i] ); if ( index >= 0 ) { store[index] = null; // Don't upset the indexes } } } localStorage.setItem('datatable_todo', JSON.stringify(store)); successCallback( {"id": id} ); } } ); // Initialise the DataTable $('#example').dataTable( { dom: "Tfrtip", data: JSON.parse( localStorage.getItem('datatable_todo') ), columns: [ { data: "item" }, { data: "status" } ], tableTools: { sRowSelect: "os", aButtons: [ { sExtends: "editor_create", editor: editor }, { sExtends: "editor_edit", editor: editor }, { sExtends: "editor_remove", editor: editor } ] } } ); } ); function findIndex( store, id ) { for ( var i=0, iLen=store.length ; i<iLen ; i++ ) { if ( store[i] && store[i].DT_RowId === id ) { return i; } } return -1; }`

In addition to the above code, the following Javascript library files are loaded for use in this example:

- ../../../../media/js/jquery.js
- ../../../../media/js/jquery.dataTables.js
- ../../../TableTools/js/dataTables.tableTools.js
- ../../js/dataTables.editor.js

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:

- ../../../../media/css/jquery.dataTables.css
- ../../../TableTools/css/dataTables.tableTools.css
- ../../css/dataTables.editor.css

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.