搜索框组合筛选(两种方法)

!!!!!!!!!!!!注意把原数组备份!!!!!!!!!!!

1.函数式
<el-form label-width="80px" class="filterForm" :model="filters" @submit.native.prevent>
      <el-form-item label="名称">
        <el-select v-model="filters.name" placeholder="名称" @change="checkboxChange" clearable>
          <el-option v-for="(item, index) in nameList" :label="item" :value="item" :key="index">
          </eloption>
        </el-select>
      </el-form-item>
      <el-form-item label="地址">
        <el-select v-model="filters.host" placeholder="主机地址" @change="checkboxChange" clearable>
          <el-option v-for="(item, index) in HostList" :label="item" :value="item" :key="index"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="类型">
        <el-select v-model="filters.slot_type" placeholder="类型" @change="checkboxChange" clearable>
          <el-option v-for="(item, index) in TypeList" :label="item" :value="item" :key="index"></el-option>
        </el-select>
      </el-form-item>
</el-form>

el-table要绑定一个 :data="computedSlotList"

<script>
    computed: {
          computedSlotList: function() {
            return this.filterTotalArray
          }
        },
    //搜索
      checkboxChange() {
        this.showList()
      },
      showList() {
        console.log( this.filters );
        let arr = this.slotList;
        console.log( arr );
        arr = arr.filter( item => {
          return Object.keys( this.filters ).every( key => {
            if ( this.filters[ key ] !== "" && item[ key ].indexOf( this.filters[ key ] ) === -1 ) {
              return false;
            }
            return true;
          } );
        } );

        console.log( arr );
        this.filterTotalArray = arr;
      },
      filterFunc( val, target, filterArr ) {
        console.log( val );
        // 参数不存在或为空时,就相当于查询全部
        if ( val == undefined || val == '' ) return filterArr
        return filterArr.filter( item => {
          console.log( item );
          return item[ target ].indexOf( val ) > -1
        } )
      },
</script>
2.第二种
showList() {
        let temp = JSON.parse( JSON.stringify(this.slotList) )
        let filteredArray = temp.filter( item => {
        const matchCluter = !this.filters.name || item.name.includes(this.filters.name ) 		
        const matchHost = !this.filters.host || item.host.includes( this.filters.host )
        const matchType = !this.filters.slot_type || item.slot_type.includes( this.filters.slot_type )
        const matchTwo = ( matchCluter && matchHost ) || ( matchHost && matchType ) || ( matchCluter && matchType )
          return matchCluter && matchHost && matchType && matchTwo
        } )
        this.filterTotalArray = filteredArray;
      },

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值