• Element表格之表头合并、单元格合并


    一、合并表头

    1. el-table配置 :header-cell-style="headFirst"
    2. headFirst({ row, colunm, rowIndex, columnIndex }) {
    3. let base = { 'background-color': 'rgba(67, 137, 249, 0.3)', color: '#333', 'text-align': 'center' };
    4. //这里为了是将第一列的表头隐藏,就形成了合并表头的效果
    5. if (rowIndex === 0) {
    6. // 判断对第几列合并 property就是prop传入的属性
    7. if (row[columnIndex].property === 'part1') {
    8. //第一列width扩展2倍
    9. return { ...base, width: '200%', display: 'inline-block' };
    10. } else if (row[columnIndex].property === 'part2') {
    11. //其余列设置font-size 0 隐藏
    12. return { 'font-size': 0 };
    13. }
    14. }
    15. return base;
    16. },

    二、合并表格行

    要进行单元格合并,关键在于你要对数据渲染的前进行判断处理,在这里,首先要在el-table中配置写入 :span-method='spanMethod'  ,然后在js部分写上方法。

    1. spanMethod({ row, column, rowIndex, columnIndex }, key) {
    2. if (columnIndex === 0) {
    3. let data = [];
    4. data = this.dataMap[key].tableData;
    5. const _row = this.flitterData(data).arr[rowIndex];
    6. const _col = _row > 0 ? 1 : 0;
    7. return {
    8. rowspan: _row,
    9. colspan: _col,
    10. };
    11. }
    12. },
    13. flitterData(arr) {
    14. let spanOneArr = [];
    15. let concatOne = 0;
    16. arr.forEach((item, index) => {
    17. if (index === 0) {
    18. spanOneArr.push(1);
    19. } else {
    20. if (item && arr[index - 1] && item.name === arr[index - 1].name) {
    21. // 第一列需合并相同内容的判断条件
    22. spanOneArr[concatOne] += 1;
    23. spanOneArr.push(0);
    24. } else {
    25. spanOneArr.push(1);
    26. concatOne = index;
    27. }
    28. }
    29. });
    30. return {
    31. arr: spanOneArr,
    32. };
    33. },

    一、合并表格列

    如果合并表格行的同时还要考虑合并表格列可以修改spanMethod方法;

    另外element的事件想要传输额外的参数 可以用 :spanMethod="(obj) => spanMethod(obj, key)" 设置多个列的单元格合并

    1. spanMethod({ row, column, rowIndex, columnIndex }, key) {
    2. if (columnIndex === 0 || columnIndex === 1 || columnIndex === 2) {
    3. let data = [];
    4. data = this.dataMap[key].tableData;
    5. const _row = this.flitterData(data, column.property).arr[rowIndex];
    6. const _col = _row > 0 ? this.colData(data, row, columnIndex) : 0;
    7. return {
    8. rowspan: _row,
    9. colspan: _col,
    10. };
    11. }
    12. },
    13. colData(arr, row, columnIndex) {
    14. // 计算横向合并单元格
    15. if (columnIndex === 1 && row.part1 === row.part2) {
    16. return 2;
    17. } else if (columnIndex === 2 && row.part1 === row.part2) {
    18. return 0;
    19. }
    20. return 1;
    21. },
    22. flitterData(arr, key) {
    23. let spanOneArr = [];
    24. let concatOne = 0;
    25. arr.forEach((item, index) => {
    26. if (index === 0) {
    27. spanOneArr.push(1);
    28. } else {
    29. if (item && arr[index - 1] && item[key] === arr[index - 1][key]) {
    30. // 第一列需合并相同内容的判断条件
    31. spanOneArr[concatOne] += 1;
    32. spanOneArr.push(0);
    33. } else {
    34. spanOneArr.push(1);
    35. concatOne = index;
    36. }
    37. }
    38. });
    39. return {
    40. arr: spanOneArr,
    41. };
    42. },

  • 相关阅读:
    Redis集群及分布式锁
    opencv_5_图像像素的算术操作
    Typecho用宝塔面板建站(保姆级教程)
    大数据面试重点之mysql篇
    微软在2022年Gartner云计算AI开发者服务魔力象限中被评为“领导者”
    手摸手,带你实现移动端H5瀑布流布局
    FPGA——SPI总线控制flash(3)含代码
    Mac app 公证处理流程
    如何搭建社群运营体系?试试快鲸scrm
    2024第八届图像、信号处理和通信国际会议 (ICISPC 2024)即将召开!
  • 原文地址:https://blog.csdn.net/qq_38188485/article/details/132845088