当前位置:首页 > VUE

vue element 实现商品sku

2026-01-20 15:42:15VUE

商品SKU实现方法

在Vue和Element UI中实现商品SKU功能,可以通过以下步骤完成:

数据结构设计

商品SKU通常需要处理多个属性组合,每个属性有不同的选项。数据结构可以设计为:

vue element 实现商品sku

data() {
  return {
    skuData: {
      attributes: [
        { name: '颜色', items: ['红色', '蓝色', '绿色'] },
        { name: '尺寸', items: ['S', 'M', 'L'] }
      ],
      skus: []
    }
  }
}

属性选择组件

使用Element UI的Checkbox或Radio组件实现属性选择:

vue element 实现商品sku

<el-form-item v-for="attr in skuData.attributes" :key="attr.name" :label="attr.name">
  <el-checkbox-group v-model="selectedAttrs[attr.name]">
    <el-checkbox v-for="item in attr.items" :key="item" :label="item"></el-checkbox>
  </el-checkbox-group>
</el-form-item>

SKU表格生成

根据选择的属性组合生成SKU表格:

generateSkus() {
  const selected = Object.values(this.selectedAttrs).filter(arr => arr.length > 0)
  if (selected.length === 0) return

  const combinations = this.cartesianProduct(...selected)
  this.skuData.skus = combinations.map(comb => ({
    combination: comb.join(','),
    price: 0,
    stock: 0
  }))
}

cartesianProduct(...arrays) {
  return arrays.reduce((a, b) => 
    a.flatMap(d => b.map(e => [...d, e])), [[]])
}

SKU表格渲染

使用Element UI的Table组件展示SKU组合:

<el-table :data="skuData.skus">
  <el-table-column prop="combination" label="规格组合"></el-table-column>
  <el-table-column label="价格">
    <template #default="{row}">
      <el-input-number v-model="row.price" :min="0"></el-input-number>
    </template>
  </el-table-column>
  <el-table-column label="库存">
    <template #default="{row}">
      <el-input-number v-model="row.stock" :min="0"></el-input-number>
    </template>
  </el-table-column>
</el-table>

完整示例代码

<template>
  <div>
    <el-form>
      <el-form-item v-for="attr in skuData.attributes" :key="attr.name" :label="attr.name">
        <el-checkbox-group v-model="selectedAttrs[attr.name]">
          <el-checkbox v-for="item in attr.items" :key="item" :label="item"></el-checkbox>
        </el-checkbox-group>
      </el-form-item>

      <el-button @click="generateSkus">生成SKU</el-button>
    </el-form>

    <el-table :data="skuData.skus" v-if="skuData.skus.length > 0">
      <el-table-column prop="combination" label="规格组合"></el-table-column>
      <el-table-column label="价格">
        <template #default="{row}">
          <el-input-number v-model="row.price" :min="0"></el-input-number>
        </template>
      </el-table-column>
      <el-table-column label="库存">
        <template #default="{row}">
          <el-input-number v-model="row.stock" :min="0"></el-input-number>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
export default {
  data() {
    return {
      skuData: {
        attributes: [
          { name: '颜色', items: ['红色', '蓝色', '绿色'] },
          { name: '尺寸', items: ['S', 'M', 'L'] }
        ],
        skus: []
      },
      selectedAttrs: {}
    }
  },
  methods: {
    generateSkus() {
      const selected = Object.values(this.selectedAttrs).filter(arr => arr.length > 0)
      if (selected.length === 0) return

      const combinations = this.cartesianProduct(...selected)
      this.skuData.skus = combinations.map(comb => ({
        combination: comb.join(','),
        price: 0,
        stock: 0
      }))
    },
    cartesianProduct(...arrays) {
      return arrays.reduce((a, b) => 
        a.flatMap(d => b.map(e => [...d, e])), [[]])
    }
  }
}
</script>

注意事项

  1. 确保在生成SKU前至少选择一个属性值
  2. 属性组合较多时,考虑性能优化
  3. 可以添加SKU图片上传功能,为每个组合设置不同图片
  4. 后端接口需要支持批量保存SKU数据

标签: 商品vue
分享给朋友:

相关文章

实现vue table

实现vue table

Vue 表格实现方法 基础表格实现 使用 Vue 的 v-for 指令可以快速渲染表格数据。在模板中定义表格结构,通过数据绑定动态生成行和列。 <template> <tabl…

vue 实现工具

vue 实现工具

Vue 实现工具的方法 使用 Vue 实现工具功能可以通过多种方式完成,具体取决于工具的类型和需求。以下是几种常见的方法: 组件化实现 创建一个独立的 Vue 组件来封装工具功能。例如,实现一个计…

vue实现openoffice

vue实现openoffice

Vue 中集成 OpenOffice 的实现方法 在 Vue 项目中集成 OpenOffice 通常需要通过后端服务或现有库实现文档的预览和编辑功能。以下是几种常见的实现方式: 使用 OnlyOff…

vue实现门户

vue实现门户

Vue 实现门户网站的关键步骤 搭建基础框架 使用 Vue CLI 或 Vite 初始化项目,安装 Vue Router 管理多页面路由。门户通常需要响应式布局,可引入 UI 库如 Element P…

vue实现梯形

vue实现梯形

在Vue中实现梯形效果,可以通过CSS的transform属性或clip-path属性来实现。以下是几种常见的实现方法: 使用CSS transform实现梯形 通过CSS的transform: s…

vue实现popover

vue实现popover

Vue 实现 Popover 的方法 使用第三方库(如 Element UI、Ant Design Vue) 许多成熟的 UI 库已经内置了 Popover 组件,可以直接调用。 Element U…