Vue2商品规格选择

Vue2+Element-ui

Vu2仿写拼多多商家后台规则选择,为什么用Vue2呢,因为公司用的Vue2...
样式不是很好看,自己调一下就行。
 

 

<template>
  <div ref="inputContainer">

    <div>
      {{ combinationsResult }}
    </div>

    <el-row>
      <el-col :span="10">
        <div class="input-group">
          <label for="columnOneName" style="width: 150px;">商品规格-1:</label>
          <el-input v-model="columnNoeName" id="columnOneName"></el-input>
        </div>
      </el-col>
    </el-row>

    <el-row>
      <el-col :span="10">
        <div class="input-group">
          <label for="columnTwoName" style="width: 150px;">商品规格-2:</label>
          <el-input v-model="columnTwoName" id="columnTwoName"></el-input>
        </div>
      </el-col>
    </el-row>

    <div v-show="columnNoeName!==''" v-for="(item, index) in classifyOne" :key="`one-${index}`"
         style="display: inline-block; width: 300px;">
      <el-input
          v-model="item.value"
          placeholder="请输入内容"
          @blur="() => handleBlur('classifyOne', index)"
          style="display: inline-block; width: 200px;"
      ></el-input>
      <el-button style="display: inline-block" v-if="item.value" @click="() => removeInput('classifyOne', index)"
                 class="right-space">删除
      </el-button>
    </div>

    <div></div>

    <div v-show="columnTwoName!==''" v-for="(item, index) in classifyTwo" :key="`two-${index}`"
         style="display: inline-block; width: 300px;  margin-top: 20px;">
      <el-input
          v-model="item.value"
          placeholder="请输入内容"
          @blur="() => handleBlur('classifyTwo', index)"
          style="display: inline-block; width: 200px;"
      ></el-input>
      <el-button style="display: inline-block" v-if="item.value" @click="() => removeInput('classifyTwo', index)"
                 class="right-space">删除
      </el-button>
    </div>

    <el-table :data="combinationsResult"
              style="width: 80%; margin: 0 auto; margin-bottom: 100px; margin-top: 50px;">
      <el-table-column v-if="showStyleColumn" prop="style" :label="columnTwoName"></el-table-column>
      <el-table-column v-if="showColorColumn" prop="color" :label="columnNoeName"></el-table-column>
      <el-table-column label="库存">
        <template slot-scope="scope">
          <el-input v-model="scope.row.stock" placeholder="请输入库存"></el-input>
        </template>
      </el-table-column>
      <el-table-column label="拼单价(元)">
        <template slot-scope="scope">
          <el-input v-model="scope.row.groupPrice" placeholder="请输入拼单价"></el-input>
        </template>
      </el-table-column>
      <el-table-column label="单买价(元)">
        <template slot-scope="scope">
          <el-input v-model="scope.row.singlePrice" placeholder="请输入单买价"></el-input>
        </template>
      </el-table-column>
      <el-table-column label="预览图">
        <template slot-scope="scope">
          <el-input v-model="scope.row.preview" placeholder="请输入预览图 URL"></el-input>
        </template>
      </el-table-column>
      <el-table-column label="规格编码">
        <template slot-scope="scope">
          <el-input v-model="scope.row.code" placeholder="请输入规格编码"></el-input>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
export default {
  data() {
    return {
      classifyOne: [{value: ''}],
      classifyTwo: [{value: ''}],
      columnNoeName: "",
      columnTwoName: "",
      combinationsResult: [{
        stock: '',
        groupPrice: '',
        singlePrice: '',
        preview: '',
        code: ''
      }],  // 用于存储表格中的数据
    };
  },
  computed: {

    showColorColumn() {
      return this.classifyOne.some(one => one.value);
    },
    showStyleColumn() {
      return this.classifyTwo.some(two => two.value);
    }
  },
  methods: {
    createRow(color, style) {
      return {
        color,
        style,
        stock: '',
        groupPrice: '',
        singlePrice: '',
        preview: '',
        code: ''
      };
    },

    generateCombinations() {
      let result = [];
      let sourceResult = this.combinationsResult; // 用于存储组合结果

      const hasColorValues = this.classifyOne.some(one => one.value);
      const hasStyleValues = this.classifyTwo.some(two => two.value);

      if (!hasColorValues && !hasStyleValues) {
        result = [this.createRow('', '')];
      } else if (hasColorValues && hasStyleValues) {
        this.classifyOne.forEach(one => {
          this.classifyTwo.forEach(two => {
            if (one.value && two.value) {
              result.push(this.createRow(one.value, two.value));
            }
          });
        });
      } else {
        this.classifyOne.forEach(one => {
          if (one.value) {
            result.push(this.createRow(one.value, ''));
          }
        });
        this.classifyTwo.forEach(two => {
          if (two.value) {
            result.push(this.createRow('', two.value));
          }
        });
      }

      for (let i = 0; i < result.length; i++) {
        for (let j = 0; j < sourceResult.length; j++) {
          if (sourceResult === []) {
            break;
          }
          if (result[i].color === sourceResult[j].color &&
              result[i].style === sourceResult[j].style) {
            result[i] = sourceResult[j];
          }
        }


        this.combinationsResult = result;
      }
    },
    handleBlur(arrayName, index) {
      this.ensureEmptyInputAtEnd(arrayName);
    }
    ,
    removeInput(arrayName, index) {
      this[arrayName].splice(index, 1);
      this.ensureEmptyInputAtEnd(arrayName);
    }
    ,
    ensureEmptyInputAtEnd(arrayName) {
      const array = this[arrayName];
      if (array && array[array.length - 1].value) {
        array.push({value: ''});
      }
      this[arrayName] = array && array.filter((item, idx) =>
          item.value || idx === array.length - 1
      );
    }
    ,
    handleClickOutside(e) {
      if (!this.$refs.inputContainer.contains(e.target)) {
        this.ensureEmptyInputAtEnd('classifyOne');
        this.ensureEmptyInputAtEnd('classifyTwo');
      }
    }
  },
  mounted() {
    document.addEventListener('click', this.handleClickOutside);
  },
  beforeDestroy() {
    document.removeEventListener('click', this.handleClickOutside);
  },
  watch: {
    classifyOne: {
      handler: 'generateCombinations',
      deep: true
    },
    classifyTwo: {
      handler: 'generateCombinations',
      deep: true
    }
  }
};
</script>

<style scoped>
.right-space {
  margin-right: 10px;
}

.input-group {
  display: flex;
  align-items: center;
}

.input-group label {
  margin-right: 10px;
}

</style>
  • 6
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据引用内容,可以得知在Vue3中处理商品规格SKU的问题时,首先需要考虑将商品的所有规格渲染上去,并且可以根据数据得知该商品有三种规格:颜色、运行内存和存储。 在判断商品规格SKU的库存时,需要注意每点击一个规格属性,都要去判断其他规格属性是否有库存。这可以通过中完整的规格信息来向父组件传递有效的数据,包括SKU的ID、价格、原价格、库存以及商品的说明等。 因此,在Vue3中处理商品规格SKU的方式可以是通过渲染所有规格属性,并在中完整的规格信息后,通过传递有效数据来判断SKU的库存情况。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [基于Vue3 Sku的设计](https://blog.csdn.net/qq_43817005/article/details/121889677)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [vue电商项目sku 规格 详细步骤](https://blog.csdn.net/m0_46846526/article/details/119142417)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值