How can I select all rows in a child grid when its parent row is selected, using a Kendo Grid?

StackOverflow https://stackoverflow.com/questions/23371803

  •  12-07-2023
  •  | 
  •  

Question

I have a kendo hierarchical grid. When I click a row in the parent grid, the parent row becomes selected and expands to show the child grid. My problem is that I need to be able to change all the children rows into the selected state as well.

This is how I accomplish changing the parent row into the selected state:

$("#gridMasterInfo").delegate('tbody>tr', 'click', function (e) {
     var row = $(this);
     $(this).toggleClass('k-state-selected');                       
}); 

Here is a JSFiddle sample of the row selection so far

Any help is appreciated, thanks.

Was it helpful?

Solution

You need some little programming and knowing how to select and deselect rows...

The very first thing is creating the detailInit method for your grid, you are probably familiar with this and the only thing here is add a little trick for easily find the details rows in the parent grid.

function detailInit(e) {
    $("<div class='ob-child-grid'/>").appendTo(e.detailCell).kendoGrid({
        ...
    });
}

As you can see, I've added the class ob-child-grid to the container of the grid. I can actually rely on k-secondary (this class is added by Kendo UI to this node) but I prefer not to rely on non documented functionality so the code is less vulnerable to future modifications of KendoUI.

Next is defining a change handler that gets invoked when the row in the parent is selected. This function has to do:

  1. Remove previously selected rows in details grid. I'm assuming that once you select a new row in the parent you want to get the children of the previously selected parent deselected.
  2. Find the child grid of the currently selected row.
  3. Select every element in the child grid.

Lets see how to do it:

// Remove previous selections
$("tr", ".ob-child-grid").removeClass("k-state-selected");

As you can see this step is pretty simple, just remove "k-state-selected" this is all you actually need.

// Find the child grid.
var child = this.select().next().find(".ob-child-grid");

this.select() gets currently selected row and next() gets next row that is where KendoUI places the child grid. With find(".ob-child-grid") we find the node where the grid actually is.

// Select every row
$("tr", child).addClass("k-state-selected");

We add k-state-selected that is the class that marks a row as selected.

You can play with the idea here: http://jsfiddle.net/OnaBai/XaMer/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top