Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select Grouping Row in ag-Grid

I'm wondering if there's a way to select the "Grouping" row in ag-grid.

For example, in the example shown on the website: http://www.ag-grid.com/angular-grid-grouping/index.php

You can set the "rowSelection" parameter to "single" in order to highlight an entire row at the lowest node. However, that does not allow you to highlight the "Country" row.

In the example, the only way to do this is to "select all" the elements below that row.

Thanks!

like image 913
slee Avatar asked Oct 03 '15 23:10

slee


People also ask

How do you do a grouping on Ag grid?

Row Group Panel - add a panel above the grid to allow users control which columns the rows are grouped by. Group Order - control how row groups are ordered. Sorting Groups - configure and customise how row groups are sorted. Filtering Groups - configure and customise how row groups are filtered.

How do you select rows on Ag grid?

Select a row by clicking on it. Selecting a row will remove any previous selection unless you hold down Ctrl while clicking. Selecting a row and holding down Shift while clicking a second row will select the range.

How do I select all rows in grid?

Normally, the users can select multiple rows in the DataGrid control by pressing Ctrl or Shift while selecting the rows. Holding Ctrl while selecting the row will add it to the current selection, and holding Shift will extend selection to the selected row.

How do I turn off the particular row in Ag grid?

There is no option in the library to make a single row disabled(both visually and keyboard event based), the only way we could make a single row disabled is by using a customCellRenderer for both header and subsequent cell checkboxes, this allows full control over the checkbox.


3 Answers

According to the documentation, setting the grid option groupSelectsChildren to false will make the grouping row selectable, independently from its children.

groupSelectsChildren: When true, selecting a group will have the impact of selecting all it's children. The group will then display 'selected' when all children are selected, 'unselected' when none are selected and 'intermediate' when children have a mix of selected and unselected. When the node is selecting children, it will never appear in the selected set when calling api.getSelectedNodes(). When false, then the group is selectable independently of the child nodes. When selecting the group node independently of the children, it will appear in the set when calling api.getSelectedNodes().

like image 128
Bnrdo Avatar answered Sep 24 '22 14:09

Bnrdo


I had the same problem so I worked around it by emulating the look-and-feel of selecting rows.

In your columnDefs object, add the cellClassRules attribute to EVERY column definition (so every cell will be highlighted, making it appear that the row itself is highlighted when you click on it):

var columnDefs = [
    { headerName: "#1", cellClassRules: { rowSelected: CustomRowStyle }, field: "Col1" },
    { headerName: "#2", cellClassRules: { rowSelected: CustomRowStyle }, field: "Col2" },
    { headerName: "#3", cellClassRules: { rowSelected: CustomRowStyle }, field: "Col3" }
]

Then add an event listener for onCellClicked in your gridOptions object:

onCellClicked: function(cell) {
    SelectedRowIndex = cell.rowIndex;
    $scope.gridOptions.api.refreshView();
}

In your controller, define a variable called SelectedRowIndex:

var SelectedRowIndex; // this will contain the currently selected row number

Now create a function called CustomRowStyle which is called by ag-grid each time it is about to render a cell on-screen. This function will check if the cell is in the same row as the SelectedRowIndex (based on which row the user last clicked on) to determine if the cell should appear with the rowSelected class.

function CustomRowStyle(params) {
    return params.rowIndex === SelectedRowIndex
}

Finally, define a rowSelected class with your selected row CSS:

.rowSelected {
    background-color: silver !important;
}

Whichever row you click on (whether it's a group item or a child item) will appear with the rowSelected CSS. Your controller can always know which row is currently selected by checking the SelectedRowIndex variable.

like image 20
Dr. Cool Avatar answered Sep 24 '22 14:09

Dr. Cool


This probably won't work for all cases but I found that as of version 20.2.0 RowNode's have a 'parent' property (Note: I'm not saying that feature was added in 20.2.0, just that I haven't gone and checked previous versions). So I do this:

api.getRowNode('child-row-id').parent.setSelected(true)

Obviously depending on how many levels of grouping you have and how dynamic that is (i.e. can the user change the grouping configuration, can you have n levels of grouping, etc.) you'll need to detect and adjust, but this works well for my particular use case of remembering what was selected and reselecting it on page refresh because my grid always starts out in the same grouping state on page load.

like image 32
davertron Avatar answered Sep 22 '22 14:09

davertron