修改源码,element的el-table合并,处理合并产生的hover样式问题

1、确认自己element-ui的版本号

2、此element-ui下的lib包是修改过hover样式的包,如何替换自己文件下的node_modules中的包

  • 修改后将lib文件夹中文件替换你项目中/node_module/element-ui/Lib中的文件

问题??如果替换开发环境中的node_module的包无法升级到测试环境,因为node_module中的包只在你本地

解决办法:需要将代码拷贝到本地分支,修改table文件,然后一起提交

如果不使用element打包之后的lib文件,就进行以下操作

1、在github中找到element-ui的源码,克隆到本地,直接修改。

el-table合并行hover显示问题

作者解决前显示的问题,如下两张图片


作者解决后如下

2、最后产品想要的效果,需求:不区分每一行,只要移入到合并的行,都需要背景颜色

3、需要源码变动的替换下边两个文件

  • 3.1、table/src/table-row.js文件
bash 复制代码
import ElCheckbox from 'element-ui/packages/checkbox';
export default {
  name: 'ElTableRow',
  props: [
    'columns',
    'row',
    'index',
    'isSelected',
    'isExpanded',
    'store',
    'context',
    'firstDefaultColumnIndex',
    'treeRowData',
    'treeIndent',
    'columnsHidden',
    'getSpan',
    'getColspanRealWidth',
    'getCellStyle',
    'getCellClass',
    'handleCellMouseLeave',
    'handleCellMouseEnter',
    'fixed',
    'recordRowIndexMap'
  ],
  components: {
    ElCheckbox
  },
  render() {
    const {
      columns,
      row,
      index: $index,
      store,
      context,
      firstDefaultColumnIndex,
      treeRowData,
      treeIndent,
      columnsHidden = [],
      isSelected,
      isExpanded
    } = this;
    const currentRowIndex = $index + '';
    this.recordRowIndexMap[currentRowIndex] = { maxRowSpen: 0, maxColSpen: 0, distanceAndParallelCoordinates: 0}; // maxRowSpen:当前行中,找出合并的行数, distanceAndParallelCoordinates: 距合并行坐标位置, 当前行是1,那么距离上一合并行的位置是一
    return (
      <tr>
        {
          columns.map((column, cellIndex) => {
            const { rowspan, colspan } = this.getSpan(row, column, $index, cellIndex);
            if (!rowspan || !colspan) {
              const { maxColSpen } = this.recordRowIndexMap[$index];
              if (maxColSpen >= 1) {
                this.recordRowIndexMap[$index].distanceAndParallelCoordinates = 0; // 获取距离上一合并行的坐标0 --- 10
                this.recordRowIndexMap[$index].maxRowSpen = 1; // 获取上一行合并行数
                return null;
              }
              const { distanceAndParallelCoordinates, maxRowSpen } = this.recordRowIndexMap[$index - 1];
              this.recordRowIndexMap[$index].distanceAndParallelCoordinates = distanceAndParallelCoordinates + 1; // 获取距离上一合并行的坐标0 --- 10
              this.recordRowIndexMap[$index].maxRowSpen = maxRowSpen; // 获取上一行合并行数
              return null;
            }

            const { maxRowSpen, maxColSpen } = this.recordRowIndexMap[currentRowIndex];
            if (maxRowSpen < rowspan) {
              this.recordRowIndexMap[currentRowIndex]['maxRowSpen'] = rowspan;
            }

            if (maxColSpen < colspan) {
              this.recordRowIndexMap[currentRowIndex]['maxColSpen'] = colspan;
            }

            const columnData = { ...column };
            columnData.realWidth = this.getColspanRealWidth(columns, colspan, cellIndex);
            const data = {
              store,
              isSelected,
              isExpanded,
              _self: context,
              column: columnData,
              row,
              $index
            };
            if (cellIndex === firstDefaultColumnIndex && treeRowData) {
              data.treeNode = {
                indent: treeRowData.level * treeIndent,
                level: treeRowData.level
              };
              if (typeof treeRowData.expanded === 'boolean') {
                data.treeNode.expanded = treeRowData.expanded;
                // 表明是懒加载
                if ('loading' in treeRowData) {
                  data.treeNode.loading = treeRowData.loading;
                }
                if ('noLazyChildren' in treeRowData) {
                  data.treeNode.noLazyChildren = treeRowData.noLazyChildren;
                }
              }
            }

            const { startRow = -1, endRow = -1 } = store.states.hoverState;
            const cellStartRow = $index;
            const cellEndRow = cellStartRow + rowspan - 1;

            const isHover = cellStartRow <= endRow && cellEndRow >= startRow;

            const cellMouseEnterStartRow = $index - this.recordRowIndexMap[$index].distanceAndParallelCoordinates;
            const cellMouseEnterRowSpan = this.recordRowIndexMap[currentRowIndex]['maxRowSpen'];
            return (
              <td
                style={this.getCellStyle($index, cellIndex, row, column)}
                class={this.getCellClass($index, cellIndex, row, column, isHover)}
                rowspan={rowspan}
                colspan={colspan}
                on-mouseenter={($event) => this.handleCellMouseEnter($event, row, cellMouseEnterStartRow, cellMouseEnterRowSpan)}
                on-mouseleave={this.handleCellMouseLeave}
              >
                {
                  column.renderCell.call(
                    this._renderProxy,
                    this.$createElement,
                    data,
                    columnsHidden[cellIndex]
                  )
                }
              </td>
            );
          })
        }
      </tr>
    );
  }
};
  • 3.2、table/src/table-body.js文件引入,并在TableRow中配置这条属性
  • import { cellStarts } from './config.js';
  • recordRowIndexMap: {} 为对象

问题?修改完之后存在的问题,当点击表格的时候,样式还需要再处理,有问题的效果如下,待优化

解决方案:跟修改hover时的样式一样,找 current-row 这个类名

相关推荐
masa01019 分钟前
JavaScript--JavaScript基础
开发语言·javascript
让开,我要吃人了2 小时前
HarmonyOS开发实战(5.0)实现二楼上划进入首页效果详解
前端·华为·程序员·移动开发·harmonyos·鸿蒙·鸿蒙系统
Passion不晚2 小时前
Vue vs React vs Angular 的对比和选择
vue.js·react.js·前端框架·angular.js
everyStudy3 小时前
前端五种排序
前端·算法·排序算法
甜兒.4 小时前
鸿蒙小技巧
前端·华为·typescript·harmonyos
她似晚风般温柔7897 小时前
Uniapp + Vue3 + Vite +Uview + Pinia 分商家实现购物车功能(最新附源码保姆级)
开发语言·javascript·uni-app
Jiaberrr8 小时前
前端实战:使用JS和Canvas实现运算图形验证码(uniapp、微信小程序同样可用)
前端·javascript·vue.js·微信小程序·uni-app
everyStudy8 小时前
JS中判断字符串中是否包含指定字符
开发语言·前端·javascript
城南云小白8 小时前
web基础+http协议+httpd详细配置
前端·网络协议·http
前端小趴菜、8 小时前
Web Worker 简单使用
前端