sku-1.html 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>SKU</title>
  6. <link type="text/css" rel="stylesheet" href="css/shCore.css" />
  7. <link type="text/css" rel="stylesheet" href="css/shCoreDefault.css"/>
  8. <script type="text/javascript" src="js/jquery-1.4.4.min.js"></script>
  9. <script type="text/javascript" src="js/json2.js"></script>
  10. <script type="text/javascript" src="js/shCore.js"></script>
  11. <script type="text/javascript" src="js/shBrushJScript.js"></script>
  12. <script type="text/javascript">SyntaxHighlighter.all();</script>
  13. <style type="text/css">
  14. .bh-sku-selected {color: red;}
  15. </style>
  16. <script type="text/javascript">
  17. var startTime = new Date().getTime();
  18. var keys = [['24', '25'], ['12', '13'], ['31', '32']];
  19. var data = {"24;12;31": {price: 366.00, count: 46}, "25;12;32": {price: 406, count: 66}}
  20. //保存最后的组合结果信息
  21. var SKUResult = {};
  22. //获得对象的key
  23. function getObjKeys(obj) {
  24. if (obj !== Object(obj)) throw new TypeError('Invalid object');
  25. var keys = [];
  26. for (var key in obj)
  27. if (Object.prototype.hasOwnProperty.call(obj, key))
  28. keys[keys.length] = key;
  29. return keys;
  30. }
  31. //把组合的key放入结果集SKUResult
  32. function add2SKUResult(combArrItem, sku) {
  33. var key = combArrItem.join(";");
  34. if(SKUResult[key]) {//SKU信息key属性·
  35. SKUResult[key].count += sku.count;
  36. SKUResult[key].prices.push(sku.price);
  37. } else {
  38. SKUResult[key] = {
  39. count : sku.count,
  40. prices : [sku.price]
  41. };
  42. }
  43. }
  44. //初始化得到结果集
  45. function initSKU() {
  46. var i, j, skuKeys = getObjKeys(data);
  47. for(i = 0; i < skuKeys.length; i++) {
  48. var skuKey = skuKeys[i];//一条SKU信息key
  49. var sku = data[skuKey]; //一条SKU信息value
  50. var skuKeyAttrs = skuKey.split(";"); //SKU信息key属性值数组
  51. skuKeyAttrs.sort(function(value1, value2) {
  52. return parseInt(value1) - parseInt(value2);
  53. });
  54. //对每个SKU信息key属性值进行拆分组合
  55. var combArr = combInArray(skuKeyAttrs);
  56. for(j = 0; j < combArr.length; j++) {
  57. add2SKUResult(combArr[j], sku);
  58. }
  59. //结果集接放入SKUResult
  60. SKUResult[skuKeyAttrs.join(";")] = {
  61. count:sku.count,
  62. prices:[sku.price]
  63. }
  64. }
  65. }
  66. /**
  67. * 从数组中生成指定长度的组合
  68. * 方法: 先生成[0,1...]形式的数组, 然后根据0,1从原数组取元素,得到组合数组
  69. */
  70. function combInArray(aData) {
  71. if(!aData || !aData.length) {
  72. return [];
  73. }
  74. var len = aData.length;
  75. var aResult = [];
  76. for(var n = 1; n < len; n++) {
  77. var aaFlags = getCombFlags(len, n);
  78. while(aaFlags.length) {
  79. var aFlag = aaFlags.shift();
  80. var aComb = [];
  81. for(var i = 0; i < len; i++) {
  82. aFlag[i] && aComb.push(aData[i]);
  83. }
  84. aResult.push(aComb);
  85. }
  86. }
  87. return aResult;
  88. }
  89. /**
  90. * 得到从 m 元素中取 n 元素的所有组合
  91. * 结果为[0,1...]形式的数组, 1表示选中,0表示不选
  92. */
  93. function getCombFlags(m, n) {
  94. if(!n || n < 1) {
  95. return [];
  96. }
  97. var aResult = [];
  98. var aFlag = [];
  99. var bNext = true;
  100. var i, j, iCnt1;
  101. for (i = 0; i < m; i++) {
  102. aFlag[i] = i < n ? 1 : 0;
  103. }
  104. aResult.push(aFlag.concat());
  105. while (bNext) {
  106. iCnt1 = 0;
  107. for (i = 0; i < m - 1; i++) {
  108. if (aFlag[i] == 1 && aFlag[i+1] == 0) {
  109. for(j = 0; j < i; j++) {
  110. aFlag[j] = j < iCnt1 ? 1 : 0;
  111. }
  112. aFlag[i] = 0;
  113. aFlag[i+1] = 1;
  114. var aTmp = aFlag.concat();
  115. aResult.push(aTmp);
  116. if(aTmp.slice(-n).join("").indexOf('0') == -1) {
  117. bNext = false;
  118. }
  119. break;
  120. }
  121. aFlag[i] == 1 && iCnt1++;
  122. }
  123. }
  124. return aResult;
  125. }
  126. //初始化用户选择事件
  127. $(function() {
  128. initSKU();
  129. var endTime = new Date().getTime();
  130. $('#init_time').text('init sku time: ' + (endTime - startTime) + " ms");
  131. $('.sku').each(function() {
  132. var self = $(this);
  133. var attr_id = self.attr('attr_id');
  134. if(!SKUResult[attr_id]) {
  135. self.attr('disabled', 'disabled');
  136. }
  137. }).click(function() {
  138. var self = $(this);
  139. //选中自己,兄弟节点取消选中
  140. self.toggleClass('bh-sku-selected').siblings().removeClass('bh-sku-selected');
  141. //已经选择的节点
  142. var selectedObjs = $('.bh-sku-selected');
  143. if(selectedObjs.length) {
  144. //获得组合key价格
  145. var selectedIds = [];
  146. selectedObjs.each(function() {
  147. selectedIds.push($(this).attr('attr_id'));
  148. });
  149. selectedIds.sort(function(value1, value2) {
  150. return parseInt(value1) - parseInt(value2);
  151. });
  152. var len = selectedIds.length;
  153. var prices = SKUResult[selectedIds.join(';')].prices;
  154. var maxPrice = Math.max.apply(Math, prices);
  155. var minPrice = Math.min.apply(Math, prices);
  156. $('#price').text(maxPrice > minPrice ? minPrice + "-" + maxPrice : maxPrice);
  157. //用已选中的节点验证待测试节点 underTestObjs
  158. $(".sku").not(selectedObjs).not(self).each(function() {
  159. var siblingsSelectedObj = $(this).siblings('.bh-sku-selected');
  160. var testAttrIds = [];//从选中节点中去掉选中的兄弟节点
  161. if(siblingsSelectedObj.length) {
  162. var siblingsSelectedObjId = siblingsSelectedObj.attr('attr_id');
  163. for(var i = 0; i < len; i++) {
  164. (selectedIds[i] != siblingsSelectedObjId) && testAttrIds.push(selectedIds[i]);
  165. }
  166. } else {
  167. testAttrIds = selectedIds.concat();
  168. }
  169. testAttrIds = testAttrIds.concat($(this).attr('attr_id'));
  170. testAttrIds.sort(function(value1, value2) {
  171. return parseInt(value1) - parseInt(value2);
  172. });
  173. if(!SKUResult[testAttrIds.join(';')]) {
  174. $(this).attr('disabled', 'disabled').removeClass('bh-sku-selected');
  175. } else {
  176. $(this).removeAttr('disabled');
  177. }
  178. });
  179. } else {
  180. //设置默认价格
  181. $('#price').text('--');
  182. //设置属性状态
  183. $('.sku').each(function() {
  184. SKUResult[$(this).attr('attr_id')] ? $(this).removeAttr('disabled') : $(this).attr('disabled', 'disabled').removeClass('bh-sku-selected');
  185. })
  186. }
  187. });
  188. });
  189. </script>
  190. </head>
  191. <body>
  192. <div>
  193. 属性2:
  194. <input type="button" class="sku" attr_id="24" value="24"/>
  195. <input type="button" class="sku" attr_id="25" value="25"/>
  196. </div>
  197. <div>
  198. 属性1:
  199. <input type="button" class="sku" attr_id="12" value="12"/>
  200. <input type="button" class="sku" attr_id="13" value="13"/>
  201. </div>
  202. <div>
  203. 属性3:
  204. <input type="button" class="sku" attr_id="31" value="31"/>
  205. <input type="button" class="sku" attr_id="32" value="32"/>
  206. </div>
  207. <span id="init_time">init sku time: </span> </br>
  208. <span id="price">--</span> </br>
  209. <pre class="brush: js;">
  210. var keys = [
  211. ['24', '25'],
  212. ['12', '13'],
  213. ['31', '32']
  214. ];
  215. var data = {
  216. "24;12;31": {price: 366.00, count: 46},
  217. "25;12;32": {price: 406, count: 66}
  218. }
  219. </pre>
  220. </span>
  221. </body>
  222. </html>