Author Topic: Filter tree parent rows only  (Read 2556 times)

srmooney

  • Pro OEM
  • Newbie
  • *
  • Posts: 4
    • View Profile
Filter tree parent rows only
« on: February 09, 2021, 06:46:02 pm »
Is there a way to apply the filter to the parent row only when using the tree model?
We would like to be able to see all children for the parent row that is shown based on he filter(s) set.

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6310
    • View Profile
Re: Filter tree parent rows only
« Reply #1 on: February 09, 2021, 10:54:09 pm »
Currently I don't see any way to do it. I would look into the possibility to support it in the upcoming version.

srmooney

  • Pro OEM
  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Filter tree parent rows only
« Reply #2 on: February 11, 2021, 06:35:45 pm »
I was able to get it working using the "filter" event.
Do you see any issues with the below?

Code: [Select]
$grid.on('pqGrid:filter', function (e, ui) {
if (ui.rules.length > 0) {
var grid = gridAPI.grid;
var DM = grid.options.dataModel;
var includeRows = [];
DM.data.forEach(function (row) {
//Find the children for this row
var rowChildren = DM.dataUF.filter(function (x) {
return x.parentId == row.ROLE_ID;
});

//remove the children from the unfiltered data
DM.dataUF = DM.dataUF.filter(function (el) {
return !rowChildren.includes(el);
});

//Match the hide state of children to the parent close state
row.pq_close = true;
rowChildren.forEach(function (value) {
value.pq_hidden = row.pq_close;
});

//add the children back to the row
row.children = row.children.concat(rowChildren);

//add to list of children to include in data
includeRows = includeRows.concat(rowChildren.filter(function (el) { return !DM.data.includes(el); }));
});

//update data with the children we added
DM.data = DM.data.concat(includeRows);

//sort the data again
DM.data = grid.iSort.sortLocalData(DM.data);
}
});

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6310
    • View Profile
Re: Filter tree parent rows only
« Reply #3 on: February 12, 2021, 02:22:41 pm »
I'm glad if it's working for you.

I noticed 2 issues with it: use of private API of grid which may break the code for future versions and due to its O(n2) time complexity it may face performance issues with increase of records.

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6310
    • View Profile
Re: Filter tree parent rows only
« Reply #4 on: March 15, 2021, 11:41:46 am »
This feature has been added in v7.7.0

Please set { filterShowChildren: true } sub-option of treeModel.

https://paramquery.com/pro/api#option-treeModel