Skip to content

Commit 7a0a6c6

Browse files
authored
Merge pull request #7186 from prx-lmo/hotfix/4457
fix: 4457 Find or Select by Row Entity's 'ID'
2 parents 30c0743 + f317bf1 commit 7a0a6c6

File tree

4 files changed

+219
-5
lines changed

4 files changed

+219
-5
lines changed

packages/core/src/js/factories/Grid.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1106,6 +1106,65 @@ angular.module('ui.grid')
11061106
});
11071107
};
11081108

1109+
/**
1110+
* @ngdoc function
1111+
* @name getRowsByKey
1112+
* @methodOf ui.grid.class:Grid
1113+
* @description returns the GridRows who have an key that is equal to comparator
1114+
* @param {boolean} isInEntity if true then key is in entity else it's directly in row
1115+
* @param {(string|number)} key the key to look for
1116+
* @param {any} comparator the value that key should have
1117+
* @param {array} lookInRows [optional] the rows to look in - if not provided then
1118+
* looks in grid.rows
1119+
*/
1120+
Grid.prototype.getRowsByKey = function getRowsByKey(isInEntity, key, comparator, lookInRows) {
1121+
if ( key == void 0 ) {
1122+
return null;
1123+
}
1124+
1125+
lookInRows = lookInRows == void 0 ? this.rows : lookInRows;
1126+
var func = isInEntity ? function (row) {
1127+
return row.entity != void 0 && row.entity.hasOwnProperty(key) && row.entity[key] === comparator;
1128+
} : function (row) {
1129+
return row.hasOwnProperty(key) && row[key] === comparator;
1130+
}
1131+
1132+
return lookInRows.filter(func);
1133+
};
1134+
1135+
/**
1136+
* @ngdoc function
1137+
* @name findRowByKey
1138+
* @methodOf ui.grid.class:Grid
1139+
* @description returns the first GridRow which has an key that is equal to comparator
1140+
* @param {boolean} isInEntity if true then key is in entity else it's directly in row
1141+
* @param {(string|number)} key the key to look for
1142+
* @param {any} comparator the value that key should have
1143+
* @param {array} lookInRows [optional] the rows to look in - if not provided then
1144+
* looks in grid.rows
1145+
*/
1146+
Grid.prototype.findRowByKey = function findRowByKey(isInEntity, key, comparator, lookInRows) {
1147+
var result = null;
1148+
if ( key != void 0 ) {
1149+
lookInRows = lookInRows == void 0 ? this.rows : lookInRows;
1150+
var func = isInEntity ? function (row) {
1151+
if ( row.entity != void 0 && row.entity.hasOwnProperty(key) && row.entity[key] === comparator ) {
1152+
result = row;
1153+
return false;
1154+
}
1155+
return true;
1156+
} : function (row) {
1157+
if ( row.hasOwnProperty(key) && row[key] === comparator ) {
1158+
result = row;
1159+
return false;
1160+
}
1161+
return true;
1162+
}
1163+
1164+
lookInRows.every(func);
1165+
}
1166+
return result
1167+
};
11091168

