The Vuetify treeview displayed all objects as open, even though I had not enabled the "open-all" option

Currently, I am configuring Vuetify's treeview component for my project. When I clicked on the folder objects within the treeview, every object opened up without me setting the 'open-all' option.

My project is based on vue cli 3 and my ESLint configs follow the 'airbnb' style guide.

TEMPLATE :

        <v-card class="mx-auto">
          <v-sheet class="pa-1 tree__resources lighten-2>
            <v-treeview
              :active.sync="active"
              :open.sync="open"
              :items="items"
              item-key="id"
              activatable
              active-class="primary--text"
              open-on-click
              return-object>
              <template v-slot:prepend="{ item, open }">
                <v-icon v-if="item.type == 'directory'">
                  {{ open ? 'mdi-folder-open' : 'mdi-folder' }}
                </v-icon>
                <v-icon v-else>
                  {{ files[item.type] }}
                </v-icon>
              </template>
            </v-treeview>
          </v-sheet>
        </v-card>  

Scripts:

export default {
  data: () => ({
    active: [],
    open: [],
    items: ["JSON DATA"],
    files: {
      html: 'mdi-language-html5',
      js: 'mdi-nodejs',
      json: 'mdi-json',
      md: 'mdi-markdown',
      pdf: 'mdi-file-pdf',
      // more file types...
    },
    filename: '',
    filepath: '',
    filetype: '',
  },
}),
computed: {
    selected() {
      if (!this.active.length) return undefined;

      const selected = this.active[0];

      console.log(selected);
    },
  },
  methods: {
.
.
.

JSON DATA SAMPLE:

[
   {
      "name":"sources",
      "type":"directory",
      // more directory/file structures...
   }
]

When I clicked on the "resources" folder object, I expected the output to look like this:

However, the actual output was different as everything opened immediately:

Answer №1

Your item-key is currently set to id, but it appears as though some nodes have an undefined id. It is recommended that each node has a unique identifier, such as:

[
   {
      "name":"sources",
      "id": 1,
      "type":"directory",
      "children":[
         {
            "name":"android",
            "id": 2
            "type":"directory",
            "children":[]
         }
        ....
   }
]   

If having a unique name is not possible within your structure, you may consider changing the item-key to name. This way, there will be a guaranteed uniqueness based on the node names.

           <v-treeview
              :active.sync="active"
              :open.sync="open"
              :items="items"
              item-key="name"
              activatable
              active-class="primary--text"
              open-on-click
              return-object>
              <template v-slot:prepend="{ item, open }">
                <v-icon v-if="item.type == 'directory'">
                  {{ open ? 'mdi-folder-open' : 'mdi-folder' }}
                </v-icon>
                <v-icon v-else>
                  {{ files[item.type] }}
                </v-icon>
              </template>
            </v-treeview>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips on how to modify database records rather than generating new ones

Currently, my team is developing a web application that utilizes wearables to monitor vital parameters. As part of our integration testing, we are using a Fitbit device. The app itself is built with Angular and JavaScript, while the database is hosted on C ...

Is there a way to design a catalog page for my website that doesn't include a shopping cart or detailed product pages?

I am looking to create a catalogue feature similar to Newegg's for my website, but with a simplified approach. I have never attempted something this advanced before and I am wondering if it is possible to achieve. My plan is to use PHP and JS for the ...

Populating a dropdown with a list by utilizing the append grid library

Hey there! I'm currently using the jquery appendGrid plugin to display a drop-down menu in one of the columns. In the code snippet below, the column labeled "Origin" has a drop-down option that is working as expected. $(function () { // Initializ ...

What is the correct method for completely eliminating a mesh from the three.js scene?

I am looking for a way to fully remove meshes from a three.js scene without causing any memory leaks. I have noticed that reloading the same models multiple times can lead to browser crashes, indicating that memory is not being properly deallocated. ...

Press the button to move the slider

I implemented a scroll slider with dynamic content by following an example from jquery.com. Now, I am looking to enhance it further by adding left and right image buttons to control the scrolling and remove the scrollbar completely. <html lang="en"&g ...

Tips for preventing a Wistia video from playing in a tab when moving away from the tab

Hey everyone, I'm facing an issue with a jsp page that contains multiple tabs. One of the tabs has a wistia video embedded in it. In Firefox and Chrome, when I switch away from the video tab, the video stops playing as expected. However, in Internet ...

React Native is facing difficulty in fetching pagination data which is causing rendering errors

Currently, I am working on fetching pagination data from an API. The process involves retrieving data from https://myapi/?page=1, then from https://myapi/?page=2, and so on. However, despite following this logic, I encountered an error that has left me puz ...

What is the best way to retrieve a URL from a function in an HTML/JavaScript environment?

Recently, I delved into learning HTML and JavaScript. One of the challenges I encountered is figuring out how to access a URL or download a file from a function written in JavaScript that was imported from another file using: <script src="jscriptSheet. ...

Can gRPC be integrated into Nuxt.js?

Currently, I am utilizing Nuxt.js to construct a frontend for interfacing with data obtained from a REST API. In order to interact with functions on a remote service, I am considering using gRPC as there is an endpoint available for this purpose. Despite ...

Implementing a configuration file into a client-side web application using asynchronous methods

Currently, I am facing a challenge with an SPA that is being developed using various custom components from different sources. The main issue at hand is how to initialize certain settings (such as Endpoint URLs) using a settings file that can be configure ...

Determine the number of elements chosen within a complex JSON structure

I need to figure out how to count the length of a jsonArray, but I'm stuck. Here's an example to start with: https://jsfiddle.net/vuqcopm7/13/ In summary, if you click on an item in the list, such as "asd1", it will create an input text every t ...

Enhancing the appearance of dropdown menus for WooCommerce Variable products with custom CSS styling

I currently have a Wordpress website with WooCommerce and several variable products. Each product's variation has a dropdown menu that displays different options when clicked. My main concern is how to customize the appearance of these dropdown menus ...

Google Extension PHP Plugin

Is it feasible to integrate a PHP file into a Google Extension for Chrome? I've been exploring the idea of creating an extension and most resources only mention HTML, CSS, and JavaScript. Any guidance on using PHP in the extension would be highly valu ...

What could be causing the value not to display in my range slider thumb tooltip?

In a recent project of mine, I implemented a range slider with two thumbs - one for setting the minimum value and one for the maximum value. The goal was to provide users with a visual representation of the range they are selecting by displaying the thumb ...

Retrieve the name of the file from a given URL by using JavaScript/jQuery, even when only the directory path is

I need to extract the current filename from the URL using: $currentFile = window.location.pathname.split("/").pop(); This method functions properly when the full path looks like: http://www.yoursite.com/folder/index.php It will return index.php, index. ...

Executing iterations within the document.ready function using jQuery and d3.js

Here is how my data is structured: var IDData = JSON.stringify([["node/105173", "node/38180995", "Agent", "Customer", "1379644.0", 1, 264.0, "1374903"]...] Each row in the data follows the same format, although the length of the array arrays can vary. I ...

Is it considered poor design to pass a function two levels deep? Are there other possible alternatives to achieve the same outcome?

I am currently working on a scenario involving componentA, which also contains another componentB with buttons that need to update the scene. My initial thought was to pass a function from the scene to componentB through componentA, but since I am new to ...

Disregard the need for synchronization with $http, but remember to consider it for

Is there a way to prevent synchronization with HTTP requests in an Angular app? I am working on a form that should be disabled while the POST request is in progress. I want to write specifications to ensure that the form remains disabled during this time. ...

ng-disabled is failing to interact with the $scope variable

Attempting something like this: <button class="btn btn-lg btn-block btn-section" ng-disabled="{{ selectedfruits.length }} < 5" > Show selected fruits</button> When inspecting in Chrome developer tools, the code appears as follows: &l ...

Why am I receiving an undefined value when I try to log the createdAnimal?

My main goal with this code is to successfully console.log(createdAnimal) and have the objectAnimal with specific parameters printed out. The following code snippet demonstrates the desired parameters: animalMaker('cat','flying',true); ...