Skip to contents

DT Javascript callback to make edit keys mimic the navigation and cell editing keys of LibreCalc/Excel

Usage

cdr_js_edit_ctrl()

Value

javascript for DT::datatable()

Examples

cdr_js_edit_ctrl()
#> 
#>    --Running: cdr_js_edit_ctrl()
#> [1] "// If you're on a cell and key 'Enter' or 'F2', enter edit mode\ntable.on('key', function(e, datatable, key, cell){\nconsole.log(key);\n  if ( key === 113 || key === 13 ){\n    $(cell.node()).trigger('dblclick.dt');\n  }\n});\ntable.on('keydown', 'td', function(e){\nconsole.log(e.which);\n// If you're editing a cell and press 'Tab, F2, up or down' to enter the new data\n  if (e.target.localName == 'input' && [9,38,40,113].indexOf(e.keyCode) > -1){\n    $(e.target).trigger('blur');\n// If you're editing a cell and press 'left, right, home, or end', perform those jumps within the cell text\n  } else if (e.target.localName == 'input' && [35,36,37,39].indexOf(e.keyCode) > -1) {\n    e.stopPropagation();\n// If you're editing a cell and then press 'escape', remove any User changes\n  } else if (e.target.localName == 'input' && e.keyCode === 27) {\n    e.stopPropagation();\n    var prior_val = table.cell( this ).data();\n    $(e.target).val( prior_val );\n    $(e.target).trigger('blur');\n// If you're editing a cell and press 'enter', mimic the downarrow\n  } else if (e.target.localName == 'input' && e.keyCode === 13 ) {\n    e.stopPropagation();\n    $(e.target).trigger('blur');\n  }\n});"
#> attr(,"class")
#> [1] "JS_EVAL"