Loading suggestions ahead of time in Vuetify's autocomplete feature

I recently started learning the Vuetify framework and have been successfully using most of its components. However, I am facing an issue with the autocomplete component. When I try to set a value during creation, it does not work as expected. I attempted to extend one of the Vuetify examples without any luck.

My goal is to load the first value from the API during creation, but it remains empty.

Thank you in advance for your assistance.

new Vue({
  el: '#app',
  data: () => ({
    descriptionLimit: 60,
    entries: [],
    isLoading: false,
    model: null,
    search: "Cats"
  }),
 created :function() {
  model={"API":"Cats","Description":"Pictures of cats from Tumblr","Auth":"","HTTPS":true,"Cors":"unknown","Link":"https://thecatapi.com/docs.html","Category":"Animals"},
   search="Cats"
},
  computed: {
    fields () {
      if (!this.model) return []

      return Object.keys(this.model).map(key => {
        return {
          key: key,
          value: this.model[key] || 'n/a'
        }
      })
    },
    items () {
      return this.entries.map(entry => {
        const Description = entry.Description.length > this.descriptionLimit
          ? entry.Description.slice(0, this.descriptionLimit) + '...'
          : entry.Description

        return Object.assign({}, entry, { Description })
      })
    }
  },

  watch: {
    search (val) {
      // Items have already been loaded
      if (this.items.length > 0) return

      this.isLoading = true
      console.log("loadgin data")
      // Lazily load input items
      axios.get('https://api.publicapis.org/entries')
        .then(res => {
          const { count, entries } = res.data
          this.count = count
          this.entries = entries
        })
        .catch(err => {
          console.log(err)
        })
        .finally(() => (this.isLoading = false))
    }
  }
})
<div id="app">
  <v-app id="inspire">
    <v-card
      color="red lighten-2"
      dark
    >
      <v-card-title class="headline red lighten-3">
        Search for Public APIs
      </v-card-title>
      <v-card-text>
        Explore hundreds of free API's ready for consumption! For more information visit
        <a
          class="grey--text text--lighten-3"
          href="https://github.com/toddmotto/public-apis"
          target="_blank"
        >the Github repository</a>.
      </v-card-text>
      <v-card-text>
        <v-autocomplete
          v-model="model"
          :items="items"
          :loading="isLoading"
          :search-input.sync="search"
          color="white"
          hide-no-data
          hide-selected
          item-text="Description"
          item-value="API"
          label="Public APIs"
          placeholder="Start typing to Search"
          prepend-icon="mdi-database-search"
          return-object
        ></v-autocomplete>
      </v-card-text>
      <v-divider></v-divider>
      <v-expand-transition>
        <v-list v-if="model" class="red lighten-3">
          <v-list-tile
            v-for="(field, i) in fields"
            :key="i"
          >
            <v-list-tile-content>
              <v-list-tile-title v-text="field.value"></v-list-tile-title>
              <v-list-tile-sub-title v-text="field.key"></v-list-tile-sub-title>
            </v-list-tile-content>
          </v-list-tile>
        </v-list>
      </v-expand-transition>
      <v-card-actions>
        <v-spacer></v-spacer>
        <v-btn
          :disabled="!model"
          color="grey darken-3"
          @click="model = null"
        >
          Clear
          <v-icon right>mdi-close-circle</v-icon>
        </v-btn>
      </v-card-actions>
    </v-card>
  </v-app>
</div>

Answer №1

It seems like there is a missing ',' at the end of the "model" in the code snippet you are trying to use.

created :function() { 
  model={...}, <----
  search="Cats"
},

Answer №2

blalan05 mentioned on the Discord channel that they received the following response: "my created function was garbled."

