Autocomplete in Vue.js with cascading suggestions based on object keys

I have been experimenting with creating a 3 or 4 level cascading autocomplete feature in Vue.js, but I'm facing some issues with its functionality. You can view the original code on this CodePen link.

<div id="app">
  <v-app id="inspire">
    <v-card>
      <v-card-text>
        <v-layout wrap>
          <v-flex xs3 md3>
            <v-autocomplete v-model="category" :items="categories" label="Category" full-width solo hint="Random set of categories">
            </v-autocomplete>
          </v-flex>
          <v-flex xs3 md3>
            <v-autocomplete v-model="purpose" :items="purposes" label="Purpose" full-width solo hint="Based on the selected category">
            </v-autocomplete>
          </v-flex>
        </v-layout>
      </v-card-text>
    </v-card>
  </v-app>
</div>

Javascript:

new Vue({
  el: "#app",
  data() {
    return {
      model: null,
      product: null,
      category: null,
      purpose: null,
      categoriesPurposes: {
        "Farm Animals": ["cow", "sheep", "hen"],
        Directions: ["left", "right", "up", "down"],
        Time: ["past", "present", "future"],
        Element: ["air", "water", "tierra", "fire"]
      }
    };
  },
  computed: {
    categories() {
      return Object.keys(this.categoriesPurposes);
    },
    purposes() {
      if (!this.category) {
        return null;
      } else {
        return this.categoriesPurposes[this.category];
      }
    }
  }
});

I attempted to add another level of depth, but the second autocomplete field displayed object data.

To address this, I modified the data structure as follows:

categoriesPurposes: {
        Asia:{
            "Farm Animals": ["cow", "sheep", "hen"],
            Directions: ["left", "right", "up", "down"],
            Time: ["past", "present", "future"],
            Element: ["air", "water", "tierra", "fire"]
          }
       }

However, even with this change, the data for the second autocomplete field still appears as an object.

Could you provide guidance on how to access and display the keys? Thank you.

Answer №1

An error occurred due to providing an Object value that is not allowed according to Vuetify's guidelines

[Vue warn]: Invalid prop: type check failed for prop 'items'. Expected Array, got Object

The issue lies in your code snippet

this.categoriesPurposes[this.category]
which returns an Object instead of the required Array of Objects specified by Vuetify. To resolve this, you should refer to Vuetify's documentation to understand the correct data format.

link to docs

You can use the following structure:

      categoriesPurposes: {
        "Farm Animals": [
          {
            text: "This is the cow",
            value: "cow",
          },
          {
            text: "This is the sheep",
            value: "sheep",
          },
          {
            text: "This is the hen",
            value: "hen"
          }
        ],
        Directions: ["left", "right", "up", "down"],
        Time: ["past", "present", "future"],
        Element: ["air", "water", "tierra", "fire"]
      }

Check out a demo showcasing the correct implementation here

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

Using regular expressions, divide the string at every occurrence of a period unless there is a quotation mark immediately following the period, in which case

