温馨提示:本文翻译自stackoverflow.com,查看原文请点击:laravel - Vue: Search filter on data table
laravel nuxt.js vue.js

laravel - Vue:数据表上的搜索过滤器

发布于 2020-03-27 15:48:29

我有一个数据表列表,我想搜索客户名称。客户名称是从客户表(关系)中获取的。我在代码上尝试了更多时间,但是没有处理。很抱歉,因为我是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>

查看更多

查看更多

提问者
Dem Sikii
被浏览
128
Khairul Habib 2020-01-31 16:59

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

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();
    }
  }
}