components icon indicating copy to clipboard operation
components copied to clipboard

mat-tree should have a filtering feature

Open ajaysattikar opened this issue 8 years ago • 23 comments

Feature request, or proposal:

Filtering through tree nodes is a very useful feature, especially to reduce the size of tree and finding out the desired node(s) quickly.

What is the expected behavior?

There should be an input box above mat-tree. As you enter the text, it will filter through the tree and renders the tree based on the content in the input box.

What is the current behavior?

Filtering feature is not available in mat-tree component.

ajaysattikar avatar May 24 '18 22:05 ajaysattikar

I tried to create a solution myself on the link below. It can help those who need it.

stackblitz sample

mfatiho avatar Sep 07 '18 07:09 mfatiho

I tried to create a solution myself on the link below. It can help those who need it.

stackblitz sample

@mehfatih - You applied filter successfully applied to Mat-tree and works fine. But when I click checkbox and remove the filtertext, then the checked values are missing.

rojagadeesh avatar Oct 12 '18 15:10 rojagadeesh

The filter could work like this one: https://angular2-tree.readme.io/docs/filtering

hugocalheira avatar Nov 07 '18 17:11 hugocalheira

@rojagadeesh any solutions for this?

BobaFett58 avatar Jan 06 '19 10:01 BobaFett58

No, I didn't found any solution how to apply filter to mat-tree

rojagadeesh avatar Jan 07 '19 02:01 rojagadeesh

solution given by Mr.mehfatih works well for linear tree data. but it fails to track node path while searching in nested tree data like in official documentation examples.

NaveenMysuru avatar Jan 21 '19 13:01 NaveenMysuru

+1 this would be really useful

abchugh avatar Sep 17 '19 13:09 abchugh

Indeed, this would be very useful

shantd9 avatar Sep 25 '19 08:09 shantd9

Need it too

Hartaxfr avatar Oct 04 '19 14:10 Hartaxfr

No solutions so far? Struggling with this issue too...

wilhelmmi avatar Dec 02 '19 10:12 wilhelmmi

I wrote this recursive function that implements search in nested tree and it works. It gets a root node, and returns a tree that includes the searched nodes, all searched nodes' descendants, and the direct path of the forefathers:

` //gets a tree root and returns a NEW tree root includes only nodes relevant to the "searchedText" //will return a tree that includes the searched nodes, all searched nodes' descendants, and the direct path of the forefathers private filterTreeRecursion(searchedText: string, node: Node): Node { // if node's name includes the searched string - returns it if (node.item.toLowerCase().includes(searchedText.toLowerCase())) { return node; }

//if not - continue to check for it's children
if (node.children && node.children.length > 0) {
  let newParentNode: Node = {
    children: [],
    item: node.item,
    id: node.id,
    pid: node.pid,
    showIcons: node.showIcons,
    data: node.data,
    character: node.character,
  }

  //going through all node's children
  for (let i = 0; i < node.children.length; i++) {
    let resNode = this.filterTreeRecursion(searchedText, node.children[i]);
    if (resNode !== undefined) {
      newParentNode.children.push(resNode); //if there is a result, it's asearched node or a forefather of the searched node - so we add it
    }
  }

  if (newParentNode.children.length > 0) {
    return newParentNode;
  }
}

} `

shirlebel avatar Jan 28 '20 10:01 shirlebel

@shirlebel , Where can i put this function, because when I add my in this example it saying node.childern is undefined.

vickyadi3 avatar Feb 17 '20 10:02 vickyadi3

@shirlebel , Where can i put this function, because when I add my in this example it saying node.childern is undefined.

Notice that the function starts with "private filterTreeRecursion(searchedText: string, node: Node)" (it's outside the marked square) and check your node structure, if it doesn't have the children field you need to change it to your field

shirlebel avatar Feb 17 '20 10:02 shirlebel

i would love to have this feature

ghost avatar Apr 01 '20 17:04 ghost

You may take a look Here

Gagulo avatar Jun 18 '20 05:06 Gagulo

+1

miladSorour avatar Nov 21 '20 13:11 miladSorour

Anything on this?

tmushayahama avatar Feb 09 '21 07:02 tmushayahama

I tried to visually filter the mat tree without modifying the datasource. Check this stackblitz link.

https://stackblitz.com/edit/tree-with-checkbox-and-a-filter?file=src/app/tree-checklist-example.ts

Amith-B avatar Jun 26 '21 05:06 Amith-B

Hi, I'm trying to get a gauge on whether this is something we actually want to implement or if this is something that should be left to developers to handle on their own. I'm currently leaning towards the latter for the following reasons:

  1. Even in this thread there have been a couple of pretty straightforward implementations
  2. Filters seem opinionated and any one implementation would probably not satisfy all desired filtering strategies

Let me know your thoughts and if you agree or disagree

wagnermaciel avatar Jun 30 '22 18:06 wagnermaciel

@wagnermaciel I'd really appreciate the implementation of this idea...

viheag-98 avatar Jul 04 '22 15:07 viheag-98