Skip to content

Commit 3dc24a7

Browse files
committed
[hotfix/4457]
- added methods: getRowsByKey, findRowByKey, selectRowByKey, unSelectRowByKey
1 parent 863c820 commit 3dc24a7

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1093,6 +1093,50 @@ angular.module('ui.grid')
10931093
return rows.length > 0 ? rows[0] : null;
10941094
};
10951095

1096+
/**
1097+
* @ngdoc function
1098+
* @name getRowsByKey
1099+
* @methodOf ui.grid.class:Grid
1100+
* @description returns the GridRows who have an key that is equal to comparator
1101+
* @param {boolean} isInEntity if true then key is in entity else it's directly in row
1102+
* @param {(string|number)} key the key to look for
1103+
* @param {any} comparator the value that key should have
1104+
* @param {array} lookInRows [optional] the rows to look in - if not provided then
1105+
* looks in grid.rows
1106+
*/
1107+
Grid.prototype.getRowsByKey = function getRowsByKey(isInEntity, key, comparator, lookInRows) {
1108+
lookInRows = lookInRows == void 0 ? this.rows : lookInRows;
1109+
var func = isInEntity ? function (row) {
1110+
return row.entity != void 0 && row.entity[key] === comparator;
1111+
} : function (row) {
1112+
return row[key] === comparator;
1113+
}
1114+
1115+
return lookInRows.filter(func);
1116+
};
1117+
1118+
/**
1119+
* @ngdoc function
1120+
* @name findRowByKey
1121+
* @methodOf ui.grid.class:Grid
1122+
* @description returns the first GridRow which has an key that is equal to comparator
1123+
* @param {boolean} isInEntity if true then key is in entity else it's directly in row
1124+
* @param {(string|number)} key the key to look for
1125+
* @param {any} comparator the value that key should have
1126+
* @param {array} lookInRows [optional] the rows to look in - if not provided then
1127+
* looks in grid.rows
1128+
*/
1129+
Grid.prototype.findRowByKey = function findRowByKey(isInEntity, key, comparator, lookInRows) {
1130+
lookInRows = lookInRows == void 0 ? this.rows : lookInRows;
1131+
var func = isInEntity ? function (row) {
1132+
return row.entity != void 0 && row.entity[key] === comparator;
1133+
} : function (row) {
1134+
return row[key] === comparator;
1135+
}
1136+
1137+
rows = lookInRows.filter(func);
1138+
return rows.length > 0 ? rows[0] : null;
1139+
};
10961140

10971141
/**
10981142
* @ngdoc function

packages/selection/src/js/selection.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,23 @@
236236
service.toggleRowSelection(grid, row, evt, grid.options.multiSelect, grid.options.noUnselect);
237237
}
238238
},
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 !== null && !row.isSelected) {
253+
service.toggleRowSelection(grid, row, evt, grid.options.multiSelect, grid.options.noUnselect);
254+
}
255+
},
239256
/**
240257
* @ngdoc function
241258
* @name unSelectRow
@@ -267,6 +284,23 @@
267284
service.toggleRowSelection(grid, row, evt, grid.options.multiSelect, grid.options.noUnselect);
268285
}
269286
},
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 !== null && !row.isSelected) {
301+
service.toggleRowSelection(grid, row, evt, grid.options.multiSelect, grid.options.noUnselect);
302+
}
303+
},
270304
/**
271305
* @ngdoc function
272306
* @name selectAllRows

0 commit comments

Comments
 (0)