jQWidgets Forums
Forum Replies Created
Viewing 1 post (of 1 total)
-
Author
-
December 20, 2017 at 11:25 am in reply to: Need key down Event in Grid Need key down Event in Grid #97986
var columns = [ //... bla bla ... ]; var lastEventTimestamp; function initGrid() { var grid = $('#grid'); grid.jqxGrid('handlekeyboardnavigation', function(event) { if (!event || lastEventTimestamp == event.timeStamp) return; if (event.timeStamp) lastEventTimestamp = event.timeStamp; var cell = grid.jqxGrid('getselectedcell'); if (!cell) return; //var column = columns.find(col => col.datafield == cell.datafield); var column = grid.jqxGrid('getcolumn', cell.datafield); var key = event.charCode || event.key || event.keyCode || 0; if (typeof key == 'string') key = key.toLowerCase(); return handleKeyboardNavigation(event, key, cell, column); }); } function handleKeyboardNavigation(event, key, cell, column) { if (key == 'enter') { // Enter should be act like Tab key key = (event.key = 'Tab').toLowerCase(); event.keyCode = 9; } switch (key) { case 'ins': case 'insert': grid.jqxGrid('unselectcell', cell.rowindex, cell.datafield); grid.jqxGrid('addrow', null, newEmptyRecord(options.columns), cell.rowindex); grid.jqxGrid('selectcell', cell.rowindex, cell.datafield); return true; // true: prevents grid's default action case 'del': case 'delete': // if ctrlKey was pressed delete row(s); otherwise delete cell content and column(s) cellvaluechanging event should be fired if (event.ctrlKey) { var rowCount = grid.jqxGrid('getdatainformation').rowscount; if (rowCount) { // build rowID array and pass to deleterow method var records = grid.jqxGrid('rows').records; var cells = grid.jqxGrid('getselectedcells'); var rowIDs = []; $.each(cells, (ind, cell) => rowIDs.push(grid.jqxGrid('getrowid', cell.rowindex))); grid.jqxGrid('deleterow', rowIDs); // unselect currently selected first cell var _cell = cells[0]; grid.jqxGrid('unselectcell', _cell.rowindex, _cell.datafield); // if rowCount == 0 then add an empty row (for excel-like free data input) rowCount = grid.jqxGrid('getdatainformation').rowscount; if (!rowCount) grid.jqxGrid('addrow', null, this.newEmptyRecord(options.columns), 'bottom'); // select original first cell grid.jqxGrid('selectcell', _cell.rowindex, _cell.datafield); } return true; // true: prevents grid's default action } else { // get selected cell(s) info and save old values var cells = grid.jqxGrid('getselectedcells'); var oldCellInfos = []; $.each(cells, function(ind, _cell) { oldCellInfos.push({ cell: _cell, oldValue: grid.jqxGrid('getcellvalue', _cell.rowindex, _cell.datafield) }); }); // make cell values as empty value (0 or '') and then call related col's cellvaluechanging event // by passing oldValue and changed newValue. // async timer used to prevent bugs i seen at setcellvalue inside event ?? setTimeout(function() { $.each(oldCellInfos, function(ind, bilgi) { var _cell = bilgi.cell; var _col = (_cell.datafield == col.datafield) ? col : grid.jqxGrid('getcolumn', _cell.datafield); if (!_col || _col.editable === false) return true; // continue loop var newValue = col.defaultValue || (col.datatype == 'number' ? 0 : ''); grid.jqxGrid('setcellvalue', _cell.rowindex, _cell.datafield, newValue); if (_col.cellvaluechanging) _col.cellvaluechanging(_cell.rowindex, _cell.datafield, _col.columntype, bilgi.oldValue, newValue); }); }, 500); return true; // true: prevents grid's default action } break; } } } function raiseColumnsKeyboardNavigationHandler(selector, key, cell, column) { var handler = column[selector]; if (handler) return handler(event, key, cell, column, options); // else returns undefined (== null) } function newEmptyRecord(columns) { var record = {}; $.each(columns, function(ind, col) { record[col.datafield] = col.defaultValue ? col.defaultValue : (col.type == 'number' ? 0 : ''); }); return record; }
-
AuthorPosts
Viewing 1 post (of 1 total)