SKU组合查询算法代码-实例二

6 篇文章 0 订阅
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Debug SKU</title>
<link type="text/css" rel="stylesheet" href="css/shCore.css" />
<link type="text/css" rel="stylesheet" href="css/shCoreDefault.css"/>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/json2.js"></script>
<script type="text/javascript" src="js/shCore.js"></script>
<script type="text/javascript" src="js/shBrushJScript.js"></script>
<script type="text/javascript">SyntaxHighlighter.all();</script>

<style type="text/css">
.bh-sku-selected {color: red;}
</style>
<script type="text/javascript">
var startTime = new Date().getTime();
//销售属性集
var keys = [
        ['10'],
        ['20','21','22','23','24'],
        ['30','31','32','33','34','35','36','37','38'],
        ['40']
        ];
        
//后台读取结果集
var data = {
    "10;24;31;40": {
        price:366,
        count:46
    },
    "10;24;32;40": {
        price:406,
        count:66
    },
    "10;24;33;40": {
        price:416,
        count:77
    },
    "10;24;34;40": {
        price:456,
        count:9
    },
    "10;24;35;40": {
        price:371,
        count:33
    },
    "10;24;36;40": {
        price:411,
        count:79
    },
    "10;24;37;40": {
        price:421,
        count:87
    },
    "10;24;38;40": {
        price:461,
        count:9
    },
    "10;24;30;40": {
        price:356,
        count:59
    },
    "10;23;31;40": {
        price:366,
        count:50
    },
    "10;23;32;40": {
        price:406,
        count:9
    },
    "10;23;33;40": {
        price:416,
        count:90
    },
    "10;23;34;40": {
        price:456,
        count:10
    },
    "10;23;35;40": {
        price:371,
        count:79
    },
    "10;23;36;40": {
        price:411,
        count:90
    },
    "10;23;37;40": {
        price:421,
        count:10
    },
    "10;23;38;40": {
        price:461,
        count:9
    },
    "10;23;30;40": {
        price:356,
        count:46
    },
    "10;22;31;40": {
        price:356,
        count:27
    },
    "10;22;32;40": {
        price:396,
        count:38
    },
    "10;22;33;40": {
        price:406,
        count:42
    },
    "10;22;34;40": {
        price:446,
        count:50
    },
    "10;22;35;40": {
        price:361,
        count:25
    },
    "10;22;36;40": {
        price:401,
        count:40
    },
    "10;22;37;40": {
        price:411,
        count:43
    },
    "10;22;38;40": {
        price:451,
        count:42
    },
    "10;21;31;40": {
        price:366,
        count:79
    },
    "10;21;32;40": {
        price:406,
        count:79
    },
    "10;21;33;40": {
        price:416,
        count:10
    },
    "10;21;34;40": {
        price:456,
        count:10
    },
    "10;21;35;40": {
        price:371,
        count:87
    },
    "10;21;36;40": {
        price:411,
        count:10
    },
    "10;21;37;40": {
        price:421,
        count:10
    },
    "10;21;38;40": {
        price:461,
        count:80
    },
    "10;21;30;40": {
        price:356,
        count:43
    },
    "10;20;31;40": {
        price:356,
        count:46
    },
    "10;20;32;40": {
        price:396,
        count:49
    },
    "10;20;33;40": {
        price:406,
        count:65
    },
    "10;20;34;40": {
        price:446,
        count:10
    },
    "10;20;35;40": {
        price:361,
        count:34
    },
    "10;20;36;40": {
        price:401,
        count:41
    },
    "10;20;37;40": {
        price:411,
        count:36
    },
    "10;20;38;40": {
        price:451,
        count:42
    },
    "10;20;30;40": {
        price:346,
        count: 3
    }
}
//保存最后的组合结果信息
var SKUResult = {};
//获得对象的key
function getObjKeys(obj) {
    if (obj !== Object(obj)) throw new TypeError('Invalid object');
    var keys = [];
    for (var key in obj)
        if (Object.prototype.hasOwnProperty.call(obj, key))
            keys[keys.length] = key;
    return keys;
}

//把组合的key放入结果集SKUResult
function add2SKUResult(combArrItem, sku) {
 var key = combArrItem.join(";");
    if(SKUResult[key]) {//SKU信息key属性·
        SKUResult[key].count += sku.count;
        SKUResult[key].prices.push(sku.price);
    } else {
        SKUResult[key] = {
            count : sku.count,
            prices : [sku.price]
        };
    }
}