11101169
/**
11111170
* @ngdoc function

packages/core/test/core/factories/Grid.spec.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,64 @@ describe('Grid factory', function() {
324324
});
325325
});
326326

327+
describe('getting Rows', function() {
328+
it('should get Rows', function() {
329+
expect(grid.getRow()).toBe(null);
330+
expect(grid.getRow(grid.rows[0].entity)).toBe(grid.rows[0]);
331+
expect(grid.getRow(grid.rows[0].entity), [grid.rows[0]]).toBe(grid.rows[0]);
332+
});
333+
334+
it('should get Rows by key', function() {
335+
grid.rows[0].entity = {str: 'abc', num: 123, nll: null, multi: true};
336+
grid.rows[1].entity = {multi: true};
337+
grid.rows[0].str = 'abc';
338+
grid.rows[0].num = 123;
339+
grid.rows[0].nll = null;
340+
grid.rows[0].innerMulti = false;
341+
grid.rows[1].innerMulti = false;
342+
343+
expect(grid.getRowsByKey()).toBe(null);
344+
expect(grid.getRowsByKey(true, "test")).toEqual([]);
345+
expect(grid.getRowsByKey(true, "str", "abc")[0].entity).toBe(grid.rows[0].entity);
346+
expect(grid.getRowsByKey(true, "str", "def")).toEqual([]);
347+
expect(grid.getRowsByKey(true, "num", 123)[0].entity).toBe(grid.rows[0].entity);
348+
expect(grid.getRowsByKey(true, "nll", null)[0].entity).toBe(grid.rows[0].entity);
349+
expect(grid.getRowsByKey(true, "multi", true).length).toBe(2);
350+
351+
expect(grid.getRowsByKey(false, "test")).toEqual([]);
352+
expect(grid.getRowsByKey(false, "str", "abc")).toEqual([grid.rows[0]]);
353+
expect(grid.getRowsByKey(false, "str", "def")).toEqual([]);
354+
expect(grid.getRowsByKey(false, "num", 123)).toEqual([grid.rows[0]]);
355+
expect(grid.getRowsByKey(false, "nll", null)).toEqual([grid.rows[0]]);
356+
expect(grid.getRowsByKey(false, "innerMulti", false).length).toBe(2);
357+
});
358+
359+
it('should find first Row by key', function() {
360+
grid.rows[0].entity = {str: 'abc', num: 123, nll: null, multi: true};
361+
grid.rows[1].entity = {multi: true};
362+
grid.rows[0].str = 'abc';
363+
grid.rows[0].num = 123;
364+
grid.rows[0].nll = null;
365+
grid.rows[0].innerMulti = false;
366+
grid.rows[1].innerMulti = false;
367+
368+
expect(grid.findRowByKey()).toBe(null);
369+
expect(grid.findRowByKey(true, "test")).toBe(null);
370+
expect(grid.findRowByKey(true, "str", "abc").entity).toBe(grid.rows[0].entity);
371+
expect(grid.findRowByKey(true, "str", "def")).toBe(null);
372+
expect(grid.findRowByKey(true, "num", 123).entity).toBe(grid.rows[0].entity);
373+
expect(grid.findRowByKey(true, "nll", null).entity).toBe(grid.rows[0].entity);
374+
expect(grid.findRowByKey(true, "multi", true).entity).toBe(grid.rows[0].entity);
375+
376+
expect(grid.findRowByKey(false, "test")).toBe(null);
377+
expect(grid.findRowByKey(false, "str", "abc")).toBe(grid.rows[0]);
378+
expect(grid.findRowByKey(false, "str", "def")).toBe(null);
379+
expect(grid.findRowByKey(false, "num", 123)).toBe(grid.rows[0]);
380+
expect(grid.findRowByKey(false, "nll", null)).toBe(grid.rows[0]);
381+
expect(grid.findRowByKey(false, "innerMulti", false).entity).toBe(grid.rows[0].entity);
382+
});
383+
})
384+
327385
describe('buildColumns', function() {
328386
it('guess correct column types when not specified', function() {
329387
var dataRow = {str: 'abc', num: 123, dat: new Date(), bool: true, obj: {}, nll: null, negNum: -1, posNum: +1};

packages/selection/src/js/selection.js

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@
201201
*/
202202
toggleRowSelection: function (rowEntity, evt) {
203203
var row = grid.getRow(rowEntity);
204-
if (row !== null) {
204+
if (row != void 0 && row !== null) {
205205
service.toggleRowSelection(grid, row, evt, grid.options.multiSelect, grid.options.noUnselect, true);
206206
}
207207
},
@@ -215,7 +215,7 @@
215215
*/
216216
selectRow: function (rowEntity, evt) {
217217
var row = grid.getRow(rowEntity);
218-
if (row !== null && !row.isSelected) {
218+
if (row != void 0 && row !== null && !row.isSelected) {
219219
service.toggleRowSelection(grid, row, evt, grid.options.multiSelect, grid.options.noUnselect, true);
220220
}
221221
},
@@ -232,7 +232,24 @@
232232
*/
233233
selectRowByVisibleIndex: function (rowNum, evt) {
234234
var row = grid.renderContainers.body.visibleRowCache[rowNum];
235-
if (row !== null && typeof (row) !== 'undefined' && !row.isSelected) {
235+
if (row != void 0 && row !== null && typeof (row) !== 'undefined' && !row.isSelected) {
236+
service.toggleRowSelection(grid, row, evt, grid.options.multiSelect, grid.options.noUnselect, false);
237+
}
238+
},
239+
/**
240+
* @ngdoc function
241+
* @name selectRowByKey
242+
* @methodOf ui.grid.selection.api:PublicApi
243+
* @description Select the data row
244+
* @param {boolean} isInEntity if true then key is in entity else it's directly in row
245+
* @param {Symbol} key the key to look for
246+
* @param {any} comparator the value that key should have
247+
* @param {Event} evt object if raised from an event
248+
* @param {array} lookInRows [optional] the rows to look in - if not provided then looks in grid.rows
249+
*/
250+
selectRowByKey: function (isInEntity, key, comparator, evt, lookInRows) {
251+
var row = grid.findRowByKey(isInEntity, key, comparator, lookInRows);
252+
if (row != void 0 && row !== null && typeof (row) !== 'undefined' && !row.isSelected) {
236253
service.toggleRowSelection(grid, row, evt, grid.options.multiSelect, grid.options.noUnselect, false);
237254
}
238255
},
@@ -246,7 +263,7 @@
246263
*/
247264
unSelectRow: function (rowEntity, evt) {
248265
var row = grid.getRow(rowEntity);
249-
if (row !== null && row.isSelected) {
266+
if (row != void 0 && row !== null && row.isSelected) {
250267
service.toggleRowSelection(grid, row, evt, grid.options.multiSelect, grid.options.noUnselect, true);
251268
}
252269
},
@@ -263,7 +280,24 @@
263280
*/
264281
unSelectRowByVisibleIndex: function (rowNum, evt) {
265282
var row = grid.renderContainers.body.visibleRowCache[rowNum];
266-
if (row !== null && typeof (row) !== 'undefined' && row.isSelected) {
283+
if (row != void 0 && row !== null && typeof (row) !== 'undefined' && row.isSelected) {
284+
service.toggleRowSelection(grid, row, evt, grid.options.multiSelect, grid.options.noUnselect, false);
285+
}
286+
},
287+
/**
288+
* @ngdoc function
289+
* @name unSelectRowByKey
290+
* @methodOf ui.grid.selection.api:PublicApi
291+
* @description Select the data row
292+
* @param {boolean} isInEntity if true then key is in entity else it's directly in row
293+
* @param {(string|number)} key the key to look for
294+
* @param {any} comparator the value that key should have
295+
* @param {Event} evt object if raised from an event
296+
* @param {array} lookInRows [optional] the rows to look in - if not provided then looks in grid.rows
297+
*/
298+
unSelectRowByKey: function (isInEntity, key, comparator, evt, lookInRows) {
299+
var row = grid.findRowByKey(isInEntity, key, comparator, lookInRows);
300+
if (row != void 0 && row !== null && typeof (row) !== 'undefined' && row.isSelected) {
267301
service.toggleRowSelection(grid, row, evt, grid.options.multiSelect, grid.options.noUnselect, false);
268302
}
269303
},

packages/selection/test/uiGridSelectionService.spec.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,69 @@ describe('ui.grid.selection uiGridSelectionService', function() {
247247
grid.api.selection.selectRow(grid.rows[4].entity);
248248
expect(grid.rows[4].isSelected).toBe(false);
249249
});
250+
251+
it('select by key then unselect rows by key in entity, including selecting rows already selected and unselecting rows not selected', function() {
252+
grid.rows[4].entity = {str: 'abc'};
253+
grid.rows[6].entity = {str: 'def'};
254+
grid.api.selection.selectRowByKey(true, "str", "abc");
255+
expect(grid.rows[4].isSelected).toBe(true);
256+
257+
grid.api.selection.selectRowByKey(true, "str", "def");
258+
expect(grid.rows[4].isSelected).toBe(true);
259+
expect(grid.rows[6].isSelected).toBe(true);
260+
261+
grid.api.selection.selectRowByKey(true, "str", "abc");
262+
expect(grid.rows[4].isSelected).toBe(true);
263+
expect(grid.rows[6].isSelected).toBe(true);
264+
265+
grid.api.selection.unSelectRowByKey(true, "str", "abc");
266+
expect(grid.rows[4].isSelected).toBe(false);
267+
expect(grid.rows[6].isSelected).toBe(true);
268+
269+
grid.api.selection.unSelectRowByKey(true, "str", "abc");
270+
expect(grid.rows[4].isSelected).toBe(false);
271+
expect(grid.rows[6].isSelected).toBe(true);
272+
273+
grid.api.selection.unSelectRowByKey(true, "str", "def");
274+
expect(grid.rows[4].isSelected).toBe(false);
275+
expect(grid.rows[6].isSelected).toBe(false);
276+
277+
grid.rows[4].enableSelection = false;
278+
grid.api.selection.selectRowByKey(true, "str", "abc");
279+
expect(grid.rows[4].isSelected).toBe(false);
280+
});
281+
282+
it('select by key then unselect rows by key outside entity, including selecting rows already selected and unselecting rows not selected', function() {
283+
grid.rows[4].str = 'abc';
284+
grid.rows[6].str = 'def';
285+
286+
grid.api.selection.selectRowByKey(false, "str", "abc");
287+
expect(grid.rows[4].isSelected).toBe(true);
288+
289+
grid.api.selection.selectRowByKey(false, "str", "def");
290+
expect(grid.rows[4].isSelected).toBe(true);
291+
expect(grid.rows[6].isSelected).toBe(true);
292+
293+
grid.api.selection.selectRowByKey(false, "str", "abc");
294+
expect(grid.rows[4].isSelected).toBe(true);
295+
expect(grid.rows[6].isSelected).toBe(true);
296+
297+
grid.api.selection.unSelectRowByKey(false, "str", "abc");
298+
expect(grid.rows[4].isSelected).toBe(false);
299+
expect(grid.rows[6].isSelected).toBe(true);
300+
301+
grid.api.selection.unSelectRowByKey(false, "str", "abc");
302+
expect(grid.rows[4].isSelected).toBe(false);
303+
expect(grid.rows[6].isSelected).toBe(true);
304+
305+
grid.api.selection.unSelectRowByKey(false, "str", "def");
306+
expect(grid.rows[4].isSelected).toBe(false);
307+
expect(grid.rows[6].isSelected).toBe(false);
308+
309+
grid.rows[4].enableSelection = false;
310+
grid.api.selection.selectRowByKey(false, "str", "abc");
311+
expect(grid.rows[4].isSelected).toBe(false);
312+
});
250313
});
251314

252315
describe('setSelected function', function() {

0 commit comments

Comments
 (0)