el-tabl 表头自定义筛选过滤

文章展示了如何在Vue应用中使用el-table-column的render-header属性定制表头,并结合el-dropdown实现动态筛选功能的filterData组件实例。

主要代码 renderHeader属性

<el-table-column align="center" prop="customerCode" label="客户编码" min-width="120" sortable :render-header='renderHeader'></el-table-column>

封装组件

<template>
  <el-dropdown trigger="click" ref='dropdownRef'>
    <i class="el-icon-arrow-down"></i>
    <el-dropdown-menu slot="dropdown" class='dropdown'>
      <el-input v-model="search" size='mini' placeholder="请输入过滤内容" />
      <el-checkbox-group v-model="checkList" class="checkbox-group">
        <el-checkbox :label="item.value" class="checkbox" v-for="(item, index) in dataList" :key='index'>{{ item.value }}</el-checkbox>
      </el-checkbox-group>
      <div class='line'></div>
      <div class='button'>
        <el-button :disabled="checkList.length === 0" type='text' @click='select'>筛选</el-button>
        <el-button type='text' @click="reset">重置</el-button>
      </div>
    </el-dropdown-menu>
  </el-dropdown>
</template>

<script>
export default {
  name: 'filterData',
  props: {
    dataArr: {
      type: Array,
      default: () => []
    },
    property: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      search: '',
      visible: false,
      checkList: [],
      dataList: [],
      defaultData: []
    }
  },
  watch: {
    search(newVal) {
      this.handleData(newVal)
    },
    dataArr(newVal) {
      this.defaultData = newVal
      this.dataList = newVal
    }
  },
  methods: {
    handleData(newVal) {
      this.dataList = this.defaultData.filter(item => {
        if (item.value === null) return newVal === ''
        if (typeof item.value === 'string') {
          return item.value.includes(newVal)
        }
        return item.value.toString().includes(newVal)
      })
    },
    select() {
      this.search = ''
      this.$emit('updateFilterData', { data: this.checkList, property: this.property })
      this.$refs.dropdownRef.hide()
    },
    reset() {
      this.search = ''
      this.checkList = []
      this.$emit('updateFilterData', { data: this.checkList, property: this.property })
      this.$refs.dropdownRef.hide()
    }
  }
}
</script>

<style scoped lang='scss'>
.checkbox-group {
  display: flex;
  flex-direction: column;
  max-height: 300px;
  overflow-y: auto;
}
.checkbox {
  margin: 4px 0;
}
.dropdown {
  min-width: 100px;
  padding: 8px;
}
.button {
  display: flex;
  align-items: center;
  justify-content: space-between;
  /deep/ span {
    color: #606266;
  }
}
.dis-button {
  /deep/ span {
    color: #C0C4CC !important;
  }
}
.line {
  width: 100%;
  border-top: 1px dashed #e5e5e5;
}
/deep/ .el-icon-arrow-down {
  font-family: iconfont !important;
  font-size: 17px;
  transform: unset;
  margin-top: 2px;
}
/deep/.el-icon-arrow-down:before {
  content: "\e91d" !important;
}
</style>
import FilterData from './filterData.vue'
export default {
  data() {
    return {
      oldData: []
    }
  },
  methods: {
    updateFilterData(params) {
      const { data, property } = params
      this.filterListData[property] = data
      let arr = this.oldData
      for (const item of Object.keys(this.filterData)) {
        if (item === 'labelListDto') {
          if (this.filterListData[item].length !== 0) {
            arr = arr.filter(it => {
              const boolArr = it.labelListDto.map(val => this.filterListData[item].includes(val.name))
              let bool = false
              boolArr.forEach(e => {
                if (e) bool = e
              })
              return bool
            })
          }
        } else {
          if (this.filterListData[item].length !== 0) {
            arr = arr.filter(it => this.filterListData[item].includes(it[item]))
          }
        }
      }
      this.AllData = arr
    },
    renderHeader(h, { column, $index }) {
      return h('div', {
        style: 'display: flex',
        on: {
          click: function(event) {
            event.stopPropagation()
          }
        }
      }, [
        h('div', column.label),
        h(FilterData, { props: { 'data-arr': this.filterData[column.property], property: column.property }, on: { updateFilterData: this.updateFilterData } })
      ])
    }
  }
}

使用方式 引入import filterData from './filterData'

export default {
  data() {
    return {
      index: 0,
      filterListData: {},
      filterData: {},
      AllData: [],
      propertyNames: [
        'allocationResultStatusName', 'routeCode', 'subOrderSnSum',
        'customerCodeSum', 'skuCodeSum', 'salesQuantitySum',
        'netWeightTotal', 'weightTotal', 'priceTotal',
        'excludeTaxPriceTotal', 'volumeTotal', 'minDispatchVolume', 'minDispatchWeight'
      ]
    }
  },
  mixins: [table, tableHeight, filterData],
  created() {
    this.getAllData()
    this.filterListData = this.propertyNames.reduce((obj, propertyName) => {
      obj[propertyName] = []
      return obj
    }, {})
  },
  watch: {
    AllData: function(newVal, oldVal) {
      this.getSearch()
    }
  },
  methods: {
    getAllData() {
      this.$axios.getLinkListApi(this.id).then(res => {
        this.AllData = res.data
        this.oldData = res.data
      })
    },
    getSearch() {
      this.index += 1
      if (this.index > 1) return
      const propertyNames = this.propertyNames

      const issetData = propertyNames.reduce((obj, propertyName) => {
        obj[propertyName] = []
        return obj
      }, {})

      const obj = propertyNames.reduce((obj, propertyName) => {
        obj[propertyName] = []
        return obj
      }, {})

      this.AllData.forEach(item => {
        propertyNames.forEach(propertyName => {
          if (issetData[propertyName].indexOf(item[propertyName]) === -1) {
            if (propertyName !== 'labelListDto') {
              issetData[propertyName].push(item[propertyName])
              obj[propertyName].push({ text: item[propertyName], value: item[propertyName] })
            } else {
              item[propertyName].map(it => {
                if (issetData[propertyName].indexOf(it.name) === -1) {
                  issetData[propertyName].push(it.name)
                  obj[propertyName].push({ text: it.name, value: it.name })
                }
              })
            }
          }
        })
      })
      this.filterData = obj
    },
    filterHandler(value, row, column) {
      const property = column.property
      return row[property] === value
    }
  }
}

评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值