vue+element-ui table实现表格列筛选

需求:
表格数据根据筛选数据动态展示,可全选/取消全选或单独选择展示。
如下图:在这里插入图片描述
代码地址:https://gitee.com/tudoumlp/just1.git 文件目录下的(vue-ele-demo).

如下:

1,结构部分:

<!-- 表格列筛选 -->
    <div style="text-align: right;margin-bottom: 0.5rem;">
      <el-popover placement="right" width="" trigger="click">
          <el-checkbox-group v-model="tableFilterData.checkBoxList">
            <template v-for="(item, i) in tableFilterData.dataNow2">
            <el-checkbox :label="item.attr" :key="i" checked style="display: block;margin:10px;" @change="filterFunHandle('filter',item.attr,i)">
              {{item.label}}
            </el-checkbox>
            </template>
          </el-checkbox-group>
          <el-button size="small" slot="reference"><i class="el-icon-arrow-down el-icon-menu" /> 列筛选</el-button>
          <el-button size="small" type="text" @click="filterFunHandle('allchecked')">全选</el-button>
          <el-button size="small" type="text" @click="filterFunHandle('cancel')">取消全选</el-button>
      </el-popover>
    </div>
    <!-- 筛选表格 -->
    <el-table size="small" border :data="tableFilterData.dataList" style="width: 100%;">
        <el-table-column type="selection" fixed="left" header-align="center" align="center" width="50"></el-table-column>
        <template v-for="(col,index) in tableFilterData.dataNow">
          <el-table-column :key="index" align="center" :prop="col.attr" :label="col.label" min-width="180">
          </el-table-column>
        </template>
        <el-table-column label="操作" fixed="right" header-align="center" align="center" width="" min-width="170">
          <template slot-scope="scope">
            <el-button type="text" size="small">修改</el-button>
          </template>
        </el-table-column>
      </el-table>

2,数据部分:数据都是模拟数据
dataList是表格数据,dataNow和dataNow2是需要进行筛选控制是否显示的数据,之所以是两个,是因为一个dataNow2需要保存默认的数据,dataNow是操作时候动态变化的数据,如[{label: “性别”, attr: “sex”},{label: “姓名”, attr: “name”}] 表示只显示姓名和性别这两个列。
checkLabels是表格所有的表头数据,checkBoxList是操作时候动态变化的表头标题数据,如[“name”, “sex”] 表示只显示姓名和性别这两列数据。

data () {
    return {
      // 表格列筛选数据
      tableFilterData:{
        dataList: [
          {name: "张三", sex: "男", age: "22", address: "天津", 'position': '人'},
          {name: "李四", sex: "保密", age: "45", address: "河北", 'position': '人'},
          {name: "王五", sex: "女", age: "26", address: "北京", 'position': '人'}
        ],
        dataNow2: [
          {label: "姓名", attr: "name"},
          {label: "性别", attr: "sex"},
          {label: "年龄", attr: "age"},
          {label: "角色", attr: "position"},
          {label: "地址", attr: "address"}
        ],
        dataNow: [
          {label: "姓名", attr: "name"},
          {label: "性别", attr: "sex"},
          {label: "年龄", attr: "age"},
          {label: "角色", attr: "position"},
          {label: "地址", attr: "address"}
        ],
        checkLabels: [], // 筛选列显示多选框数据
        checkBoxList: [] // 筛选列数据
      }
    }
  }

3,方法部分:全选,取消全选,单独选择都在此方法。

// 列筛选
    filterFunHandle (type, currentItem, index) {
      if (type === 'filter') { // 筛选列
        if (event.target.checked === false) {
          let addItem = this.tableFilterData.dataNow.filter((item, index, arr) => {
            return currentItem !== item.attr
          })
          this.tableFilterData.dataNow = addItem
        } else {
          let checkData = this.tableFilterData.dataNow2.filter((item, index, arr) => {
            return currentItem === item.attr
          })
          checkData.map((item, index, arr) => {
            this.tableFilterData.dataNow.unshift(item)
          })
        }
      } else if (type === 'allchecked') { // 全选
        if (this.tableFilterData.checkLabels.length === 0) {
          this.tableFilterData.dataNow2.forEach((result) => {
            this.tableFilterData.checkLabels.push(result.attr)
          })
          this.tableFilterData.checkBoxList = this.tableFilterData.checkLabels // 复选框置为复选所有值,全部选择
          this.tableFilterData.dataNow = this.tableFilterData.dataNow2
        } else {
          this.tableFilterData.checkBoxList = this.tableFilterData.checkLabels // 复选框置为复选所有值,全部选择
          this.tableFilterData.dataNow = this.tableFilterData.dataNow2
        }
      } else if (type === 'cancel') { // 取消全选
        this.tableFilterData.checkBoxList = [] // 复选框置为空,全部不选择
        this.tableFilterData.dataNow = []
      }
    }

4,完整vue文件:

