当前位置:   article > 正文

纵向el-table表格_实现el-table表头纵向显示效果

实现el-table表头纵向显示效果

一、实现效果:

        表头纵向的el-table表格,每个内容的单元格长度为30px,单元格不足6格的补足6格空格,超出6格的用实际内容填充。

二、实现方式:

a、数据结构调整:
  • 通常表格数据是以列为主,每列对应一个字段。为了实现纵向表头,需要将数据结构调整为以行为主,具体来说,每行对应一个表头(例如 "工厂焊(A)","现场焊(M)" 等),然后在每一行中填充列数据。
  • 每行的数据可以以对象的形式定义,label 表示纵向表头的名称,values 数组表示该表头下的列数据。
b、动态生成列:
  • 使用 el-table-column 标签,通过 v-for 动态生成列,并根据行数据的 values 数组进行填充。
  • 这里的关键是 v-for 的应用:通过遍历 values 数组生成多列,每一列的数据来源于 values 的不同索引。

代码实现:

  1. <template>
  2. <div style="width: 300px; font-size: 12px;">
  3. <el-table
  4. :data="processedTableData"
  5. border
  6. :show-header="false"
  7. class="custom-table"
  8. >
  9. <el-table-column
  10. prop="label"
  11. width="120"
  12. >
  13. <template slot-scope="scope">
  14. {{ scope.row.label }}
  15. </template>
  16. </el-table-column>
  17. <el-table-column
  18. v-for="(item, index) in rowLength"
  19. :key="index"
  20. width="30"
  21. >
  22. <template slot-scope="scope">
  23. {{ scope.row.values[index] !== undefined ? scope.row.values[index] : '' }}
  24. </template>
  25. </el-table-column>
  26. </el-table>
  27. </div>
  28. </template>
  29. <script>
  30. export default {
  31. data() {
  32. return {
  33. rowLength: 0,
  34. tableData: [
  35. {
  36. label: "工厂焊(A)",
  37. values: Array(5).fill('')
  38. },
  39. {
  40. label: "现场焊(M)",
  41. values: [4, 5, 6]
  42. },
  43. {
  44. label: "现场调整焊(F)",
  45. values: [7, 8, 9, 9]
  46. }
  47. ]
  48. };
  49. },
  50. computed: {
  51. processedTableData() {
  52. return this.tableData.map(row => {
  53. while (row.values.length < this.rowLength) {
  54. row.values.push('');
  55. }
  56. return row;
  57. });
  58. }
  59. },
  60. mounted() {
  61. // 放在请求到tableData数据后
  62. const maxValuesLength = Math.max(
  63. ...this.tableData.map(row => row.values.length)
  64. );
  65. this.rowLength = maxValuesLength > 6 ? maxValuesLength : 6;
  66. console.log(this.tableData[0].values, 'tableData---');
  67. }
  68. };
  69. </script>

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

闽ICP备14008679号