//初始化得到结果集
function initSKU() {
    var i, j, skuKeys = getObjKeys(data);
    for(i = 0; i < skuKeys.length; i++) {
        var skuKey = skuKeys[i];//一条SKU信息key
        var sku = data[skuKey]; //一条SKU信息value
        var skuKeyAttrs = skuKey.split(";"); //SKU信息key属性值数组
  skuKeyAttrs.sort(function(value1, value2) {
   return parseInt(value1) - parseInt(value2);
  });

        //对每个SKU信息key属性值进行拆分组合
  var combArr = combInArray(skuKeyAttrs);
  for(j = 0; j < combArr.length; j++) {
   add2SKUResult(combArr[j], sku);
  }

        //结果集接放入SKUResult
        SKUResult[skuKeyAttrs.join(";")] = {
            count:sku.count,
            prices:[sku.price]
        }
    }
 var dataStr = '';
 var index = 0;
 for (var kk in SKUResult) {
  dataStr += (JSON.stringify(kk) + "\n");
  index++;
 }

}

/**
 * 从数组中生成指定长度的组合
 * 方法: 先生成[0,1...]形式的数组, 然后根据0,1从原数组取元素,得到组合数组
 */
function combInArray(aData) {
 if(!aData || !aData.length) {
  return [];
 }

 var len = aData.length;
 var aResult = [];

 for(var n = 1; n < len; n++) {
  var aaFlags = getCombFlags(len, n);
  while(aaFlags.length) {
   var aFlag = aaFlags.shift();
   var aComb = [];
   for(var i = 0; i < len; i++) {
    aFlag[i] && aComb.push(aData[i]);
   }
   aResult.push(aComb);
  }
 }
 return aResult;
}


/**
 * 得到从 m 元素中取 n 元素的所有组合
 * 结果为[0,1...]形式的数组, 1表示选中,0表示不选
 */
function getCombFlags(m, n) {
 if(!n || n < 1) {
  return [];
 }

 var aResult = [];
 var aFlag = [];
 var bNext = true;
 var i, j, iCnt1;

 for (i = 0; i < m; i++) {
  aFlag[i] = i < n ? 1 : 0;
 }

 aResult.push(aFlag.concat());

 while (bNext) {
  iCnt1 = 0;
  for (i = 0; i < m - 1; i++) {
   if (aFlag[i] == 1 && aFlag[i+1] == 0) {
    for(j = 0; j < i; j++) {
     aFlag[j] = j < iCnt1 ? 1 : 0;
    }
    aFlag[i] = 0;
    aFlag[i+1] = 1;
    var aTmp = aFlag.concat();
    aResult.push(aTmp);
    if(aTmp.slice(-n).join("").indexOf('0') == -1) {
     bNext = false;
    }
    break;
   }
   aFlag[i] == 1 && iCnt1++;
  }
 }
 return aResult;
}

 

//初始化用户选择事件
$(function() {

 
 initSKU();
 var endTime = new Date().getTime();
 $('#init_time').text('init sku time: ' + (endTime - startTime) + " ms");
 $('.sku').each(function() {
  var self = $(this);
  var attr_id = self.attr('attr_id');
  if(!SKUResult[attr_id]) {
   self.attr('disabled', 'disabled');
  }
 }).click(function() {
  var self = $(this);

  //选中自己,兄弟节点取消选中
  self.toggleClass('bh-sku-selected').siblings().removeClass('bh-sku-selected');
  
  //已经选择的节点
  var selectedObjs = $('.bh-sku-selected');

  if(selectedObjs.length) {
   //获得组合key价格
   var selectedIds = [];
   selectedObjs.each(function() {
    selectedIds.push($(this).attr('attr_id'));
   });
   selectedIds.sort(function(value1, value2) {
    return parseInt(value1) - parseInt(value2);
   });
   var len = selectedIds.length;
   var prices = SKUResult[selectedIds.join(';')].prices;
   var maxPrice = Math.max.apply(Math, prices);
   var minPrice = Math.min.apply(Math, prices);
   $('#price').text(maxPrice > minPrice ? minPrice + "-" + maxPrice : maxPrice);
   $('#count').text(SKUResult[selectedIds.join(';')].count);
   
   //用已选中的节点验证待测试节点 underTestObjs
   $(".sku").not(selectedObjs).not(self).each(function() {
    var siblingsSelectedObj = $(this).siblings('.bh-sku-selected');
    var testAttrIds = [];//从选中节点中去掉选中的兄弟节点
    if(siblingsSelectedObj.length) {
     var siblingsSelectedObjId = siblingsSelectedObj.attr('attr_id');
     for(var i = 0; i < len; i++) {
      (selectedIds[i] != siblingsSelectedObjId) && testAttrIds.push(selectedIds[i]);
     }
    } else {
     testAttrIds = selectedIds.concat();
    }
    testAttrIds = testAttrIds.concat($(this).attr('attr_id'));
    testAttrIds.sort(function(value1, value2) {
     return parseInt(value1) - parseInt(value2);
    });
    if(!SKUResult[testAttrIds.join(';')]) {
     $(this).attr('disabled', 'disabled').removeClass('bh-sku-selected');
    } else {
     $(this).removeAttr('disabled');
    }
   });
  } else {
   //设置默认价格
   $('#price').text('--');
   //设置属性状态
   $('.sku').each(function() {
    SKUResult[$(this).attr('attr_id')] ? $(this).removeAttr('disabled') : $(this).attr('disabled', 'disabled').removeClass('bh-sku-selected');
   })
  }
  
 });
});
</script>
</head>
<body>
<div>
属性1:
<input type="button" class="sku" attr_id="10" value="10"/>
</div>

