Unified
Split
Showing
with
55 additions
and 7 deletions.
- +24 −7 src/features/selection/js/selection.js
- +31 −0 src/features/selection/test/uiGridSelectionService.spec.js
| @@ -93,7 +93,7 @@ | ||
| */ | ||
| toggleRowSelection: function (rowEntity, evt) { | ||
| var row = grid.getRow(rowEntity); | ||
| if (row !== null) { | ||
| if (row !== null && row.enableSelection !== false) { | ||
| service.toggleRowSelection(grid, row, evt, grid.options.multiSelect, grid.options.noUnselect); | ||
| } | ||
| }, | ||
| @@ -107,7 +107,7 @@ | ||
| */ | ||
| selectRow: function (rowEntity, evt) { | ||
| var row = grid.getRow(rowEntity); | ||
| if (row !== null && !row.isSelected) { | ||
| if (row !== null && !row.isSelected && row.enableSelection !== false) { | ||
| service.toggleRowSelection(grid, row, evt, grid.options.multiSelect, grid.options.noUnselect); | ||
| } | ||
| }, | ||
| @@ -124,7 +124,7 @@ | ||
| */ | ||
| selectRowByVisibleIndex: function ( rowNum, evt ) { | ||
| var row = grid.renderContainers.body.visibleRowCache[rowNum]; | ||
| if (row !== null && typeof(row) !== 'undefined' && !row.isSelected) { | ||
| if (row !== null && typeof(row) !== 'undefined' && !row.isSelected && row.enableSelection !== false) { | ||
| service.toggleRowSelection(grid, row, evt, grid.options.multiSelect, grid.options.noUnselect); | ||
| } | ||
| }, | ||
| @@ -156,7 +156,7 @@ | ||
|
|
||
| var changedRows = []; | ||
| grid.rows.forEach(function (row) { | ||
| if ( !row.isSelected ){ | ||
| if ( !row.isSelected && row.enableSelection !== false ){ | ||
| row.isSelected = true; | ||
| service.decideRaiseSelectionEvent( grid, row, changedRows, evt ); | ||
| } | ||
| @@ -179,7 +179,7 @@ | ||
| var changedRows = []; | ||
| grid.rows.forEach(function (row) { | ||
| if (row.visible) { | ||
| if (!row.isSelected){ | ||
| if (!row.isSelected && row.enableSelection !== false){ | ||
| row.isSelected = true; | ||
| service.decideRaiseSelectionEvent( grid, row, changedRows, evt ); | ||
| } | ||
| @@ -276,6 +276,23 @@ | ||
| * set using the ui-grid {@link ui.grid.class:GridOptions gridOptions} | ||
| */ | ||
|
|
||
| /** | ||
| * @ngdoc object | ||
| * @name ui.grid.selection.api:GridRow | ||
| * | ||
| * @description GridRow options for selection feature | ||
| */ | ||
| /** | ||
| * @ngdoc object | ||
| * @name enableSelection | ||
| * @propertyOf ui.grid.selection.api:GridRow | ||
| * @description Enable row selection for this row, only settable by internal code. | ||
| * | ||
| * The grouping feature, for example, might set group header rows to not be selectable. | ||
| * <br/>Defaults to true | ||
| */ | ||
|
|
||
|
|
||
| /** | ||
| * @ngdoc object | ||
| * @name enableRowSelection | ||
| @@ -374,7 +391,7 @@ | ||
|
|
||
| if (selected && noUnselect){ | ||
| // don't deselect the row | ||
| } else { | ||
| } else if (row.enableSelection !== false) { | ||
| row.isSelected = !selected; | ||
| if (row.isSelected === true) { | ||
| grid.selection.lastSelectedRow = row; | ||
| @@ -412,7 +429,7 @@ | ||
| for (var i = fromRow; i <= toRow; i++) { | ||
| var rowToSelect = grid.renderContainers.body.visibleRowCache[i]; | ||
| if (rowToSelect) { | ||
| if ( !rowToSelect.isSelected ){ | ||
| if ( !rowToSelect.isSelected && rowToSelect.enableSelection !== false ){ | ||
| rowToSelect.isSelected = true; | ||
| grid.selection.lastSelectedRow = rowToSelect; | ||
| service.decideRaiseSelectionEvent( grid, rowToSelect, changedRows, evt ); | ||
| @@ -52,6 +52,12 @@ describe('ui.grid.selection uiGridSelectionService', function () { | ||
| expect(grid.rows[1].isSelected).toBe(true); | ||
| }); | ||
|
|
||
| it('should not toggle selected with enableSelection: false', function () { | ||
| grid.rows[0].enableSelection = false; | ||
| uiGridSelectionService.toggleRowSelection(grid, grid.rows[0], null, true); | ||
| expect(grid.rows[0].isSelected).toBe(undefined); | ||
| }); | ||
|
|
||
| it('should toggle selected with noUnselect', function () { | ||
| uiGridSelectionService.toggleRowSelection(grid, grid.rows[0], null, false, true); | ||
| expect(grid.rows[0].isSelected).toBe(true, 'row should be selected, noUnselect doesn\'t stop rows being selected'); | ||
| @@ -104,6 +110,16 @@ describe('ui.grid.selection uiGridSelectionService', function () { | ||
| expect(grid.rows[5].isSelected).toBe(true); | ||
| }); | ||
|
|
||
| it('should skip non-selectable rows', function () { | ||
| grid.rows[4].enableSelection = false; | ||
| grid.api.selection.toggleRowSelection(grid.rows[2].entity); | ||
| uiGridSelectionService.shiftSelect(grid, grid.rows[5], null, true); | ||
| expect(grid.rows[2].isSelected).toBe(true); | ||
| expect(grid.rows[3].isSelected).toBe(true); | ||
| expect(grid.rows[4].isSelected).toBe(undefined); | ||
| expect(grid.rows[5].isSelected).toBe(true); | ||
| }); | ||
|
|
||
| it('should reverse selection order if from is bigger then to', function () { | ||
| grid.api.selection.toggleRowSelection(grid.rows[5].entity); | ||
| uiGridSelectionService.shiftSelect(grid, grid.rows[2], null, true); | ||
| @@ -143,6 +159,10 @@ describe('ui.grid.selection uiGridSelectionService', function () { | ||
| grid.api.selection.unSelectRow(grid.rows[6].entity); | ||
| expect(grid.rows[4].isSelected).toBe(false); | ||
| expect(grid.rows[6].isSelected).toBe(false); | ||
|
|
||
| grid.rows[4].enableSelection = false; | ||
| grid.api.selection.selectRow(grid.rows[4].entity); | ||
| expect(grid.rows[4].isSelected).toBe(false); | ||
| }); | ||
| }); | ||
|
|
||
| @@ -174,6 +194,11 @@ describe('ui.grid.selection uiGridSelectionService', function () { | ||
| expect(grid.rows[i].isSelected).toBe(false); | ||
| } | ||
| expect(grid.selection.selectAll).toBe(false); | ||
|
|
||
| grid.rows[8].enableSelection = false; | ||
| grid.api.selection.selectAllRows(); | ||
| expect(grid.rows[7].isSelected).toBe(true); | ||
| expect(grid.rows[8].isSelected).toBe(false); | ||
| }); | ||
| }); | ||
|
|
||
| @@ -203,6 +228,7 @@ describe('ui.grid.selection uiGridSelectionService', function () { | ||
| grid.rows[4].visible = true; | ||
| grid.rows[6].visible = false; | ||
| grid.rows[7].visible = true; | ||
| grid.rows[8].enableSelection = false; | ||
| grid.rows[9].visible = true; | ||
| expect(grid.selection.selectAll).toBe(false); | ||
|
|
||
| @@ -211,6 +237,7 @@ describe('ui.grid.selection uiGridSelectionService', function () { | ||
| expect(grid.rows[4].isSelected).toBe(true); | ||
| expect(grid.rows[6].isSelected).toBe(false); | ||
| expect(grid.rows[7].isSelected).toBe(true); | ||
| expect(grid.rows[8].isSelected).toBe(undefined); | ||
| expect(grid.rows[9].isSelected).toBe(true); | ||
| expect(grid.selection.selectAll).toBe(true); | ||
| }); | ||
| @@ -226,6 +253,10 @@ describe('ui.grid.selection uiGridSelectionService', function () { | ||
|
|
||
| grid.api.selection.selectRowByVisibleIndex(1); | ||
| expect(grid.rows[2].isSelected).toBe(true); | ||
|
|
||
| grid.rows[3].enableSelection = false; | ||
| grid.api.selection.selectRowByVisibleIndex(2); | ||
| expect(grid.rows[3].isSelected).toBe(undefined); | ||
| }); | ||
| }); | ||
|
|
||