Skip to content

fix(VTreeview): automatically expand filtered items' ancestors#22367

Closed
prestonyford wants to merge 2 commits into
vuetifyjs:masterfrom
prestonyford:fix/vtreeview-expand-ancestors
Closed

fix(VTreeview): automatically expand filtered items' ancestors#22367
prestonyford wants to merge 2 commits into
vuetifyjs:masterfrom
prestonyford:fix/vtreeview-expand-ancestors

Conversation

@prestonyford

@prestonyford prestonyford commented Nov 25, 2025

Copy link
Copy Markdown

fixes #22035
When searching a VTreeView, automatically expands the ancestors of filtered items.

Description

I used a watcher to watch when the filtered items change. I find the path of all filtered items and emit update:opened to include those so that they are opened in addition to the filtered items.

Markup:

<template>
  <div>
    opened: {{ openedTitles }}
  </div>
  <v-card class="mx-auto" max-width="500">
    <v-sheet class="pa-4" color="surface-variant">
      <v-text-field v-model="search" clear-icon="mdi-close-circle-outline" label="Search Company Directory"
        variant="solo" clearable flat hide-details></v-text-field>

      <v-checkbox-btn v-model="caseSensitive" label="Case sensitive search"></v-checkbox-btn>
    </v-sheet>

    <v-treeview v-model:opened="open" :custom-filter="filter" :items="items" :search="search" item-value="id"
      open-on-click>
      <template v-slot:prepend="{ item }">
        <v-icon v-if="item.children" :icon="`mdi-${item.id === 1 ? 'home-variant' : 'folder-network'}`"></v-icon>
      </template>
    </v-treeview>
  </v-card>
</template>

<script setup>
import { shallowRef, computed } from 'vue'

const items = [
  {
    id: 1,
    title: 'Vuetify Human Resources',
    children: [
      {
        id: 2,
        title: 'Core team',
        children: [
          {
            id: 20001,
            title: 'John',
            children: [
              {
                id: 30001,
                title: 'TEST 1',
              },
              {
                id: 30002,
                title: 'TEST 2',
              },
            ],
          },
          {
            id: 202,
            title: 'Kael',
          },
          {
            id: 203,
            title: 'Nekosaur',
          },
          {
            id: 204,
            title: 'Jacek',
          },
          {
            id: 205,
            title: 'Andrew',
          },
        ],
      },
      {
        id: 3,
        title: 'Administrators',
        children: [
          {
            id: 301,
            title: 'Blaine',
          },
          {
            id: 302,
            title: 'Yuchao',
          },
        ],
      },
      {
        id: 4,
        title: 'Contributors',
        children: [
          {
            id: 401,
            title: 'Phlow',
          },
          {
            id: 402,
            title: 'Brandon',
          },
          {
            id: 403,
            title: 'Sean',
          },
        ],
      },
    ],
  },
]

const open = shallowRef([1, 2])
const search = shallowRef(null)
const caseSensitive = shallowRef(false)

function filter(value, search, item) {
  return caseSensitive.value ? value.indexOf(search) > -1 : value.toLowerCase().indexOf(search.toLowerCase()) > -1
}


function findById(items, id) {
  for (const item of items) {
    if (item.id === id) return item;

    if (item.children) {
      const found = findById(item.children, id);
      if (found) return found;
    }
  }

  return null;
}

const openedTitles = computed(() => {
  return open.value.map(id => findById(items, id)?.title ?? '??')
})

</script>

@J-Sek

J-Sek commented Jun 16, 2026

Copy link
Copy Markdown
Contributor

The direction seems good, but there are some shortcomings

  • breaks when opened is null or undefined
  • should not require v-model:opened to work
  • probably missing toRaw
  • should it auto-collapse the tree when search is cleared?

J-Sek added a commit that referenced this pull request Jul 2, 2026
@J-Sek

J-Sek commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

closing in favor of #22983

@J-Sek J-Sek closed this Jul 2, 2026
J-Sek added a commit that referenced this pull request Jul 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug Report][3.9.7] VTreeview won't automatically expand searched items' parents

2 participants