el-table 合并单元格:如何实现部分成功部分失败的效果?

DDD
发布: 2024-11-13 15:15:17
原创
541人浏览过

el-table 合并单元格:如何实现部分成功部分失败的效果?

el-table合并部分成功部分不成功

我们希望可以实现以下效果:

<img src="效果图.png" alt="效果图">

代码实现:

<el-table :data="waterData" border :span-method="handleSpanM">
  <el-table-column align="center" width="65">
    <template slot-scope="scope">{{scope.row.name }}</template>
  </el-table-column>
  <el-table-column align="center" width="70" label="系数">
    <template slot-scope="scope"><el-input size="mini" class="" v-model="scope.row.factor"></el-input></template>
  </el-table-column>
  <el-table-column align="center" width="120" label="等级分数">
    <template slot-scope="scope"><el-input size="mini" v-model="scope.row.grade"></el-input></template>
  </el-table-column>
  <el-table-column align="center" width="180" label="符号选择">
    <template slot-scope="scope">
      <div class="symbol">
        <span style="display: none;">{{ scope.row.symbol }}</span>
        <span class="symbol_range"></span>
      </div>
    </template>
  </el-table-column>
  <el-table-column
    width="120"
    v-for="(column,index) in 6"
    :key="`column-${index}`"
  >
    <template slot="header" slot-scope="scope">
      <div>
        <el-input size="small" v-model="waterForm[`water${index + 1}_label`]" ></el-input>
      </div>
    </template>
    <template slot-scope="scope">
      <div>
        <el-input size="small" v-model="waterForm[`water${index + 1}_factor`]" ></el-input>
      </div>
    </template>
  </el-table-column>
</el-table>
登录后复制
export default {
  data() {
    return {
      tableData:[
        {name:'降水(mm)',factor:'0.7',grade:'',symbol: '1','':'','':'','':'','':'','':'','':''},
        {name:'降水(mm)',factor:'0.7',grade:'',symbol: '1','':'','':'','':'','':'','':'','':''},
        {name:'降水(mm)',factor:'0.7',grade:'',symbol: '1','':'','':'','':'','':'','':'','':''},
        {name:'降水(mm)',factor:'0.7',grade:'',symbol: '1','':'','':'','':'','':'','':'','':''},
        {name:'风速(m/s)',factor:'0.5',grade:'',symbol: '0','':'','':'','':'','':'','':'','':''},
        {name:'风速(m/s)',factor:'0.5',grade:'',symbol: '0','':'','':'','':'','':'','':'','':''},
        {name:'风速(m/s)',factor:'0.5',grade:'',symbol: '0','':'','':'','':'','':'','':'','':''},
        {name:'风速(m/s)',factor:'0.5',grade:'',symbol: '0','':'','':'','':'','':'','':'','':''},
      ],
      colFields:['name','factor','grade','symbol','','','','','',''],
      spanArr:[],
      waterForm:{},
    };
  },
  methods: {
    getSpanArr() {
      for (let i = 0; i < this.tableData.length; i++) {
        let row = i;
        // let col = i % this.colCount;
        if (row === 0) {
          // i 表示行 j表示列
          for (let j = 0; j < this.colFields.length; j++) {
            this.spanArr[i * this.colFields.length + j] = {
              rowspan: 1,
              colspan: 1,
            };
          }
        } else {
          for (let j = 0; j < this.colFields.length; j++) {
            if (
              this.colFields[j] == "name" ||
              this.colFields[j] == "factor" ||
              this.colFields[j] == "symbol"
            ) { // 指定合并哪些列
            /*this.tableData[row]["School"] !==
                  this.tableData[row - 1]["School"]*/
              if (
                this.tableData[row][this.colFields[j]] !==
                  this.tableData[row - 1][this.colFields[j]]
              ) { // 哪些不合并:School不一样的,不合并
                this.spanArr[row * this.colFields.length + j] = {
                  rowspan: 1,
                  colspan: 1,
                };
              } else if (
                this.tableData[row][this.colFields[j]] ===
                this.tableData[row - 1][this.colFields[j]]
              ) {
                let beforeItem =
                  this.spanArr[(row - 1) * this.colFields.length + j];
                this.spanArr[row * this.colFields.length + j] = {
                  rowspan: 1 + beforeItem.rowspan,// 合并几列
                  colspan: 1,// 合并几行
                };
                beforeItem.rowspan = 0;
                beforeItem.colspan = 0;
              } else {
                // rowspan 和 colspan 都为1表格此单元格不合并
                this.spanArr[row * this.colFields.length + j] = {
                  rowspan: 1,
                  colspan: 1,
                };
              }
            }
          }
        }
      }
      // 对数据进行倒序
      let stack = [];
      for (let i = 0; i < this.colFields.length; i++) {
        for (let j = 0; j < this.tableData.length; j++) {
          // console.log("i=" + i + " j=" + j);
          // i 表示列 j表示行
          if (j === 0) {
            if (this.spanArr[j * this.colFields.length + i].rowspan === 0) {
              stack.push(this.spanArr[j * this.colFields.length + i]);
            }
          } else {
            if (this.spanArr[j * this.colFields.length + i].rowspan === 0) {
              stack.push(this.spanArr[j * this.colFields.length + i]);
            } else {
              stack.push(this.spanArr[j * this.colFields.length + i]);
              while (stack.length > 0) {
                let pop = stack.pop();
                let len = stack.length;
                this.spanArr[(j - len) * this.colFields.length + i] = pop;
              }
            }
          }
        }
      }
      // console.log(this.spanArr);
    },
    objectSpanMethod({ row, column, rowIndex, columnIndex }) {
        return this.spanArr[rowIndex * this.colFields.length + columnIndex];
    },
  },
  mounted() {
    this.getSpanArr();
  },
};
登录后复制

注意,以上代码中,我们定义了 handleSpanM 方法来处理表格合并。但是,代码中并没有使用这个方法。你可以根据需要修改代码来使用这个方法。

火龙果写作
火龙果写作

用火龙果,轻松写作,通过校对、改写、扩展等功能实现高质量内容生产。

火龙果写作 106
查看详情 火龙果写作

此外,如果你希望后几列表头不可编辑,可以修改以下代码:

<el-input size="small" v-model="waterForm[`water${index + 1}_label`]" ></el-input>
登录后复制

改成:

<el-input size="small" disabled v-model="waterForm[`water${index + 1}_label`]" ></el-input>
登录后复制

以上就是el-table 合并单元格:如何实现部分成功部分失败的效果?的详细内容,更多请关注php中文网其它相关文章!

相关标签:
最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门推荐
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号