Vuetify autocomplete similar items are not showing

旧时模样 提交于 2019-12-08 04:50:44

问题


I have custom posts with similar title in my case from my local API and I've tried to show posts by search query from items array.

Data:

{
    "count": 5,
    "entries": [
        {
            "id": 3,
            "title": "Senior developer Python"
        },
        {
            "id": 4,
            "title": "Senior developer Python"
        },
        {
            "id": 5,
            "title": "Senior developer Python"
        }
    ]
}

Vuetify autocomplete code:

  <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"
    return-object
  ></v-autocomplete>

Javascript code:

<script>
  export default {
    data: () => ({
      descriptionLimit: 60,
      entries: [],
      isLoading: false,
      model: null,
      search: null
    }),

    computed: {
      items () {
        return this.entries.map(entry => {
          const Description = entry.title.length > this.descriptionLimit
            ? entry.title.slice(0, this.descriptionLimit) + '...'
            : entry.title

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

    watch: {
      search (val) {  
        // Items have already been requested
        if (this.isLoading) return

        this.isLoading = true

        // Lazily load input items
        fetch('https://api.website.org/posts')
          .then(res => res.json())
          .then(res => {
            const { count, entries } = res
            this.count = count
            this.entries = entries
          })
          .catch(err => {
            console.log(err)
          })
          .finally(() => (this.isLoading = false))
      }
    }
  }
</script>

How I can show in my autocomplete all similar posts by title also?


回答1:


Try to set item-value to id like :

 <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="id"
    return-object
  ></v-autocomplete>

check this pen



来源:https://stackoverflow.com/questions/57038191/vuetify-autocomplete-similar-items-are-not-showing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!