Working with row selection in Infragistics WebHierarchicalDataGrid can be complicated since it maintains independent row selection collection for each row island and basic operations (select/unselect) aren’t that obvious. I put together a few functions to make it a little bit easier.
This first function is very basic, it returns total count of selected rows across all expanded row islands, no matter what depth. The key here is get_selectedRowsResolved() method of Selection behavior which returns an array of all selected rows:
// Returns number of selected rows from all rowislands in WebHiearchicalDataGrid // i_oGrid: Infragistics WebHiearchicalDataGrid object function selRowsCount(i_oGrid) { return i_oGrid.get_gridView().get_behaviors().get_selection().get_selectedRowsResolved().length }
The following function loops thru all selected rows, collecting values from specific column (useful for example for collecting IDs of selected rows). It accepts 3 parameters – WebHiearchicalDataGrid object, column key and separator character and returns a string in which selected values are separated by that character (if nothing is passed – default separator is comma). The function assumes that the column with this key exist at every level of hierarchy:
// Return list values from selected rows of Infragistics WebHiearchicalDataGrid // i_oGrid: Infragistics WebHiearchicalDataGrid object // i_sColumnKey: Key of column from which cells to extract values // i_sSeparator: Separator between values in output string function selRowsToList(i_oGrid, i_sColumnKey, i_sSeparator) { var sOutput = ''; if (typeof (i_sSeparator) == 'undefined') i_sSeparator = ','; //Getting array of ALL selected rows accross all rowislands var aSelectedRows = i_oGrid.get_gridView().get_behaviors().get_selection().get_selectedRowsResolved(); var sCellValue; //looping thru ALL selected rows collecting values for (I = 0; I < aSelectedRows.length; I++) { //getting cell value sCellValue = aSelectedRows[I].get_cellByColumnKey(i_sColumnKey).get_text(); sOutput += sCellValue; if (I < aSelectedRows.length - 1) sOutput += i_sSeparator } return sOutput }
The next function does the opposite of the previous one – it selects rows based on passed values. It accepts string with list of values and then attempts to locate a row by value from that list; if row is found – it’s selected. Function assumes that column with the key exists at at every level of hierarchy
// Selects row in WebHiearchicalDataGrid based on list of passed values // i_oGridView: Top level grid in Infragistics WebHiearchicalDataGrid object // i_sColumnKey: Key of column to which cells to compare // i_sIDlist: String with list of values based on which rows are selected // i_sSeparator: Separator between values in string with values function listToSelRows(i_oGridView, i_sColumnKey, i_sIDlist, i_sSeparator) { if (typeof (i_sSeparator) == 'undefined') i_sSeparator = ','; var aIDlist = i_sIDlist.split(i_sSeparator); var aRows = i_oGridView.get_rows(); // getting collection of all rows from current gridview var aSelectedRows = i_oGridView.get_behaviors().get_selection().get_selectedRows(); // getting collection of selected rows from current view var oRow, oCell, aRowIslands; //looping thru all rows in current gridview for (var I = 0; I < aRows.get_length(); I++) { oRow = aRows.get_row(I); //locating cell by column key oCell = oRow.get_cellByColumnKey(i_sColumnKey); //if cell's value exists in passed-in string of values - select that row if (Array.indexOf(aIDlist, oCell.get_text()) != -1) aSelectedRows.add(oRow); //getting child row islands of current row aRowIslands = oRow.get_rowIslands(); //looping thru child row islands calling function itself recursively for (var J = 0; J < aRowIslands.length; J++) { listToSelRows(aRowIslands[J], i_sColumnKey, i_sIDlist, i_sSeparator) } } }
The last function simple clears selection from the grid completely:
// Clear selection accross all the rowislands of the WebHiearchicalDataGrid // i_oGrid: Infragistics WebHiearchicalDataGrid object function clearRowSelection(i_oGrid) { //Getting array of groups of selected rows var oSelRowsCollections = i_oGrid.get_gridView().get_behaviors().get_selection().get_selectedRowsCollections(); //Looping thru array of groups of selected rows, clearing each in turn for (var I = 0; I < oSelRowsCollections.length;I++ ) { oSelRowsCollections[I].clear(); } }