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

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

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

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

效果图

代码实现:

<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 方法来处理表格合并。但是,代码中并没有使用这个方法。你可以根据需要修改代码来使用这个方法。

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

<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 合并单元格:如何实现部分成功部分失败的效果?的详细内容,更多请关注硕下网其它相关文章!