new Vue({
  el: '#app',
  data: () => ({
    descriptionLimit: 60,
    entries: [],
    isLoading: false,
    model: null,
    search: "Cats"
  }),
 created () {
  this.model = {"API":"Cats","Description":"Pictures of cats from Tumblr","Auth":"","HTTPS":true,"Cors":"unknown","Link":"https://thecatapi.com/docs.html","Category":"Animals"},
   this.search = "Cats"
},
  computed: {
    fields () {
      if (!this.model) return []

      return Object.keys(this.model).map(key => {
        return {
          key: key,
          value: this.model[key] || 'n/a'
        }
      })
    },
    items () {
      return this.entries.map(entry => {
        const Description = entry.Description.length > this.descriptionLimit
          ? entry.Description.slice(0, this.descriptionLimit) + '...'
          : entry.Description

        return Object.assign({}, entry, { Description })
      })
    }
  },

  watch: {
    search (val) {
      // Items have already been loaded
      if (this.items.length > 0) return

      this.isLoading = true
      console.log("loading data")
      // Lazily load input items
      axios.get('https://api.publicapis.org/entries')
        .then(res => {
          const { count, entries } = res.data
          this.count = count
          this.entries = entries
        })
        .catch(err => {
          console.log(err)
        })
        .finally(() => (this.isLoading = false))
    }
  }
})
<div id="app">
  <v-app id="inspire">
    <v-card
      color="red lighten-2"
      dark
    >
      <v-card-title class="headline red lighten-3">
        Search for Public APIs
      </v-card-title>
      <v-card-text>
        Explore hundreds of free API's ready for consumption! For more information visit
        <a
          class="grey--text text--lighten-3"
          href="https://github.com/toddmotto/public-apis"
          target="_blank"
        >the Github repository</a>.
      </v-card-text>
      <v-card-text>
        <v-autocomplete
          v-model="model"
          :items="items"
          :loading="isLoading"
          :search-input.sync="search"
          color="white"
          hide-no-data
          hide-selected
          item-text="Description"
          item-value="API"
          label="Public APIs"
          placeholder="Start typing to Search"
          prepend-icon="mdi-database-search"
          return-object
        ></v-autocomplete>
      </v-card-text>
      <v-divider></v-divider>
      <v-expand-transition>
        <v-list v-if="model" class="red lighten-3">
          <v-list-tile
            v-for="(field, i) in fields"
            :key="i"
          >
            <v-list-tile-content>
              <v-list-tile-title v-text="field.value"></v-list-tile-title>
              <v-list-tile-sub-title v-text="field.key"></v-list-tile-sub-title>
            </v-list-tile-content>
          </v-list-tile>
        </v-list>
      </v-expand-transition>
      <v-card-actions>
        <v-spacer></v-spacer>
        <v-btn
          :disabled="!model"
          color="grey darken-3"
          @click="model = null"
        >
          Clear
          <v-icon right>mdi-close-circle</v-icon>
        </v-btn>
      </v-card-actions>
    </v-card>
  </v-app>
</div>

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

Organize the strings by first sorting them alphabetically based on the first letter and then by length starting from the longest. Next, arrange the strings in ascending