<div>
属性2:
<input type="button" class="sku" attr_id="20" value="20"/>
<input type="button" class="sku" attr_id="21" value="21"/>
<input type="button" class="sku" attr_id="22" value="22"/>
<input type="button" class="sku" attr_id="23" value="23"/>
<input type="button" class="sku" attr_id="24" value="24"/>
</div>

<div>
属性3:
<input type="button" class="sku" attr_id="30" value="30"/>
<input type="button" class="sku" attr_id="31" value="31"/>
<input type="button" class="sku" attr_id="32" value="32"/>
<input type="button" class="sku" attr_id="33" value="33"/>
<input type="button" class="sku" attr_id="34" value="34"/>
<input type="button" class="sku" attr_id="35" value="35"/>
<input type="button" class="sku" attr_id="36" value="36"/>
<input type="button" class="sku" attr_id="37" value="37"/>
<input type="button" class="sku" attr_id="38" value="38"/>
</div>

<div>
属性4:
<input type="button" class="sku" attr_id="40" value="40"/>
</div>

<span id="init_time">init sku time: </span> </br>
<span id="price">--</span> </br>
<span id="count">--</span> </br>
<pre class="brush: js;">
//销售属性集
var keys = [
        ['10'],
        ['20','21','22','23','24'],
        ['30','31','32','33','34','35','36','37','38'],
        ['40']
        ];
        
