mootools/mootools-more — Issue #1081: HtmlTable.selectNone fails with empty table
Author: jfly
Created: 2011-12-16 11:19:22+00:00
State: open

HtmlTable.selectNone is failing when my table is empty. This appears to be due to the fact that HtmlTable.selectRange doesn't do very good bounds checking. It bounds properly bounds endRow, but doesn't deal with startRow < 0. Here's the relevant section of code:

selectRange: function(startRow, endRow, _deselect){
if (!this.options.allowMultiSelect && !_deselect) return;
var method = _deselect ? 'deselectRow' : 'selectRow',
rows = Array.clone(this.body.rows);

if (typeOf(startRow) == 'element') startRow = rows.indexOf(startRow);
if (typeOf(endRow) == 'element') endRow = rows.indexOf(endRow);
endRow = endRow < rows.length - 1 ? endRow : rows.length - 1;

if (endRow < startRow){
var tmp = startRow;
startRow = endRow;
endRow = tmp;
}

for (var i = startRow; i <= endRow; i++){
if (this.options.selectHiddenRows || rows[i].isDisplayed()) this[method](rows[i], true);
}

return this;
},

Here's my suggested fix:

selectRange: function(startRow, endRow, _deselect){
if (!this.options.allowMultiSelect && !_deselect) return;
var method = _deselect ? 'deselectRow' : 'selectRow',
rows = Array.clone(this.body.rows);

if (typeOf(startRow) == 'element') startRow = rows.indexOf(startRow);
if (typeOf(endRow) == 'element') endRow = rows.indexOf(endRow);

if (endRow < startRow){
var tmp = startRow;
startRow = endRow;
endRow = tmp;
}

var rowAfterEndRow = Math.min(endRow + 1, rows.length);
startRow = Math.max(startRow, 0);

for (var i = startRow; i < rowAfterEndRow; i++){
if (this.options.selectHiddenRows || rows[i].isDisplayed()) this[method](rows[i], true);
}

return this;
},