element 表格合并

效果如图 前两列合并

在这里插入图片描述

1.启用函数:
span-method="objectSpanMethod"

在这里插入图片描述

2.创建存储合并行的数组
      item1:[],//存放合并行数据
      item2:[],//存放合并行数据

在这里插入图片描述
数据类型 data[]为表格数据
在这里插入图片描述

3.处理合并数据
  updated: function () {
    let itemDot1 = 0;
    let itemDot2 = 0;
    let item1 = [];
    let item2 = [];
    this.data.forEach((item, index) => {
      if (index === 0) {//第一行
        item1.push(1)
        item2.push(1)
      } else {
        //第一列
        if (item.id === this.data[index - 1].id) {// // 判断当前元素与上一个元素是否相同 data为表格数据对象  id为合并列字段
          item1[itemDot1] += 1;
          item1.push(0)
        } else {
          itemDot1 = index
          item1.push(1)
        }
        //第二列
        if (item.id === this.data[index - 1].id && item.plate === this.data[index - 1].plate) {
          item2[itemDot2] += 1;
          item2.push(0)
        } else {
          itemDot2 = index
          item2.push(1)
        }
      }
    })
    this.item1 = item1;
    this.item2 = item2;
  },

在这里插入图片描述

4.合并表格
    //合并表格
    objectSpanMethod:function ({ row, column, rowIndex, columnIndex }) {
      if (columnIndex === 0) {
        // 第一列的合并方法
        const row1 = this.item1[rowIndex];
        const col1 = row1 > 0 ? 1 : 0; // 如果被合并了row = 0; 则他这个列需要取消
        return {
          rowspan: row1,
          colspan: col1,
        };

      } else if (columnIndex === 1) {
        // 第二列的合并方法
        const row2 = this.item2[rowIndex];
        const col2 = row2 > 0 ? 1 : 0; // 如果被合并了row = 0; 则他这个列需要取消
        return {
          rowspan: row2,
          colspan: col2,
        };
      }

    },

在这里插入图片描述

全部代码
<template>
  <basic-container>
    <avue-crud
      :option="option"
      :span-method="objectSpanMethod"
      :table-loading="loading"
      :data="data"
      :page="page"
      :permission="permissionList"
      :before-open="beforeOpen"
      v-model="form"
      ref="crud"

      @row-update="rowUpdate"
      @row-save="rowSave"
      @row-del="rowDel"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @on-load="onLoad"
    >

      <template slot-scope="scope" slot="rangeDateSearch">
        <el-date-picker v-model="query.rangeDate" type="daterange" size="mini" value-format="yyyy-MM-dd" range-separator="-"></el-date-picker>
      </template>

      <!--列表导航操作-->
      <template slot="menuLeft">
        <el-button size="small" type="warning"  @click="excel2" icon="el-icon-document">导出excel</el-button>
      </template>

    </avue-crud>
  </basic-container>

</template>

<script>
import {
  getList,excel2
} from "@/api/cash/finalPrintExcel";
import {getCurrentMonthFirstDay, getCurrentMonthLastDay} from "@/util/date";
import {mapGetters} from "vuex";

export default {
  data() {
    return {
      item1:[],//存放合并行数据
      item2:[],//存放合并行数据

      form: {},
      query: {
        dateStart: '',
        dateEnd: '',
        rangeDate: [getCurrentMonthFirstDay(), getCurrentMonthLastDay()],
      },
      loading: true,
      page: {
        pageSize: 20,
        currentPage: 1,
        total: 0
      },
      selectionList: [],
      option: {
        height: "auto",
        menuWidth: 120,
        tip: false,
        border: true,
        menu: false,
        viewBtn: false,
        editBtn: false,
        addBtn: false,
        delBtn: false,
        cellBtn: false,
        printBtn: true,
        headerAlign: "center",
        searchMenuSpan: 6,
        align: "center",
        title:'运输结算单',
        column: [
          {
            label: "单号",
            prop: "id",
          },
          {
            label: "车号",
            prop: "plate",
          },
          {
            label: "类型",
            prop: "flag",
          },

          {
            label: "明细",
            prop: "flowType",
          },
          {
            label: "金额",
            prop: "amount",
          },

        ]
      },
      data: []
    };
  },

  computed: {
    ...mapGetters(["permission"]),
    permissionList() {
      return {};
    },
    ids() {
      let ids = [];
      this.selectionList.forEach(ele => {
        ids.push(ele.id);
      });
      return ids.join(",");
    }
  },

  updated: function () {
    let itemDot1 = 0;
    let itemDot2 = 0;
    let item1 = [];
    let item2 = [];
    this.data.forEach((item, index) => {
      if (index === 0) {//第一行
        item1.push(1)
        item2.push(1)
      } else {
        //第一列
        if (item.id === this.data[index - 1].id) {// // 判断当前元素与上一个元素是否相同 data为表格数据对象  id为合并列字段
          item1[itemDot1] += 1;
          item1.push(0)
        } else {
          itemDot1 = index
          item1.push(1)
        }
        //第二列
        if (item.id === this.data[index - 1].id && item.plate === this.data[index - 1].plate) {
          item2[itemDot2] += 1;
          item2.push(0)
        } else {
          itemDot2 = index
          item2.push(1)
        }
      }
    })
    this.item1 = item1;
    this.item2 = item2;
  },
  methods: {

    //合并表格
    objectSpanMethod:function ({ row, column, rowIndex, columnIndex }) {
      if (columnIndex === 0) {
        // 第一列的合并方法
        const row1 = this.item1[rowIndex];
        const col1 = row1 > 0 ? 1 : 0; // 如果被合并了row = 0; 则他这个列需要取消
        return {
          rowspan: row1,
          colspan: col1,
        };

      } else if (columnIndex === 1) {
        // 第二列的合并方法
        const row2 = this.item2[rowIndex];
        const col2 = row2 > 0 ? 1 : 0; // 如果被合并了row = 0; 则他这个列需要取消
        return {
          rowspan: row2,
          colspan: col2,
        };
      }

    },

    beforeOpen(done, type) {
      if (["edit", "view"].includes(type)) {
        getDetail(this.form.id).then(res => {
          this.form = res.data.data;
        });
      }
      done();
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      params.dateStart = this.query.dateStart;
      params.dateEnd = this.query.dateEnd;
      params.rangeDate = this.query.rangeDate;
      done();
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange(page) {
      this.onLoad(page);
    },
    onLoad(page, params = {}) {
      this.loading = true;

      if (this.query.rangeDate.length == 2) {
        this.query.dateStart = this.query.rangeDate[0];
        this.query.dateEnd = this.query.rangeDate[1];
      } else {
        this.$message.error("请先选择时间范围");
        return;
      }

      getList(
        page.currentPage,
        page.pageSize,
        Object.assign(params, this.query)
      ).then(res => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
        this.selectionClear();

      });
    },

    excel2() {
      excel2();
    },

  }

}
</script>

<style>
.avue-crud__title {margin-left: 45%}
</style>

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值