//后台读取结果集
var data = {
    "10;24;31;40": {
        price:366,
        count:46
    },
    "10;24;32;40": {
        price:406,
        count:66
    },
    "10;24;33;40": {
        price:416,
        count:77
    },
    "10;24;34;40": {
        price:456,
        count:9
    },
    "10;24;35;40": {
        price:371,
        count:33
    },
    "10;24;36;40": {
        price:411,
        count:79
    },
    "10;24;37;40": {
        price:421,
        count:87
    },
    "10;24;38;40": {
        price:461,
        count:9
    },
    "10;24;30;40": {
        price:356,
        count:59
    },
    "10;23;31;40": {
        price:366,
        count:50
    },
    "10;23;32;40": {
        price:406,
        count:9
    },
    "10;23;33;40": {
        price:416,
        count:90
    },
    "10;23;34;40": {
        price:456,
        count:10
    },
    "10;23;35;40": {
        price:371,
        count:79
    },
    "10;23;36;40": {
        price:411,
        count:90
    },
    "10;23;37;40": {
        price:421,
        count:10
    },
    "10;23;38;40": {
        price:461,
        count:9
    },
    "10;23;30;40": {
        price:356,
        count:46
    },
    "10;22;31;40": {
        price:356,
        count:27
    },
    "10;22;32;40": {
        price:396,
        count:38
    },
    "10;22;33;40": {
        price:406,
        count:42
    },
    "10;22;34;40": {
        price:446,
        count:50
    },
    "10;22;35;40": {
        price:361,
        count:25
    },
    "10;22;36;40": {
        price:401,
        count:40
    },
    "10;22;37;40": {
        price:411,
        count:43
    },
    "10;22;38;40": {
        price:451,
        count:42
    },
    "10;21;31;40": {
        price:366,
        count:79
    },
    "10;21;32;40": {
        price:406,
        count:79
    },
    "10;21;33;40": {
        price:416,
        count:10
    },
    "10;21;34;40": {
        price:456,
        count:10
    },
    "10;21;35;40": {
        price:371,
        count:87
    },
    "10;21;36;40": {
        price:411,
        count:10
    },
    "10;21;37;40": {
        price:421,
        count:10
    },
    "10;21;38;40": {
        price:461,
        count:80
    },
    "10;21;30;40": {
        price:356,
        count:43
    },
    "10;20;31;40": {
        price:356,
        count:46
    },
    "10;20;32;40": {
        price:396,
        count:49
    },
    "10;20;33;40": {
        price:406,
        count:65
    },
    "10;20;34;40": {
        price:446,
        count:10
    },
    "10;20;35;40": {
        price:361,
        count:34
    },
    "10;20;36;40": {
        price:401,
        count:41
    },
    "10;20;37;40": {
        price:411,
        count:36
    },
    "10;20;38;40": {
        price:451,
        count:42
    },
    "10;20;30;40": {
        price:346,
        count: 3
    }
}
</pre>
</span>
</body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Vue是一种流行的JavaScript框架,用于构建用户界面。在使用Vue生成SKU组合table时,我们可以遵循以下步骤: 1. 定义数据结构:首先,我们需要定义用于存储SKU组合的数据结构。可以使用一个数组或对象来存储组合的属性值,如颜色、尺寸等。 2. 生成组合:通过循环嵌套的方式,生成所有可能的SKU组合。将组合的属性值存储在定义的数据结构中。 3. 渲染表格:使用Vue的数据绑定功能,在HTML模板中渲染SKU组合的表格。可以使用v-for指令来循环遍历组合数据结构,并将组合的属性值显示在表格中的对应单元格中。 4. 添加事件处理:如果需要在表格中的某个单元格中添加按钮或其他交互元素,可以使用Vue的事件绑定功能。可以监听按钮点击事件,并执行相应的操作。 5. 样式设计:通过CSS样式对表格进行美化或自定义设计,使其符合项目或产品的需求。 总之,使用Vue生成SKU组合表格的关键是定义合适的数据结构,将生成的组合存储起来,并使用Vue的数据绑定和事件绑定等功能实现表格的渲染和交互设计。这样,我们就可以方便地生成和展示SKU组合表格。 ### 回答2: Vue生成SKU组合Table是指根据一组已知的属性值,生成所有可能的SKU组合,并以Table的形式展示出来。在Vue中,可以通过使用v-for指令来实现此功能。 首先,需要定义一个包含属性和属性值的数据结构,例如: ```javascript data() { return { properties: [ { name: '颜色', values: ['红色', '蓝色', '黄色'] }, { name: '尺寸', values: ['S', 'M', 'L'] }, // 其他属性... ], skus: [] // 用于存储生成的所有SKU组合 } }, ``` 然后,可以使用嵌套的v-for指令来生成所有可能的SKU组合,并将其存储在skus数组中: ```javascript methods: { generateSKUCombinations() { const properties = this.properties; const combinations = properties.reduce((acc, property) => { if (!acc.length) { return property.values.map(value => ({ [property.name]: value })); } const newCombinations = []; property.values.forEach(value => { acc.forEach(combination => { newCombinations.push({ ...combination, [property.name]: value }); }); }); return newCombinations; }, []); this.skus = combinations; } }, ``` 最后,在Vue的模板中,可以使用表格标签和v-for指令来展示生成的SKU组合: ```html <table> <tr> <th v-for="property in properties" :key="property.name">{{ property.name }}</th> <th>SKU编码</th> </tr> <tr v-for="sku in skus" :key="sku"> <td v-for="(value, name) in sku" :key="name">{{ value }}</td> <td>{{ generateSKUCode(sku) }}</td> </tr> </table> ``` 在上述模板中,properties的v-for指令用于生成表头,skus的v-for指令用于生成表格内容。另外,可以通过generateSKUCode方法来生成每个SKU组合的编码。 以上就是使用Vue生成SKU组合Table的方法。 ### 回答3: Vue生成SKU组合table是一种常用的电商商品规格选择的实现方式。在电商网站中,商品往往有多个规格可选,比如颜色、尺寸、款式等。而SKU(Stock Keeping Unit)是指商品的唯一标识,每个SKU对应一个具体的商品。生成SKU组合table的目的是为了方便用户选择商品规格,同时展示所有可选的规格组合。 在Vue中实现这个功能,可以采用动态生成表格的方式。首先,需要在Vue的data中定义商品规格的数组,每个规格都包含一个唯一标识和多个选项。然后,在页面中使用v-for指令遍历规格数组,生成规格的选择区域。 当用户选择了某个规格的选项后,需要根据已选择的规格生成SKU组合。可以使用computed属性来实时计算已选择的规格组合。根据已选择的规格,使用v-for指令遍历SKU数组,生成SKU组合的表格。 在生成表格的同时,可以将每个SKU的唯一标识作为value绑定到checkbox或者radio的input上。这样,用户选择某个具体的SKU时,可以通过v-model获取到所选SKU的值。 最后,可以通过监听选择规格的change事件,动态更新已选择的规格组合SKU表格。当用户选择了所有的规格后,可以添加一个添加购物车或立即购买的按钮,将所选SKU加入购物车或跳转到下一步的支付页面。 这样,通过Vue生成SKU组合table,既方便用户选择商品规格,同时也提升了用户体验。同时,通过动态生成表格的方式,可以适应不同商品的规格数量和种类,具有较好的灵活性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值