可以在element-ui el-autocomplete回调中传递数组吗?

时间:2018-10-01 11:38:27

标签: javascript vue.js vue-component element-ui

我尝试将el-autocomplete标记用于最简单的版本:由回调函数(JSFiddle version)返回的Array

Vue.component('button-counter', {
  data: function() {
    return {
      selectdusers: [],
      user: ''
    }
  },
  template: '<el-container>        <el-container style="margin-left: 15px; width: 150px">            <el-autocomplete                class="inline-input"                v-model="user"                :fetch-suggestions="filterUsers"                placeholder="add user"                @select="handleSelect"            ></el-autocomplete>            <el-button type="info" icon="el-icon-plus" click="addUser(user)">Add</el-button>        </el-container>    </el-container>',
  methods: {
    addUser(user) {
      this.selectedUsers.push(user)
      this.user = ''
    },
    filterUsers(user, cb) {
      console.log(user)
      cb(['qqq', 'zzz'])
      //return this.allusers.filter(x => _.includes(x.f+x.t, user))
    },
    handleSelect() {}
  },
})

new Vue({
  el: '#components-demo'
})
@import url("//unpkg.com/element-ui@2.4.8/lib/theme-chalk/index.css");
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/element-ui/2.4.7/index.js"></script>
<div id="components-demo">
  <button-counter></button-counter>
</div>

建议框在视觉上是空的,尽管DevTools显示了<ElAutocompleteSuggestions>datasuggestions中的预期元素

image

image

2 个答案:

答案 0 :(得分:2)

要使用数组而不是对象,可以在自动完成块中使用插槽

<el-autocomplete ...>
  <template slot-scope="{ item }">
    <div class="item">{{ item }}</div>
  </template>
</el-autocomplete>

答案 1 :(得分:0)

需要通过回调传递的是Object,它必须具有一个称为value的属性。此属性保存显示为建议的内容。

对于上面的示例:

filterUsers(user, cb) {
      console.log(user)
      cb([{'value': 'qqq', 'whatever': 'djhjfh'}, {'value': 'zzz', 'whatever': 'uiuiiu'}])
    },