当前位置:   article > 正文

二次封装el-table 实现过滤列_el-table过滤

el-table过滤

当表格列过多时,实现动态展示列,以下是实现代码:

1.my-filter-column.vue

  1. <template>
  2. <div>
  3. <el-dropdown
  4. trigger="click"
  5. class="icon"
  6. :hide-on-click="false"
  7. >
  8. <slot class="icon"></slot>
  9. <!-- <el-icon class="el-icon--right icon"></el-icon> -->
  10. <!-- <i class="el-icon--right" title="过滤表格列"></i> -->
  11. <template #dropdown>
  12. <el-dropdown-menu >
  13. <div class="options">
  14. <el-button
  15. @click="selectAll"
  16. title="全部选中"
  17. >
  18. 全选
  19. </el-button>
  20. </div>
  21. <el-dropdown-item
  22. v-for="item in Object.values(this.columnList)"
  23. :key="item.label"
  24. >
  25. <el-checkbox
  26. :label="item.label"
  27. v-model="item.show"
  28. @change="handleChange"
  29. />
  30. </el-dropdown-item>
  31. </el-dropdown-menu>
  32. </template>
  33. </el-dropdown>
  34. </div>
  35. </template>
  36. <script>
  37. import { onMounted, reactive, toRefs } from 'vue-demi'
  38. import { useRouter } from 'vue-router'
  39. export default {
  40. props: {
  41. columnList: {
  42. type: Object,
  43. default: () => ({})
  44. }
  45. },
  46. name: 'my-filter-column',
  47. setup(props) {
  48. const date = toRefs(props)
  49. const router = useRouter()
  50. const state = reactive({
  51. getColumnList: date.columnList.value
  52. })
  53. onMounted(() => {
  54. })
  55. const handleChange = () => {
  56. let path = router.currentRoute
  57. let filterCol = localStorage.filterColumn ? JSON.parse(localStorage.filterColumn) : {}
  58. filterCol[path] = date.columnList.value
  59. localStorage.filterColumn = JSON.stringify(filterCol)
  60. }
  61. const selectAll = () => {
  62. for (let key in date.columnList.value) {
  63. if (date.columnList.value[key].show) continue
  64. date.columnList.value[key].show = true
  65. }
  66. }
  67. return{
  68. ...toRefs(state),
  69. handleChange,
  70. selectAll
  71. }
  72. },
  73. }
  74. </script>
  75. <style scoped lang="scss">
  76. .options{
  77. margin-top: 5px;
  78. display: flex;
  79. justify-content: center;
  80. }
  81. .icon{
  82. float: right;
  83. cursor: pointer;
  84. padding-bottom: 10px;
  85. }
  86. </style>

2.引入my-filter-column.vue

import myFilterColumn from './my-filter-column.vue';

3.注册组件

  1. components: {
  2. myFilterColumn,
  3. }

4.组件的使用

  1. <template>
  2. <div>
  3. <my-filter-column
  4. :column-list="tableColumn"
  5. >{{filterName}}</my-filter-column>
  6. <el-table
  7. :data="data"
  8. border
  9. stripe
  10. :header-cell-style="{textAlign: 'center'}"
  11. :cell-style="{ textAlign: 'center' }"
  12. style="width: 100%">
  13. <template v-for="(item) in col">
  14. <el-table-column
  15. :key="item.prop"
  16. v-if="setKey(`${item.label}`, `${item.prop}`)"
  17. :prop="item.prop"
  18. :label="item.label"
  19. >
  20. </el-table-column>
  21. </template>
  22. </el-table>
  23. </div>
  24. </template>
  25. <script>
  26. import myFilterColumn from './my-filter-column.vue';
  27. import { reactive, toRefs, computed } from 'vue-demi';
  28. export default {
  29. components: {
  30. myFilterColumn,
  31. },
  32. name: 'HelloVue3FilterTable',
  33. props: {
  34. // 传入的表格数据
  35. data: {
  36. type: Array,
  37. default: [],
  38. required: true
  39. },
  40. // 传入的表格列
  41. col: {
  42. type: Array,
  43. default: [],
  44. },
  45. // 过滤组件名称
  46. filterName: {
  47. type: String,
  48. default: '过滤表格列'
  49. }
  50. },
  51. setup(props) {
  52. const setKey = computed(() => {
  53. return (label, key) => {
  54. // if ( key !== 'options') return state.tableColumn[key] = label
  55. if (state.tableColumn[key] && 'show' in state.tableColumn[key]) return state.tableColumn[key].show
  56. state.tableColumn[key] = {
  57. label,
  58. show: true
  59. }
  60. return true
  61. }
  62. })
  63. const state = reactive({
  64. tableColumn: {}
  65. })
  66. return{
  67. ...toRefs(state),
  68. setKey
  69. }
  70. }
  71. };
  72. </script>
  73. <style lang="scss" scoped>
  74. </style>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/426361
推荐阅读
相关标签
  

闽ICP备14008679号