element表格,表格合并

3 篇文章 0 订阅

 

<!-- eslint-disable vue/multi-word-component-names -->
<template>
  <div class="table">
    <el-table
      :data="newTableData"
      height="500"
      :span-method="objectSpanMethod"
      border
      style="width: 70%; margin: 20px auto;"
      @select="handleSelectChange"
      @select-all="handleSelectAll"
    >
      <el-table-column label="ID" width="180">
        <template slot-scope="scope">
          {{ scope.row.id ? scope.row.id : scope.row.sku.id }}
        </template>
      </el-table-column>
      <el-table-column label="商品名称">
        <template slot-scope="scope">
          {{
            scope.row.goods_name
              ? scope.row.goods_name
              : scope.row.sku.goods_name
          }}
        </template>
      </el-table-column>
      <el-table-column label="规格名称">
        <template slot-scope="scope">
          {{ scope.row.sku ? scope.row.sku.sku_name : "---" }}
        </template>
      </el-table-column>

      <el-table-column label="商品编码">
        <template slot-scope="scope">
          {{ scope.row.sku ? scope.row.sku.sku_code : "---" }}
        </template>
      </el-table-column>
      <el-table-column label="价格">
        <template slot-scope="scope">
          {{ scope.row.sku && scope.row.sku.online_price ? scope.row.sku.online_price : "---" }}
        </template>
      </el-table-column>
      <!-- <el-table-column type="selection" width="55">
        <template slot-scope="scope">
            <el-checkbox></el-checkbox>
        </template>
      </el-table-column> -->
      <el-table-column type="selection" width="55"> </el-table-column>
    </el-table>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: [
        {
          id: "58",
          goods_name: "仔妹仔弹嘴豆干麻辣味70g",
          goods_code: "9463016960",
          goods_number: "104005",
          attribute_state: "2",
          online_price: "0.01",
          purchase_price: "120.00",
          skus: {
            status: 1,
            count: 2,
            data: [
              {
                id: "29",
                sku_id_list: "18",
                sku_code: "9463016960-2",
                sku_goods_number: "10402",
                online_price: "0.01",
                purchase_price: "110.00",
                sku_name: "1*30",
              },
              {
                id: "28",
                sku_id_list: "17",
                sku_code: "9463016960-1",
                sku_goods_number: "10401",
                online_price: "0.01",
                purchase_price: "60.00",
                sku_name: "1*1",
              },
            ],
          },
        },
        {
          id: "57",
          goods_name: "仔妹仔弹嘴豆干糊辣味70g",
          goods_code: "---",
          goods_number: "104006",
          attribute_state: "2",
          online_price: "5.00",
          purchase_price: "120.00",
          skus: {
            status: 1,
            count: 2,
            data: [
              {
                id: "27",
                sku_id_list: "18",
                sku_code: "---",
                sku_goods_number: "10404",
                online_price: "140.00",
                purchase_price: "100.00",
                sku_name: "1*30",
              },
              {
                id: "26",
                sku_id_list: "17",
                sku_code: "---",
                sku_goods_number: "10403",
                online_price: "5.00",
                purchase_price: "19.00",
                sku_name: "1*1",
              },
            ],
          },
        },
        {
          id: "56",
          goods_name: "康师傅冰红茶1L",
          goods_code: "---",
          goods_number: "111003",
          attribute_state: "2",
          online_price: "4.00",
          purchase_price: "2.75",
          skus: {
            status: 1,
            count: 2,
            data: [
              {
                id: "25",
                sku_id_list: "15",
                sku_code: "---",
                sku_goods_number: "1000123-2",
                online_price: "48.00",
                purchase_price: "33.00",
                sku_name: "箱",
              },
              {
                id: "24",
                sku_id_list: "16",
                sku_code: "---",
                sku_goods_number: "1000123-1",
                online_price: "4.00",
                purchase_price: "2.75",
                sku_name: "瓶",
              },
            ],
          },
        },
        {
          id: "55",
          goods_name: "双汇清伊坊清真鸡肉香肠140g(1*18)",
          goods_code: "---",
          goods_number: "111002",
          attribute_state: "2",
          online_price: "10.00",
          purchase_price: null,
          skus: {
            status: 1,
            count: 2,
            data: [
              {
                id: "23",
                sku_id_list: "15",
                sku_code: "---",
                sku_goods_number: "100004-2",
                online_price: "50.00",
                purchase_price: "25.00",
                sku_name: "箱",
              },
              {
                id: "22",
                sku_id_list: "14",
                sku_code: "---",
                sku_goods_number: "100004-1",
                online_price: "5.00",
                purchase_price: "2.50",
                sku_name: "袋",
              },
            ],
          },
        },
        {
          id: "54",
          goods_name: "双汇清伊坊清真鸡肉香肠140g(1*18)",
          goods_code: "---",
          goods_number: "111002",
          attribute_state: "2",
          online_price: "5.00",
          purchase_price: null,
          skus: { status: 1, count: 0, data: [] },
        },
        {
          id: "53",
          goods_name: "青岛啤酒",
          goods_code: "---",
          goods_number: "10231231",
          attribute_state: "2",
          online_price: null,
          purchase_price: "8.00",
          skus: {
            status: 1,
            count: 2,
            data: [
              {
                id: "19",
                sku_id_list: "11",
                sku_code: "123165436",
                sku_goods_number: "10231231-3",
                online_price: null,
                purchase_price: "96.00",
                sku_name: "12瓶(箱)",
              },
              {
                id: "18",
                sku_id_list: "10",
                sku_code: "123165435",
                sku_goods_number: "10231231-1",
                online_price: null,
                purchase_price: "8.00",
                sku_name: "瓶",
              },
            ],
          },
        },
        {
          id: "2",
          goods_name: "康师傅牛肉泡面",
          goods_code: "620012385582",
          goods_number: "1002",
          attribute_state: "2",
          online_price: "5.50",
          purchase_price: "2.50",
          skus: {
            status: 1,
            count: 5,
            data: [
              {
                id: "17",
                sku_id_list: "9",
                sku_code: "620012385582-21",
                sku_goods_number: "1002-12",
                online_price: "38.00",
                purchase_price: "24.00",
                sku_name: "红烧*10(箱)",
              },
              {
                id: "16",
                sku_id_list: "8",
                sku_code: "620012385582-11",
                sku_goods_number: "1002-7",
                online_price: "38.00",
                purchase_price: "24.00",
                sku_name: "麻辣(箱)",
              },
              {
                id: "3",
                sku_id_list: "3",
                sku_code: "620012385582-3",
                sku_goods_number: "100001-3",
                online_price: "5.00",
                purchase_price: "2.50",
                sku_name: "酸辣",
              },
              {
                id: "2",
                sku_id_list: "2",
                sku_code: "620012385582-2",
                sku_goods_number: "100001-2",
                online_price: "5.00",
                purchase_price: "2.50",
                sku_name: "红烧",
              },
              {
                id: "1",
                sku_id_list: "1",
                sku_code: "620012385582-1",
                sku_goods_number: "100001-1",
                online_price: "5.00",
                purchase_price: "2.50",
                sku_name: "麻辣",
              },
            ],
          },
        },
        {
          id: "1",
          goods_name: "商品1",
          goods_code: "4249399385",
          goods_number: "39341",
          attribute_state: "1",
          online_price: null,
          purchase_price: "10.00",
          skus: "",
        },
      ],
      newTableData: [],
      rowspan: [],
    };
  },
  created() {
    let newTableData = [];
    let rowspan = []; //存放数组每行的 合并row 数据
    this.tableData.map((item, index) => {
      if (item.skus.data && item.skus.data.length > 0) {
        item.skus.data.map((i, j) => {
          if (j === 0) {
            rowspan.push(item.skus.data.length);
          } else {
            rowspan.push(0);
          }
          newTableData.push({ ...item, sku: i });
        });
      } else {
        newTableData.push(item);
        rowspan.push(1); //无规格商品
      }
    });
    this.newTableData = newTableData;
    this.rowspan = rowspan;
    console.log(this.newTableData);
  },
  methods: {
    objectSpanMethod({ row, column, rowIndex, columnIndex }) {
      if (columnIndex === 1) {
        const _row = this.rowspan[rowIndex];
        const _col = _row > 0 ? 1 : 0; // 如果这一行隐藏了,这列也隐藏
        return {
          rowspan: _row, //合并的行数
          colspan: _col, //合并的列数,设为0则直接不显示
        };
      }
    },
    handleSelectChange(selection, row) {
      //   this.multipleSelection = selection;
      console.log(selection); // [{...}, {...}]
    //   console.log(row); // [{...}, {...}]
    },
    handleSelectAll(selection) {
      //   console.log(this.multipleSelection);
      console.log(selection); // [{...}, {...}]
    },
  },
  components: {},
};
</script>

<style lang="scss"></style>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值