No More Posting New Topics!

If you have a question or an issue, please start a thread in our Github Discussions Forum.
This forum is closed for new threads/ topics.

Navigation

    Quasar Framework

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search

    [Solved] Tree filter: how to filter the tree from a button click in addition to search text

    Framework
    1
    3
    492
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • M
      murrah last edited by murrah

      Hi. I have a tree that filters based on a search input. All good.

      I also want to filter based on a button that relates to a status property of each node. So, when I click the button, filter the tree nodes for status === 1 (for example)

      I have added the status filter to the custom filter method and as long as I type something into the search box the status filter works perfectly.

      But… how do I tell the tree to start filtering ONLY from the button click? i.e. even if the search input is empty, filter on status?

      I could not find a myTree.runFilter() method and as I am pretty new to Vue I could not work it out from the Quasar tree source code.

      Thanks,
      Murray

      1 Reply Last reply Reply Quote 0
      • M
        murrah last edited by

        This post is deleted!
        1 Reply Last reply Reply Quote 0
        • M
          murrah last edited by

          So, I want to filter the tree by both a Search input box and / or a value passed by a button click that represents a status property on the leaf nodes. (Actually, there are 5 such buttons, each representing a different status, but for simplicity’s sake we pretend there is just one status value.)

          This is the stripped-down solution.

          <!-- The search input box -->
          <q-input
            v-model="searchBox"
            label="Filter"
          ></q-input>
          
          <!-- The button to filter where node status = 1 -->
          <q-button @click="onClickStatus(1)">
            Status 1
          </q-button>
          
          <!-- The tree -->
          <q-tree
            ref="projectTree"
            :filter="filterTrigger"
            :filter-method="customFilterMethod"
           ....
          ></q-tree>
          
          export default {
            data () {
              return {
                searchBox: '',
                filterTrigger: 'true',
                statusFilter: ''
                ....
              }
            },
          
            watch: {
              // Whenever the searchBox property changes, 
              // use it to trigger the tree filter's reactivity
              // by assigning its value to the 
              // filterTrigger property. 
              // Actually, the value we assign here is irrelevant.
              searchBox: function (newVal, oldVal) {
                this.filterTrigger = newVal
              }
            },
          
            methods: {
              onClickStatus (statusId) {
                // This method is more complicated than this in reality,
                // but basically we track which of the 5 status' were clicked
                this.statusFilter = statusId
              },
          
              customFilterMethod (node, filterTrigger) {
                // The filterTrigger param is ignored in here.
          
                // Instead, we use the contents of the search box input ...
                const searchBoxText = this.searchBox.toLowerCase()
          
                const matchedNodeLabel = (node.label && node.label.toLowerCase().indexOf(searchBoxText) > -1)
          
                // And any status button that was clicked ....
                let matchedStatus = true
          
                if (this.statusFilter !== '') {
                  // if the node status is in the list of filters, match it
                  // Very simplified example code:
                  matchedStatus = (this.statusFilter.indexOf(node.status) > -1)
                }
          
                // In our context we always AND the filters
                return (matchedNodeLabel && matchedStatus)
              }
            }
          }
          

          Now, if there is something typed into the search box, or if a status button is clicked, either or both of those values will be used to filter the tree nodes. Yay! 🙂

          1 Reply Last reply Reply Quote 1
          • First post
            Last post