Skip to content

Instantly share code, notes, and snippets.

@gpalazuelosg
Created June 19, 2018 06:30
Show Gist options
  • Save gpalazuelosg/b0e0869dd1871c7dce4ab1cef7c9846d to your computer and use it in GitHub Desktop.
Save gpalazuelosg/b0e0869dd1871c7dce4ab1cef7c9846d to your computer and use it in GitHub Desktop.
fix for jexcel.1.5.0.js, so ondeleterow can return proper and useful values
/**
* Delete a row by number
*
* @param integer rowNumber - row number show be excluded
* @param integer numOfRows - number of lines
* @return void
*/
deleteRow : function(rowNumber, numOfRows) {
// Id
var id = $(this).prop('id');
// console log added by GP
console.log('numOfRows: ' + numOfRows + ', rowNumber: ' + rowNumber + ', id: ' + id);
// Main configuration
var options = $.fn.jexcel.defaults[id];
// Global Configuration
if (options.allowDeleteRow == true) {
// Keep history
var records = [];
// Delete row definitions
if (rowNumber == undefined) {
var number = $(this).jexcel('getSelectedRows');
if (! number[0]) {
rowNumber = $.fn.jexcel.defaults[id].data.length - 1;
numOfRows = 1;
} else {
rowNumber = parseInt(number[0]);
numOfRows = parseInt(number.length);
}
rowNumber = parseInt(number[0]);
numOfRows = parseInt(number.length);
}
// Remove the last column
if (rowNumber == -1) {
rowNumber = $.fn.jexcel.defaults[id].data.length - 1;
}
if (numOfRows != parseInt(numOfRows)) {
numOfRows = 1;
}
// Can't delete more than the limit of the table
if (numOfRows > options.data.length - rowNumber) {
numOfRows = options.data.length - numOfRows;
}
// console log added by GP
console.log('numOfRows: ' + numOfRows + ', rowNumber: ' + rowNumber + ', id: ' + id);
// Can't remove the last row
if (options.data.length > 1) {
if (parseInt(rowNumber) > -1) {
// Test
var isRowRemoved = false;
// added by GP
var iterationsIndex = 0;
// Remove rows
for (var row = rowNumber; row < rowNumber + numOfRows; row++) {
// Keep row data
// console log added by GP
//records[row] = $.fn.jexcel.defaults[id].data[row];
// added by GP
records[iterationsIndex] = $.fn.jexcel.defaults[id].data[row];
// Remove visualy
var line = $(this).find('#row-' + row);
// The current row was excluded
if ($(line).hasClass('selected')) {
isRowRemoved = true;
}
$(line).remove();
// added by GP
iterationsIndex++;
}
// Remove from source
$.fn.jexcel.defaults[id].data.splice(parseInt(rowNumber), numOfRows);
// Remove table references
$(this).jexcel('updateTableReferences', 0, rowNumber);
// Select first row any of the selected rows were excluded
if (isRowRemoved) {
// Get cell objects
var o1 = $(this).find('#0-0');
var o2 = $(this).find('#0-0');
// Update selection
$(this).jexcel('updateSelection', o1, o2);
}
// Keeping history of changes
$(this).jexcel('setHistory', null, {
type:'deleteRow',
rowNumber: rowNumber,
numOfRows: numOfRows,
rowData: records
});
// Delete events
if ($.fn.jexcel.ignoreEvents != true) {
if (typeof(options.ondeleterow) == 'function') {
// commented out by GP
//options.ondeleterow($(this));
// added by GP
options.ondeleterow($(this), rowNumber, numOfRows, records);
}
}
}
} else {
console.error('It is not possible to delete the last row');
}
}
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment