Vue.js 实现搜索功能:使用 el-button 和 el-input 组件
<template>
<div>
<el-button type="primary" size="mini" @click="handleQuery">查询</el-button>
<el-input
v-show="showSearch"
v-model="queryParams.voucherGroupName"
placeholder="输入抽样模板编码或名称"
clearable
size="small"
@keyup.native.enter="handleQuery"
class="search-input"
style="width: 200px; margin-right: 10px"
></el-input>
<ul>
<li v-for="item in filteredList" :key="item.voucherGroupId">{{ item.voucherGroupId }}</li>
</ul>
</div>
</template>
<script>
export default {
data() {
return {
showSearch: true,
queryParams: {
voucherGroupName: '',
},
voucherGroupList: [],
filteredList: [],
};
},
created() {
this.getList();
},
methods: {
handleQuery() {
if (this.queryParams.voucherGroupName) {
this.filteredList = this.voucherGroupList.filter((item) => {
return (
item.voucherGroupId.includes(this.queryParams.voucherGroupName) ||
item.parentId.includes(this.queryParams.voucherGroupName)
);
});
} else {
this.filteredList = this.voucherGroupList;
}
},
getList() {
this.loading = true;
this.queryParams.accountsId = this.accountsId;
listVoucherGroup(this.queryParams).then((response) => {
this.voucherGroupList = this.handleTree(
response.data,
'voucherGroupId',
'parentId'
);
this.filteredList = this.voucherGroupList;
this.loading = false;
});
},
},
};
</script>
<p>在这个例子中,我们使用了<code>el-button</code>和<code>el-input</code>组件来实现查询功能。当用户点击查询按钮或按下回车键时,会调用<code>handleQuery</code>方法进行过滤操作。过滤后的结果会赋值给<code>filteredList</code>数组,然后在模板中使用<code>v-for</code>指令进行渲染。</p>
<p>注意:在<code>handleQuery</code>方法中,我们使用了<code>this.queryParams.voucherGroupName</code>来获取输入框中的值,而不是<code>this.searchvalue</code>。所以在<code>el-input</code>组件中,我们将<code>v-model</code>绑定到了<code>queryParams.voucherGroupName</code>。</p>
<p>此外,你还需要根据你的实际需求来完善其他部分的代码,比如定义<code>listVoucherGroup</code>方法和<code>handleTree</code>方法等。</p>
AI 优化版
原文地址:https://www.cveoy.top/t/topic/qwOW 著作权归作者所有。请勿转载和采集!