Can anyone help me split this string? It includes quotation marks: "This is a test. I need this to be splitted." And here is one with a question? I am looking for: ['"This is a test.', 'I need this to be splitted."' ...

React.js is throwing a 429 error message indicating "Too Many Requests" when attempting to send 2 requests with axios

Currently, I am in the process of learning React with a project focused on creating a motorcycle specifications search web application. In my code file /api/index.js, I have implemented two axios requests and encountered an error stating '429 (Too Ma ...

After completing the Spotify authentication process using implicit grant in a React application, the redirection is not functioning as

I am working on integrating Spotify authentication using the implicit grant method as outlined in their documentation. I have implemented the relevant code into a React component and successfully logged into Spotify. However, I am facing an issue where the ...

What is the process of extracting data from JavaScript output using Mongoose?

Currently facing an issue with mongoose while working on a blog website and integrating quilljs. The problem arises from the output file in my JavaScript file that contains the configurations for my quill editor. var quill = new Quill('#editor&ap ...

Guide to making a Typescript interface by combining elements from two separate interfaces without utilizing inheritance

Programming Language: Typescript I am looking to combine the properties of two interfaces as the value of an indexable-type within a third interface. Interface 1: export interface Employee { id: string name: string } Interface 2: export interfa ...

Capture the onclick attribute with jQuery and then reapply it

I am facing a challenge with a page that has several calendars composed of HTML tables where each day is represented by a td. The td elements have an onClick attribute which I need to manipulate using jQuery. Specifically, I need to remove the onClick attr ...

Creating a custom table layout with JavaScript and jQuery

I've been grappling with a project for some time now, and I'm stuck on this particular issue. In my database, I have a table structured like this: ProjectName UserName SpentDays FirstProject User1 10 SecondProject User1 5 SecondProjec ...

Selecting a main node using the key value

Is there a way to select the root node of a d3.hierarchy based on a specific JSON key value? For instance, instead of having "A1" as the root node in the given JSON object, can we make "B1" the root node by referencing its name value? { "name": "A1", ...

Tips for achieving a seamless page transition using Vuetify

I'm a beginner with Vue/Vuetify and I have a question regarding page transition/rendering upon cache refresh in Chrome: As an example, I added a small code snippet to my project here: codepen.io/vreaxe/pen/oeWwOJ. After performing a cache-refres ...

Exploiting jQuery UI in a Web Browser Bookmarklet

When using CoffeeScript, even though the code is very similar to JavaScript: tabs_html = "<div id='nm-container'><ul><li><a href='#tabs-1'>Guidelines</a></li><li><a href='#tabs-2'& ...

locate and interact with a dynamically created button using JavaScript through ajax

Hey there, I'm trying to display an image along with a button using AJAX whenever a visitor uploads an image. for (var i = 0; i < data.length; i = i + 1) { imageThump += '<img src="' + data[i].path + '" />'; image ...

The Next.js build process encountered an error when building due to an issue with plotly.js (The build failed with a ReferenceError: self is

Whenever I attempt to build the Next.js app for production using the yarn build command, I encounter the following error. Strangely enough, everything works perfectly fine on the development server when using the yarn dev command. The app employs the react ...

converting objects into an array

I am currently working on a project using JavaScript/Vue where I am displaying ingredients by mapping each item and joining them with ", ". However, I now want to display each item as a list but I am unsure of how to achieve this. The current code I have i ...

Horizontal alignment of buttons within a textfield in Vuetify 2

I'm currently working with vuetify 2 and I am attempting to create a unique text field that includes 2 or more buttons as an inner append. I recently came across a snippet showcasing something similar here: https://www.reddit.com/r/vuetifyjs/comments/ ...

The data from the server is inaccessible to node.js

I am a beginner in nodejs and jquery, trying to retrieve data from a server and use it to update a webpage: Using jquery on the client side: I would like to change the text inside '#info' element with the data fetched from the server. $('# ...

Close any open alerts using Protractor

While using protractor and cucumber, I have encountered an issue where some tests may result in displaying an alert box. In order to handle this, I want to check for the presence of an alert box at the start of each test and close/dismiss it if it exists. ...

Executing certain test suites with defined capabilities in Protractor

My website is designed to be compatible with both desktop and mobile browsers, each having its own unique user interface. In my protractor config file, I have some suites that need to be tested using the desktop user agent, while others require testing usi ...

The messaging feature is not functioning properly within the page tab

The iframe page tab is not compatible with the Send dialog. I followed the example on the 'Send Dialog' page https://developers.facebook.com/docs/reference/dialogs/send/ <html xmlns:fb="https://www.facebook.com/2008/fbml"> <body> ...

Re-enable VueJS Devtools during the development process

While working in my homestead development environment, I decided to run the command npm run production to test if the vuejs devtools would disappear and make sure everything was functioning properly. Surprisingly, the devtools did vanish. Now, I am struggl ...

Eliminating blank attributes within an array of objects

I'm currently working on a task that involves creating an array to summarize another array. I've received valuable suggestions from various sources, including this discussion on Stack Overflow. Although the solutions provided are effective, they ...