<template>
  <div class="table">
    <el-divider content-position="left">表格筛选</el-divider>
    <!-- 表格列筛选 -->
    <div style="text-align: right;margin-bottom: 0.5rem;">
      <el-popover placement="right" width="" trigger="click">
          <el-checkbox-group v-model="tableFilterData.checkBoxList">
            <template v-for="(item, i) in tableFilterData.dataNow2">
            <el-checkbox :label="item.attr" :key="i" checked style="display: block;margin:10px;" @change="filterFunHandle('filter',item.attr,i)">
              {{item.label}}
            </el-checkbox>
            </template>
          </el-checkbox-group>
          <el-button size="small" slot="reference"><i class="el-icon-arrow-down el-icon-menu" /> 列筛选</el-button>
          <el-button size="small" type="text" @click="filterFunHandle('allchecked')">全选</el-button>
          <el-button size="small" type="text" @click="filterFunHandle('cancel')">取消全选</el-button>
      </el-popover>
    </div>
    <!-- 筛选表格 -->
    <el-table size="small" border :data="tableFilterData.dataList" style="width: 100%;">
        <el-table-column type="selection" fixed="left" header-align="center" align="center" width="50"></el-table-column>
        <template v-for="(col,index) in tableFilterData.dataNow">
          <el-table-column :key="index" align="center" :prop="col.attr" :label="col.label" min-width="180">
          </el-table-column>
        </template>
        <el-table-column label="操作" fixed="right" header-align="center" align="center" width="" min-width="170">
          <template slot-scope="scope">
            <el-button type="text" size="small">修改</el-button>
          </template>
        </el-table-column>
      </el-table>
  </div>
</template>

<script>
export default {
  data () {
    return {
      // 表格列筛选数据
      tableFilterData:{
        dataList: [
          {name: "张三", sex: "男", age: "22", address: "天津", 'position': '人'},
          {name: "李四", sex: "保密", age: "45", address: "河北", 'position': '人'},
          {name: "王五", sex: "女", age: "26", address: "北京", 'position': '人'}
        ],
        dataNow2: [
          {label: "姓名", attr: "name"},
          {label: "性别", attr: "sex"},
          {label: "年龄", attr: "age"},
          {label: "角色", attr: "position"},
          {label: "地址", attr: "address"}
        ],
        dataNow: [
          {label: "姓名", attr: "name"},
          {label: "性别", attr: "sex"},
          {label: "年龄", attr: "age"},
          {label: "角色", attr: "position"},
          {label: "地址", attr: "address"}
        ],
        checkLabels: [], // 筛选列显示多选框数据
        checkBoxList: [] // 筛选列数据
      }
    }
  },
  methods: {
    // 列筛选
    filterFunHandle (type, currentItem, index) {
      if (type === 'filter') { // 筛选列
        if (event.target.checked === false) {
          let addItem = this.tableFilterData.dataNow.filter((item, index, arr) => {
            return currentItem !== item.attr
          })
          this.tableFilterData.dataNow = addItem
        } else {
          let checkData = this.tableFilterData.dataNow2.filter((item, index, arr) => {
            return currentItem === item.attr
          })
          checkData.map((item, index, arr) => {
            this.tableFilterData.dataNow.unshift(item)
          })
        }
      } else if (type === 'allchecked') { // 全选
        if (this.tableFilterData.checkLabels.length === 0) {
          this.tableFilterData.dataNow2.forEach((result) => {
            this.tableFilterData.checkLabels.push(result.attr)
          })
          this.tableFilterData.checkBoxList = this.tableFilterData.checkLabels // 复选框置为复选所有值,全部选择
          this.tableFilterData.dataNow = this.tableFilterData.dataNow2
        } else {
          this.tableFilterData.checkBoxList = this.tableFilterData.checkLabels // 复选框置为复选所有值,全部选择
          this.tableFilterData.dataNow = this.tableFilterData.dataNow2
        }
      } else if (type === 'cancel') { // 取消全选
        this.tableFilterData.checkBoxList = [] // 复选框置为空,全部不选择
        this.tableFilterData.dataNow = []
      }
    }
  }
}
</script>

仅做一丢丢分享和记录,若有错误也烦请大佬指正,谢谢哇哇哇~~

  • 12
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 9
    评论
实现虚拟表格,可以在 `el-table` 组件中添加 `:row-height` 属性,来指定每行的高度,然后再添加 `:height` 属性,来指定表格的高度。接着,可以在 `el-table` 中添加 `slot-scope` 属性,来自定义表格的每行内容。最后,在 `el-table` 中添加 `:highlight-current-row` 属性,来高亮展示当前行。 以下是一个示例代码: ```html <template> <el-table :data="tableData" :row-height="rowHeight" :height="tableHeight" :highlight-current-row="false"> <el-table-column prop="name" label="姓名"></el-table-column> <el-table-column prop="age" label="年龄"></el-table-column> <el-table-column prop="address" label="地址"></el-table-column> <template slot-scope="{ row }"> <div class="table-row" :style="{ height: rowHeight + 'px' }"> <span>{{ row.name }}</span> <span>{{ row.age }}</span> <span>{{ row.address }}</span> </div> </template> </el-table> </template> <script> export default { data() { return { tableData: [ { name: '张三', age: 20, address: '北京市' }, { name: '李四', age: 25, address: '上海市' }, { name: '王五', age: 30, address: '广州市' }, { name: '赵六', age: 35, address: '深圳市' } ], rowHeight: 50, tableHeight: 200 } } } </script> <style> .table-row { display: flex; justify-content: space-between; align-items: center; padding: 0 20px; box-sizing: border-box; border-bottom: 1px solid #f0f0f0; } </style> ``` 在上述代码中,我们指定了每行高度为 `50px`,表格高度为 `200px`,并且自定义了每行内容的展示方式。同时,我们也添加了一些样式来美化表格
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值