出于某种原因,我无法弄清楚如何在Vue正常工作的情况下获得“自动完成”风格的多选。
我正确地设置了在axios块中调用的路由,并且控制器被设置为使用查询作为使用LIKE子句命中数据库的方法,但某些地方出了问题并且我的多选未填充结果从数据库中可以搜索到它。
我在这做错了什么?
路线:
Route::get('search', 'Controller@searchTags')
->name('search');
控制器:
public function searchTags(Request $request)
{
if ($request->get('query')) {
$query = $request->get('query');
$data = TAGS::where('TAG_DATA', 'LIKE', "%{$query}%")->get();
$output = '<ul class="dropdown-menu" style="display:block; position:relative">';
foreach ($data as $row) {
$output .= '<li><a href="#">' . $row->tag_data . '</a></li>';
}
$output .= '</ul>';
return $output;
}
}
刀:
<div id="tagContent">
<multiselect v-model="value" open-direction="bottom" :options="options" :multiple="true" :close-on-select="false" :taggable="true" :clear-on-select="false" :preserve-search="true" placeholder="Add Tag(s)" label="name" track-by="name">
<template slot="selection" slot-scope="{ values, search, isOpen }"><span class="multiselect__single" v-if="values.length && !isOpen">{{ values.length }} options selected</span></template>
</multiselect>
</div>
new Vue({
components: {
Multiselect: window.VueMultiselect.default
},
data () {
return {
value: [],
options: []
}
},
methods: {
read(){
window.axios.get('campaigns/search').then(({ data }) =>{
console.log(data)
});
},
addTag (newTag) {
const tag = {
name: newTag,
code: newTag.substring(0, 2) + Math.floor((Math.random() * 10000000))
}
this.options.push(tag)
this.value.push(tag)
}
}
}).$mount('#tagContent');
我相信你的例子中有一些缺失的东西。
read
函数 - 使用@search-change
事件this.options
,以便multiselect
组件可以使用它们。在this example中,我使用超时模拟了数据请求,但你应该明白这一点。您还可以使用loading
属性让您的用户了解幕后发生的事情。