After experimenting and seeking help on stackoverflow, I have managed to sort an array of objects based on the 'plate' key using the following function: sort(function(a, b) { return a.plate.toLowerCase() > b.plate.toLowerCase() ? a.pla ...

Issue with Vue data table where the row's q-input does not retain the values of the model

For access to the code, please visit the azure link for the built code and check out the source code on github. In this particular scenario, I am utilizing a q-datatable from Quasar Framework. The data is structured in nested form with primary data displ ...

Using Javascript and Node.js to implement AI: Repeating a word from an array by using matching techniques

Seeking assistance with an AI project involving javascript and Node.js. I have set up hardcoded questions and answers, allowing users to modify the AI's responses. The majority of the javascript code has been implemented on the server-side. I am wor ...

Ensuring the accuracy of a condition within an HTML form through the utilization of

I am interested in designing an HTML page that includes a small form. The form should include: Name Gender Date of Birth "I Agree" checkbox Submit button One important condition to note is that if the individual's age falls between 20 and 25 (calc ...

What adjustments should I make for mobile cell phone browsing to enable the player to automatically jump upon tapping the screen?

How can I modify the code to make the player move automatically to the right and jump on tap in a mobile cell phone browser? I've been searching for an answer but haven't found one yet, as I'm still learning. Here is the updated code: updat ...

Angular 2 FileReader: A comprehensive guide

I have a situation where I need to upload an image in section X and pass the base 64 data of the uploaded image to section Y. But I am encountering some difficulties in section X HTML: <input type="file" id="hotspot" (change)="uploadHotSpot($event)"&g ...

Issue with Vue.js hot reload: Property 'Ctor' is undefined and cannot be read

Encountering an error after updating: index.js:106 TypeError: Cannot read property 'Ctor' of undefined at index.js:202 at Object.reload (index.js:104) at reload (hot-reload.ts:17) at router.ts:30 at hotApply (bootstrap 796061 ...

Tic Tac Toe is not functioning properly

Hey there! Can someone please help me figure out what's going on? I'm trying to create a Tic Tac Toe game, but instead of using "O" and "X" to mark the fields, I want to use different colors. Specifically, one player should be able to mark with b ...

Tips for removing a WordPress theme to customize plugin styles and scripts

Hello everyone! I am currently using the Kalixo Magazine theme for my WordPress website. Despite trying out various widget menu plugins with different designs, I've noticed that the default theme design keeps overriding them. Is there a way to prevent ...

Incorporating personalized CSS and JavaScript into Shopify

Currently, my project involves integrating vertical tabs into a Shopify page that is utilizing the 'Atlantic' theme. Since this particular theme doesn't come with vertical tabs built-in, I have incorporated external JS and CSS files known as ...

Establish a table containing rows derived from an object

I am currently dealing with a challenge in creating a table that contains an array of nested objects. The array I have follows this schema: array = [ { id: 'Column1', rows: { row1: 'A', row2 ...

Properly storing information in the mongoDB

Having trouble saving data in my Angular application correctly. Here is a snippet of my API code: .post('/type/add', function(req, res){ var type = new NewType({ type: req.body.type, subtype: req.body.subtype, }); type.save ...

creating an identical copy of a dynamic element's size

I am currently working on a project using react js where I need to incorporate multiple images. My goal is to have standard background divs / rectangles that match the exact size of each image, allowing for animations to be performed on top of them. The sn ...

Menu background changes color while hovering over a new item being added

Currently working on a straightforward menu that allows users to add new items. The issue arises when the user clicks on the last div ("new item"), causing a new item to be created and shifting the last item to the right, resulting in a flickering hover ef ...

Detect prohibited terms with Jquery

Check out this jsfiddle. I need assistance with a feature where certain specific words trigger an alert for the user when typed. Currently, if I type a phrase like I love ants, the entire line gets alerted. However, I want only the word ants to trigger th ...

Unable to generate a JSON string from the JavaScript object

I need help converting the object I receive from the server into a JSON file for use in a d3.js chart: data = { "dog ":"5", "cat ":"4", "fish ":"12", } The desired output is: { "name" : "animal", "children" : [ {"name":"dog" ...

Exploring AngularJS capabilities: Retrieving data and headers from an HttpResponse

Currently, I have a REST API developed using Spring Boot. In the frontend, AngularJS 1.5 is being used. The login service not only sends data but also includes a header. I am wondering how I can effectively read both the data and header on the AngularJS ...

Are there any unique purposes for _id in MongoDB?

I have a nodejs application that defines answers like this: var answerSchema = mongoose.Schema({ session : String, // the ID of the session question : String, // the ID of the question answer : Number // the selected answer (1-5) }); whic ...

Error: The combination of 0 and .... is invalid and cannot be used as a function

I am currently in the process of developing a next.js application using Material-ui. I have been attempting to integrate material-ui into my project. Following guidance from the official GitHub page, I have copied the _app.js , _document.js , theme.js fil ...

Enabling or disabling mouse scroll wheel function across various web browsers

I am facing an issue with enabling/disabling mouse scroll using two buttons: "disable_scroll" and "enable_scroll". The code for disabling scroll works perfectly fine: var cancelscroll = function(e) { e.preventDefault(); }; $("#disable_scroll"). ...