relateStoreTable.vue 5.19 KB
<!--
 * @Author: 'wangzhihui' '1733904062@qq.com'
 * @Date: 2024-06-12 15:21:09
 * @LastEditors: 'wangzhihui' '1733904062@qq.com'
 * @LastEditTime: 2026-04-08 17:13:53
 * @FilePath: \dj-car-life-system\src\views\goods\grounding\components\relateStoreTable.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
  <div class="store-table">
    <el-table header-cell-class-name="header-cell-class-name"
              style="width: 100%"
              border
              :data="isHide ? tableData : merchantGoodsRefList"
              :header-row-style="headerRowStyle"
              :span-method="isHide ? objectSpanMethod : null">
      <el-table-column align="center"
                       label="序号"
                       width="60px">
        <template #default="scope">
          {{ getIndex(scope.$index) }}
        </template>
      </el-table-column>
      <el-table-column align="center"
                       label="门店名称"
                       prop="merchantName" />
      <el-table-column align="center"
                       label="门店商户号"
                       prop="merchantId" />
      <el-table-column v-if="isHide"
                       align="center"
                       label="产品名称"
                       prop="upgradeGoodsName" />
      <el-table-column align="center"
                       label="库存管控">
        <template #default="scope">
          <span v-if="isDetail">{{ scope.row.inventoryType == 1 ? '虚拟' : '实物' }}</span>
          <el-select v-else
                     v-model="scope.row.inventoryType"
                     placeholder="请选择"
                     @change="handleInventoryChange(scope.row)">

            <el-option label="实物" :value="2"></el-option>
            <el-option label="虚拟" :value="1"></el-option>
          </el-select>
        </template>
      </el-table-column>
      <el-table-column v-if="!isDetail"
                       align="center"
                       label="操作"
                       width="90">
        <template #default="scope">
          <el-button type="text"
                     @click="handleDelete(scope.row)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
export default {
	name: "RelateProTable",
	props: {
		merchantGoodsRefList: {
			type: Array,
			default: () => {},
		},
		isDetail: {
			type: Boolean,
			default: false,
		},
		isHide: {
			type: Boolean,
			default: false,
		},
	},
	data() {
		return {
			tableData: [],
		};
	},
	watch: {
		merchantGoodsRefList: {
			handler(val) {
				if (this.isHide) {
					this.initData();
				}
			},
			immediate: true,
		},

	},
	mounted() {

	},
	methods: {
		initData() {
			const tableData = [];
			(this.merchantGoodsRefList || []).forEach((pkg) => {
				if (pkg.inventoryTypeRefList && pkg.inventoryTypeRefList.length > 0) {
					pkg.inventoryTypeRefList.forEach((goods) => {
						if (goods) {
							tableData.push({
								merchantId: pkg.merchantId,
								merchantName: pkg.merchantName,
								merchantGoodsId: goods.merchantGoodsId,
								upgradeGoodsName: goods.upgradeGoodsName,
								inventoryType: goods.inventoryType,
							});
						}
					});
				} else {
					tableData.push({
						merchantId: pkg.merchantId,
						merchantName: pkg.merchantName,
						merchantGoodsId: "",
						upgradeGoodsName: "",
						inventoryType: 2,
					});
				}
			});
			this.tableData = tableData;
			this.getSpanArr(this.tableData);
		},
		getIndex(index) {
			if (!this.isHide) {
				return index + 1;
			}
			// 当有合并单元格时,计算当前行在 spanArr 中是第几个非0项,从而得出正确的顺序序号
			let count = 0;
			for (let i = 0; i <= index; i++) {
				if (this.spanArr[i] > 0) {
					count++;
				}
			}
			return count;
		},
		getSpanArr(data) {
			this.spanArr = [];
			this.pos = 0;
			for (let i = 0; i < data.length; i++) {
				if (i === 0) {
					this.spanArr.push(1);
					this.pos = 0;
				} else {
					// 判断当前元素与上一个元素是否属于同一个套餐
					if (data[i].merchantId === data[i - 1].merchantId) {
						this.spanArr[this.pos] += 1;
						this.spanArr.push(0);
					} else {
						this.spanArr.push(1);
						this.pos = i;
					}
				}
			}
		},
		objectSpanMethod({ row, column, rowIndex, columnIndex }) {
			// 合并序号(0)和套餐名称(1)列
			if (columnIndex === 0 || columnIndex === 1 || columnIndex === 2 || columnIndex === 5) {
				const _row = this.spanArr[rowIndex];
				const _col = _row > 0 ? 1 : 0;
				return {
					rowspan: _row,
					colspan: _col,
				};
			}
		},
		handleDelete(item) {
			this.$emit("handleDelete", item);
		},
		handleInventoryChange(row) {
			this.$emit("updateInventory", row);
		},
		headerRowStyle() {
			return {
				height: "40px",
				background: "#F2F4F7",
			};
		},
	},
};
</script>

<style lang="scss" scoped>
.store-table {
	margin-top: 15px;
}
::v-deep .header-cell-class-name {
	height: 30px;
	background: #f2f4f7;
}
::v-deep .el-table th {
	padding: 0;
}
::v-deep .el-table__body-wrapper tr td {
	padding: 8px 0 !important;
}
</style>