Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/js/modules/SelectRow/SelectRow.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ export default class SelectRow extends Module{
if(Array.isArray(rowMatch)){
if(rowMatch.length){
rowMatch.forEach((row) => {
change = this._selectRow(row, true, true);
change = this._selectRow(row, true);

if(change){
changes.push(change);
Expand All @@ -269,11 +269,11 @@ export default class SelectRow extends Module{
}
}else{
if(rowMatch){
this._selectRow(rowMatch, false, true);
this._selectRow(rowMatch, false);
}
}
}
}

//select an individual row
_selectRow(rowInfo, silent, force){
//handle max row count
Expand Down
47 changes: 47 additions & 0 deletions test/unit/modules/SelectRow.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,53 @@ describe("SelectRow module", () => {
expect(result).toBe(false);
});

it("should enforce selectableRows limit when selectRows is called (issue #4881)", () => {
// Reproduces https://github.com/tabulator-tables/tabulator/issues/4881
// selectRow (public API) was ignoring the selectableRows limit because
// selectRows() passed force=true to _selectRow, bypassing the cap.

// Limit to a single selected row, no rolling reselection assumptions needed
mockTable.options.selectableRows = 1;
mockTable.options.selectableRowsRollingSelection = true;

selectRowMod.selectedRows = [];

mockTable.rowManager.findRow.mockImplementation((arg) => {
if (typeof arg === 'object' && arg !== null) return arg;
return mockRows.find(r => r.data.id === arg);
});

// Call selectRow for each row individually, mirroring the jsfiddle in the issue
selectRowMod.selectRows(mockRows[0]);
selectRowMod.selectRows(mockRows[1]);
selectRowMod.selectRows(mockRows[2]);

// With selectableRows = 1, no more than one row should ever be selected
expect(selectRowMod.selectedRows.length).toBe(1);
// With rolling selection on, the most recently requested row should be the one selected
expect(selectRowMod.selectedRows).toContain(mockRows[2]);
expect(selectRowMod.selectedRows).not.toContain(mockRows[0]);
expect(selectRowMod.selectedRows).not.toContain(mockRows[1]);
});

it("should not exceed selectableRows limit when selectRows is passed an array (issue #4881)", () => {
mockTable.options.selectableRows = 2;
mockTable.options.selectableRowsRollingSelection = false;

selectRowMod.selectedRows = [];

mockTable.rowManager.findRow.mockImplementation((arg) => {
if (typeof arg === 'object' && arg !== null) return arg;
return mockRows.find(r => r.data.id === arg);
});

// Pass all three rows at once; only the first two should be selected
selectRowMod.selectRows([mockRows[0], mockRows[1], mockRows[2]]);

expect(selectRowMod.selectedRows.length).toBe(2);
expect(selectRowMod.selectedRows).not.toContain(mockRows[2]);
});

it("should handle rolling selection when max rows is reached", () => {
// Set maximum of 2 selectable rows with rolling selection
mockTable.options.selectableRows = 2;
Expand Down
Loading