0

我有一个数据表列表,我想搜索客户的姓名。客户的姓名是从客户表(关系)中获取的。我在我的代码上尝试更多时间,但这不是过程。对不起,因为我是 Vue js 的新手。

在此处输入图像描述

文本域

<div>
  <v-text-field
    label="Search Customer"
    solo 
    outlined
    dense
    v-model="search"
  ></v-text-field>
</div>

脚本

<script>
  export default {

	created() {
		this.fetchSearch();
	},

	watch: {
		search: {
			handler() {
				this.fetchSearch()
			}
		},
		deep: true,
	},

	data() {
		return {
			items: [],
			search: '',
		}
	},

	methods: {
		fetchSearch(){
			this.$axios.$get(`/api/return-sale?search=${this.search}`)
			.then(res =>{
				this.items = res.returnsale.data;
				console.log(res);
			})
			.catch(err =>{
				console.log(err.response);
			})
		},
	}
}

</script>

4

2 回答 2

1

如果我理解正确,您需要一种在前端过滤表格的方法。您可以尝试这样做的一种方法是使用一个计算变量来返回过滤后的列表。

computed: {
 tableItems () {
   if (!this.search) {
    return this.items;
   }

   return this.items.filter(item => item.custormer.indexOf(this.search) > -1)
 }
}

如果您要在不同的地方使用此搜索功能,您还可以查看过滤器

于 2020-01-31T08:12:28.683 回答
1

您可以通过 this.items 的 watch 属性创建一个函数并过滤数据

watch: {
  search() {
    const data = this.items
    if (this.search.length > 0) { 
      if (data.filter(item => item.custormerName === this.search)) { 
        this.items = data.filter(item => item.custormerName === this.search);
      } else {
        this.search = '';
        this.fetchSearch();
      }
    } else {
      this.fetchSearch();
    }
  }
}
于 2020-01-31T08:59:25.193 回答