当前位置:   article > 正文

vue3 列表页开发【选择展示列】功能_vue设置表格列可见

vue设置表格列可见

目录

背景描述:

开发流程:

详细开发流程:

总结:


背景描述:

这个功能是基于之前写的   封装列表页  的功能继续写的,加了一个选择展示列的功能,可以随时控制表格里展示那些列的数据,如图,大概样式是这样:


开发流程

基本上和封装列表页的流程相似,这里不做多余描述,只是需要从父组件里传递tableColumn,也可以在本组件定义

tableColumn除了控制表格的column,还有就是【选择列】的功能的数据从这里来,这里可以设置哪些需要显示与隐藏,如下:

  1. const tableColumn = ref([
  2. {
  3. column_id: 'op_name',
  4. column_name: '操作人',
  5. default_display: true,
  6. sortable: true,
  7. minWidth: 100
  8. },
  9. {
  10. column_id: 'op_roles',
  11. column_name: '角色',
  12. default_display: true,
  13. sortable: true,
  14. minWidth: 150
  15. },
  16. //....
  17. {
  18. column_id: 'create_at',
  19. column_name: '名称12',
  20. default_display: true,
  21. sortable: true,
  22. minWidth: 170
  23. },
  24. {
  25. column_id: 'update_at',
  26. column_name: '名称13',
  27. default_display: false,
  28. sortable: true,
  29. minWidth: 170
  30. }
  31. ])

详细开发流程:

提示:这里描述项目中遇到的问题:

1.选择展示列

  1. <el-col :span="12">
  2. <el-popover placement="bottom" trigger="click" :width="300">
  3. <template #reference>
  4. <el-button class="right-button" type="default">
  5. <el-icon><Filter /></el-icon>
  6. </el-button>
  7. </template>
  8. <span style="margin: 0 10px 0 0; font-size: 14px">选择展示列</span>
  9. <el-select v-model="selectedColumns" multiple collapse-tags :teleported="false" @change="selectColumns">
  10. <el-option
  11. v-for="(item, index) in tableCol"
  12. :key="item.column_id"
  13. :disabled="index == 0"
  14. :label="item.column_name"
  15. :value="item.column_id"
  16. ></el-option>
  17. </el-select>
  18. </el-popover>
  19. </el-col>

这里的tableCol是从父组件传的tableColumn, tableCol.value = props.tableColumn

2.已选择的展示列怎么控制表格的列显隐

  1. // 已选的展示列
  2. const selectedColumns = ref([])
  3. //选择展示列
  4. const selectColumns = () => {
  5. showTableCol.value = []
  6. let arr = []
  7. if (selectedColumns.value.length && selectedColumns.value.length != 0) {
  8. selectedColumns.value.forEach((element) => {
  9. tableCol.value.forEach((item, index) => {
  10. if (index == 0) {
  11. item.default_display = true
  12. }
  13. item.default_display = false
  14. if (element == item.column_id || index == 0) { //比如至少要选择第一列,不能一列都不显示
  15. arr.push(index)
  16. }
  17. })
  18. })
  19. arr = [...new Set(arr)]
  20. arr.forEach((element) => {
  21. tableCol.value[element].default_display = true
  22. })
  23. let dataTable = tableCol.value.filter((item, index) => {
  24. return item.default_display
  25. })
  26. showTableCol.value = dataTable
  27. } else {
  28. let dataTable = []
  29. dataTable = tableCol.value.filter((item) => {
  30. return item.default_display
  31. })
  32. dataTable.forEach((item) => {
  33. selectedColumns.value.push(item.column_id)
  34. })
  35. showTableCol.value = dataTable
  36. }
  37. }

3. 表格的列显示

  1. <el-table
  2. v-loading="loading"
  3. :data="tableData"
  4. class="table-small-custom"
  5. height="calc(100vh - 240px)"
  6. stripe
  7. @sort-change="changeTableSort"
  8. >
  9. <el-table-column type="index" width="70" label="序号">
  10. <template #default="scope">
  11. <span v-text="getIndex(scope.$index)"></span>
  12. </template>
  13. </el-table-column>
  14. <el-table-column
  15. v-for="(col, index) in showTableCol"
  16. :key="index"
  17. :prop="col.column_id"
  18. :label="col.column_name"
  19. :min-width="col.minWidth"
  20. :sortable="col.sortable"
  21. :is-show-overflow-tooltip="true"
  22. />
  23. </el-table>

这里表格的渲染是通过v-for  showTableCol ,主要就是这个。

over


总结:

目前我经常是通过这个方式写【选择展示列】功能,过滤那部分,没怎么考虑最优解,反正数据也不多,直接这样写了,如果有更合适的方式,欢迎分享~

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

闽ICP备14008679号