ElementUI中统计Table筛选结果数量

在这里插入图片描述1、el-table标签栏添加@filter-change="filterChange"

2、需过滤的列el-table-column加上

:filters="type"
:filter-multiple='false'
filter-placement='right-end'

3、初始化页面界面显示数据

created () {
    this.tableData = this.tableTempData // tableTempData为临时静态数据
}

4、监听计算表格数量

computed: {
    num: function () {
        return this.tableData.length
    }
}

5、过滤事件

filterChange (filters) {
    for (const key in filters) {
        if (filters[key].length > 0) {
            // 配合data中定义的数据枚举数组type,确定操作的是那一列
            if (filters[key][0].substr(0, 1) === 'p') {
                let queryParams = null
                // 获取选中的枚举值
                queryParams = filters[key][0].substr(1, 2)
                // 1、用的是静态数据,根据枚举值确定tableData
                // 2、如果用的是http请求的话,将queryParams作为参数去获取结果集,赋值给tableData即可
                if (queryParams === '') {
                    this.tableData = this.tableTempData
                } else {
                    this.tableData = this.tableTempData.filter((item) => item.status === queryParams)
                }
            }
        }
    }
}

6、隐藏掉组件自带的全部枚举,使用自定义的

.el-table-filter__list li:first-child {
  display: none;
}

7、单页面的完整代码

<template>
  <div class="ele-table">
    <h3>数量({{num}})</h3>
    <el-table ref="filterTable"
              :data="tableData"
              style="width: 100%"
              @filter-change="filterChange">
      <el-table-column prop="date"
                       label="日期"
                       width="180">
      </el-table-column>
      <el-table-column prop="name"
                       label="姓名"
                       width="180">
      </el-table-column>
      <el-table-column prop="status"
                       label="订单状态"
                       width="180"
                       column-key="status"
                       :filters="type"
                       :filter-multiple='false'
                       filter-placement='right-end'>
        <template slot-scope='scope'>
          <span v-if='scope.row.status === "1"'>状态01</span>
          <span v-if='scope.row.status === "2"'>状态02</span>
          <span v-if='scope.row.status === "3"'>状态03</span>
          <span v-if='scope.row.status === "4"'>状态04</span>
        </template>
      </el-table-column>
      <el-table-column prop="address"
                       label="地址"
                       :formatter="formatter">
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
export default {
  data () {
    return {
      tableData: [],
      tableTempData: [{
        date: '2016-05-02',
        name: '王小虎',
        status: '1',
        address: '上海市普陀区金沙江路 1518 弄',
      }, {
        date: '2016-05-04',
        name: '王小虎',
        status: '2',
        address: '上海市普陀区金沙江路 1517 弄',
      }, {
        date: '2016-05-01',
        name: '王小虎',
        status: '2',
        address: '上海市普陀区金沙江路 1519 弄',
      }, {
        date: '2016-05-03',
        name: '王小虎',
        status: '4',
        address: '上海市普陀区金沙江路 1516 弄',
      }],
      type: [
        {
          text: '全部',
          value: 'p'
        }, {
          text: '状态01',
          value: 'p1'
        }, {
          text: '状态02',
          value: 'p2'
        },
        {
          text: '状态03',
          value: 'p3'
        }, {
          text: '状态04',
          value: 'p4'
        }
      ],
    }
  },
  created () {
    this.tableData = this.tableTempData
  },
  computed: {
    num: function () {
      return this.tableData.length
    }
  },
  methods: {
    formatter (row) {
      return row.address
    },
    filterChange (filters) {
      for (const key in filters) {
        if (filters[key].length > 0) {
          if (filters[key][0].substr(0, 1) === 'p') {
            let queryParams = null
            queryParams = filters[key][0].substr(1, 2)
            if (queryParams === '') {
              this.tableData = this.tableTempData
            } else {
              this.tableData = this.tableTempData.filter((item) => item.status === queryParams)
              console.log(queryParams, this.tableData, filters)
            }
          }
        }
      }
    }
  }
}
</script>


<style>
.el-table-filter__list li:first-child {
  display: none;
}
</style>
<style scoped>
.ele-table {
  margin-top: 20px;
}
h3 {
  text-indent: 2rem;
}
.el-table--fit {
  margin-top: 20px;
  padding: 0 30px 30px 30px;
  border-top: 1px solid #ccc;
}
</style>
  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值