0
点赞
收藏
分享

微信扫一扫

IDEA 2024安装指南(含安装包以及使用说明 cannot collect jvm options 问题一)

J简文 2024-11-22 阅读 44

Elmentui实现订单拆分开票功能

需求

思路

示例代码

<template>
  <div>
    <el-table :data="orderDetails" border style="width: 100%">
      <el-table-column prop="productName" label="产品名称" width="150"></el-table-column>
      <el-table-column prop="quantity" label="数量" width="100">
        <template slot-scope="scope">
          <el-input-number
            v-model="scope.row.quantity"
            :min="0"
            @change="handleQuantityChange(scope.row)"
          ></el-input-number>
        </template>
      </el-table-column>
      <el-table-column prop="price" label="单价" width="100"></el-table-column>
      <el-table-column prop="total" label="总价" width="100"></el-table-column>
    </el-table>

    <el-tabs v-model="activeTabIndex" type="card" @tab-remove="handleTabRemove">
      <el-tab-pane v-for="(tab, index) in splitTabs" :key="index" :label="'拆单' + (index + 1)">
        <el-table :data="tab.selectedDetails" border style="width: 100%">
          <el-table-column prop="productName" label="产品名称" width="150"></el-table-column>
          <el-table-column prop="quantity" label="数量" width="100">
            <template slot-scope="scope">
              <el-input-number
                v-model="scope.row.quantity"
                :min="0"
                @change="handleSelectedQuantityChange(tab, scope.row)"
              ></el-input-number>
            </template>
          </el-table-column>
          <el-table-column prop="price" label="单价" width="100"></el-table-column>
          <el-table-column prop="total" label="总价" width="100"></el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button
                type="danger"
                size="mini"
                @click="handleRemoveSelectedRow(tab, scope.$index)"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-row>
          <el-col :span="12">
            <el-select v-model="selectedProduct" placeholder="选择产品">
              <el-option
                v-for="product in orderDetails"
                :key="product.productName"
                :label="product.productName"
                :value="product.productName"
              ></el-option>
            </el-select>
          </el-col>
          <el-col :span="12">
            <el-input-number v-model="selectedQuantity" :min="0" placeholder="输入数量"></el-input-number>
          </el-col>
          <el-button type="primary" @click="handleAddSelectedRow(splitTabs[activeTabIndex])">添加明细</el-button>
        </el-row>
      </el-tab-pane>
    </el-tabs>
    <el-button type="primary" @click="handleAddTab">添加拆单Tab</el-button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      orderDetails: [
        {
          productName: '产品A',
          quantity: 10,
          price: 100,
          total: 1000
        },
        {
          productName: '产品B',
          quantity: 5,
          price: 200,
          total: 1000
        },
        {
          productName: '产品C',
          quantity: 8,
          price: 150,
          total: 1200
        }
      ],
      splitTabs: [
        {
          selectedDetails: []
        }
      ],
      activeTabIndex: 0,
      selectedProduct: '',
      selectedQuantity: 0
    }
  },
  methods: {
    handleAddTab() {
      this.splitTabs.push({
        selectedDetails: []
      });
    },
    handleTabRemove(targetName) {
      const index = this.splitTabs.findIndex(tab => tab.label === targetName);
      if (index!== -1) {
        this.splitTabs.splice(index, 1);
        if (this.activeTabIndex === index && this.splitTabs.length > 0) {
          this.activeTabIndex = Math.min(this.activeTabIndex, this.splitTabs.length - 1);
        }
      }
    },
    handleAddSelectedRow(tab) {
      const product = this.orderDetails.find(item => item.productName === this.selectedProduct);
      if (product) {
        tab.selectedDetails.push({
          ...product,
          quantity: this.selectedQuantity
        });
        this.selectedProduct = '';
        this.selectedQuantity = 0;
      }
    },
    handleRemoveSelectedRow(tab, rowIndex) {
      const removedRow = tab.selectedDetails[rowIndex];
      this.returnQuantityToOriginal(removedRow);
      tab.selectedDetails.splice(rowIndex, 1);
    },
    handleQuantityChange(row) {
      const currentQuantity = row.quantity;
      const index = this.orderDetails.findIndex(item => item.productName === row.productName);
      if (index!== -1) {
        this.orderDetails[index].quantity = currentQuantity;
      }
    },
    handleSelectedQuantityChange(tab, row) {
      const currentQuantity = row.quantity;
      const sourceIndex = this.orderDetails.findIndex(item => item.productName === row.productName);
      if (sourceIndex!== -1) {
        const quantityDiff = this.orderDetails[sourceIndex].quantity - currentQuantity;
        this.orderDetails[sourceIndex].quantity = currentQuantity;
        this.updateQuantityInTabs(tab, row.productName, quantityDiff);
      }
    },
    updateQuantityInTabs(tab, productName, quantityDiff) {
      tab.selectedDetails.forEach(item => {
        if (item.productName === productName) {
          item.quantity += quantityDiff;
        }
      });
    },
    returnQuantityToOriginal(removedRow) {
      const index = this.orderDetails.findIndex(item => item.productName === removedRow.productName);
      if (index!== -1) {
        this.orderDetails[index].quantity += removedRow.quantity;
      }
    }
  }
}
</script>

最终效果

在这里插入图片描述

点击添加拆单tab新增一个tab
添加明细,选择产品,输入数量,在当前tab新增一行

样式和交互暂时未做处理

举报

相关推荐

0 条评论