search

Home  >  Q&A  >  body text

How to set the attributes of multiple subitems of v-treeview

<p>Suppose I have the variable items with the following structure: </p> <pre class="brush:php;toolbar:false;">[ { id: 1, name: 'value1', item_nested: [ { id: 2, name: 'value2', }, { id: 3, name: 'value3', nested_item: [ { id: 4, name: 'value4' } ] } ] } ]</pre> <p>In v-treeview we have a props called item-children which accepts a string which allows to set which property will be referenced as a child. In the first level I want to have the attribute <code>item_nested</code> as a child and in the second level I want to have the <code>nested_item</code> as a child but is it possible to set Multiple values ​​for item-children? </p>
P粉852578075P粉852578075460 days ago564

reply all(1)I'll reply

  • P粉850680329

    P粉8506803292023-08-29 00:50:29

    VTreeView uses a string as the key for the child, so - without extending/overwriting the original component - I don't see a way to provide an alternative key for the child .

    However, you can always "retype" the objects you want to display in VTreeview (this code snippet only applies to this specific case, but can be generalized to other cases):

    new Vue({
      el: '#app',
      vuetify: new Vuetify(),
      computed: {
        // "re-keying" items:
        modifiedTreeviewItems() {
          const updateItemKeys = (items) => {
            if (!items.length) return []
    
            const mapped = items.map((item) => {
              const {
                item_nested = [], nested_item = [], ...rest
              } = item
              const children = item_nested.length ? item_nested : nested_item.length ? nested_item : []
              return {
                ...rest,
                children: updateItemKeys(children),
              }
            })
            return mapped
          }
          return updateItemKeys(this.treeviewItems)
        },
      },
      data() {
        return {
          treeviewItems: [{
            id: 1,
            name: 'value1',
            item_nested: [{
                id: 2,
                name: 'value2',
              },
              {
                id: 3,
                name: 'value3',
                nested_item: [{
                  id: 4,
                  name: 'value4'
                }]
              }
            ]
          }]
        }
      },
    })
    <link href="https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,700,900" rel="stylesheet">
    <link href="https://cdn.jsdelivr.net/npm/@mdi/font@6.x/css/materialdesignicons.min.css" rel="stylesheet">
    <link href="https://cdn.jsdelivr.net/npm/vuetify@2.x/dist/vuetify.min.css" rel="stylesheet">
    <div id="app">
      <v-app>
        <v-main>
          <v-container>
            <v-treeview :items="modifiedTreeviewItems" />
          </v-container>
        </v-main>
      </v-app>
    </div>
    
    <script src="https://cdn.jsdelivr.net/npm/vue@2.x/dist/vue.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/vuetify@2.x/dist/vuetify.js"></script>

    reply
    0
  • Cancelreply