From ff4a3202fd4078bd0379641e23bf12393585664c Mon Sep 17 00:00:00 2001 From: LukasK13 Date: Thu, 22 Feb 2018 21:37:25 -0800 Subject: [PATCH 1/6] extended editor capabilities I extendedn the editor capabilities, so distinct columns can be marked as editable and the type of the editor can be switched between a textinput and a selectinput. --- .gitignore | 3 + inst/htmlwidgets/datatables.js | 107 ++++++++++++++++++++++++++------- 2 files changed, 89 insertions(+), 21 deletions(-) diff --git a/.gitignore b/.gitignore index 3151c335..8ee44718 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,6 @@ large.txt inst/examples/random.R .*.Rnb.cached revdep/ +inst/.DS_Store +.DS_Store +inst/examples/DT-edit/app.R diff --git a/inst/htmlwidgets/datatables.js b/inst/htmlwidgets/datatables.js index d66752c9..ce307985 100644 --- a/inst/htmlwidgets/datatables.js +++ b/inst/htmlwidgets/datatables.js @@ -682,29 +682,94 @@ HTMLWidgets.widget({ // run the callback function on the table instance if (typeof data.callback === 'function') data.callback(table); - // double click to edit the cell - if (data.editable) table.on('dblclick.dt', 'tbody td', function() { - var $input = $(''); - var $this = $(this), value = table.cell(this).data(), html = $this.html(); - var changed = false; - $input.val(value); - $this.empty().append($input); - $input.css('width', '100%').focus().on('change', function() { - changed = true; - var valueNew = $input.val(); - if (valueNew != value) { - table.cell($this).data(valueNew); - if (HTMLWidgets.shinyMode) changeInput('cell_edit', cellInfo($this)); - // for server-side processing, users have to call replaceData() to update the table - if (!server) table.draw(false); - } else { - $this.html(html); + // editor is enabled + if (table.init().editable) { + var editorNextCell = null; // declare variable for next cell to be acivated by the tab key + var options = table.init(); // load table options + for (var key in options.editType) { + $(table.column(key).header()).attr("data-editortype", options.editType[key]).attr("data-editoroptions", JSON.stringify(options.editAttribs[key])); // set column editor attributes + } + + // double click to edit the cell + table.on('dblclick.dt', 'tbody td', function() { + if (table.column(this).header().hasAttribute('data-editortype')) { // cell is marked as editable + var $this = $(this), value = table.cell(this).data(), html = $this.html(); + var changed = false; + if (table.column(this).header().getAttribute('data-editortype') == 'text') { // cell shall display a textinput + var $input = $(''); + $input.val(value); + $input.attr("placeholder", JSON.parse(table.column(this).header().getAttribute("data-editoroptions")).placeholder); + } else if (table.column(this).header().getAttribute('data-editortype') == 'select') { // cell shall display a selectinput + var $input = $(''); $input.val(value); - $input.attr("placeholder", JSON.parse(table.column(this).header().getAttribute("data-editoroptions")).placeholder); + $input.attr('placeholder', JSON.parse(table.column(this).header().getAttribute('data-editoroptions')).placeholder); } else if (table.column(this).header().getAttribute('data-editortype') == 'select') { // cell shall display a selectinput var $input = $('