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
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值