From 05f05c4ee1c6d16cbef60afac2a921d203f5674a Mon Sep 17 00:00:00 2001 From: Silvio Date: Sat, 5 Aug 2017 10:18:21 -0300 Subject: [PATCH 1/2] if onUpdate returns a promise object, last actions of updateEditableCell are executed after it's resolved. --- js/dataTables.cellEdit.js | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/js/dataTables.cellEdit.js b/js/dataTables.cellEdit.js index 4ad1e7c..c76baef 100644 --- a/js/dataTables.cellEdit.js +++ b/js/dataTables.cellEdit.js @@ -33,6 +33,7 @@ jQuery.fn.dataTable.Api.register('MakeCellsEditable()', function (settings) { var cell = table.cell($(callingElement).parent()); var columnIndex = cell.index().column; var inputField =getInputField(callingElement); + var returnValue; // Update var newValue = inputField.val(); @@ -41,7 +42,7 @@ jQuery.fn.dataTable.Api.register('MakeCellsEditable()', function (settings) { if (settings.allowNulls.columns) { // If current column allows nulls if (settings.allowNulls.columns.indexOf(columnIndex) > -1) { - _update(newValue); + returnValue = _update(newValue); } else { _addValidationCss(); } @@ -51,7 +52,7 @@ jQuery.fn.dataTable.Api.register('MakeCellsEditable()', function (settings) { } //All columns allow null } else { - _update(newValue); + returnValue = _update(newValue); } function _addValidationCss() { // Show validation error @@ -65,13 +66,24 @@ jQuery.fn.dataTable.Api.register('MakeCellsEditable()', function (settings) { var oldValue = cell.data(); cell.data(newValue); //Return cell & row. - settings.onUpdate(cell, row, oldValue); + return settings.onUpdate(cell, row, oldValue); } - // Get current page - var currentPageIndex = table.page.info().page; - - //Redraw table - table.page(currentPageIndex).draw(false); + var _lastActions = function(){ + // Get current page + var currentPageIndex = table.page.info().page; + + //Redraw table + table.page(currentPageIndex).draw(false); + } + + // If returnValue has then method, assume it's a promise + if( "object" === typeof returnValue && "function" === typeof returnValue.then ){ + returnValue.then(function(){ + _lastActions(); + }); + } + else _lastActions(); + }, // CANCEL cancelEditableCell: function (callingElement) { From c7b19e435d52e6780f6288a84c9a3409fe32e60d Mon Sep 17 00:00:00 2001 From: Silvio Date: Sat, 5 Aug 2017 10:18:53 -0300 Subject: [PATCH 2/2] Update onUpdate setting documentation --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 294cedb..0d03364 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ This plugin allows cells within a [DataTable](https://datatables.net/) to be edi ##### Settings { JSON Object } Property | Type | Default | Example | Details :------ | :------ | :------ | :-----| :------ -**onUpdate** | function | | ```function(cell, row, oldValue){ } ``` | The call back function to be executed. The updated **[cell](https://datatables.net/reference/api/cell())**, **[row](https://datatables.net/reference/api/row())**, and previous value in that cell are passed as arguements. +**onUpdate** | function | | ```function(cell, row, oldValue){ } ``` | The call back function to be executed. The updated **[cell](https://datatables.net/reference/api/cell())**, **[row](https://datatables.net/reference/api/row())**, and previous value in that cell are passed as arguements. This function can return a jQuery promise object. In such case, the table will be redraw when the promise is resolved. **inputCss** _(optional)_| string | none |```'my-css-class'```| A CSS class that will be applied to the input field **columns** _(optional)_| array | All columns |```[0,1,3,4]```| An array of column indexes defining the columns that you want to be editable. **allowNulls** _(optional)_| object | false | ```{ "columns": [4], "errorClass":"my-error"}``` | Determines which columns should allow null values to be entered and what CSS to apply if user input fails validation. If **errorClass** is null a default error class will be applied.