...
|
...
|
@@ -3,7 +3,7 @@ |
|
|
// 商品规格数据
|
|
|
var data = {
|
|
|
spec_attr: [],
|
|
|
spec_list: []
|
|
|
goods_spec_list: []
|
|
|
}
|
|
|
|
|
|
// 配置信息
|
...
|
...
|
@@ -28,18 +28,18 @@ |
|
|
// 注册html容器
|
|
|
this.$container = $(setting.container);
|
|
|
this.$specAttr = this.$container.find('.spec-attr');
|
|
|
// 显示添加规则组表单事件
|
|
|
this.showAddSpecGroupEvent();
|
|
|
// 确认新增规则组事件
|
|
|
this.submitAddSpecGroupEvent();
|
|
|
// 取消新增规则组事件
|
|
|
this.cancelAddSpecGroupEvent();
|
|
|
// 注册添加规格元素事件
|
|
|
this.addSpecItemEvent();
|
|
|
// 注册删除规则组事件
|
|
|
this.deleteSpecGroupEvent();
|
|
|
// 注册删除规则元素事件
|
|
|
this.deleteSpecItemEvent();
|
|
|
// 显示添加规格事件
|
|
|
this.showAddSpecEvent();
|
|
|
// 确认新增规格事件
|
|
|
this.submitAddSpecEvent();
|
|
|
// 取消新增规格事件
|
|
|
this.cancelAddSpecEvent();
|
|
|
// 注册添加规格值事件
|
|
|
this.addSpecValueEvent();
|
|
|
// 注册删除规格事件
|
|
|
this.deleteSpecEvent();
|
|
|
// 注册删除规格值事件
|
|
|
this.deleteSpecValueEvent();
|
|
|
// 注册批量设置sku事件
|
|
|
this.batchUpdateSku();
|
|
|
// 注册表格input数据修改事件
|
...
|
...
|
@@ -49,27 +49,27 @@ |
|
|
},
|
|
|
|
|
|
/**
|
|
|
* 显示添加规则组表单
|
|
|
* 显示添加规格
|
|
|
*/
|
|
|
showAddSpecGroupEvent: function () {
|
|
|
showAddSpecEvent: function () {
|
|
|
// 显示添加规则组表单
|
|
|
this.$container.on('click', '.btn-addSpecGroup', function () {
|
|
|
var $specGroupButton = $(this).parent()
|
|
|
, $specGroupAdd = $specGroupButton.next();
|
|
|
$specGroupButton.hide();
|
|
|
$specGroupAdd.show();
|
|
|
this.$container.on('click', '.btn-addSpec', function () {
|
|
|
var $specButton = $(this).parent()
|
|
|
, $specAdd = $specButton.next();
|
|
|
$specButton.hide();
|
|
|
$specAdd.show();
|
|
|
});
|
|
|
},
|
|
|
|
|
|
/**
|
|
|
* 确认新增规则组
|
|
|
* 确认新增规格
|
|
|
*/
|
|
|
submitAddSpecGroupEvent: function () {
|
|
|
submitAddSpecEvent: function () {
|
|
|
var _this = this;
|
|
|
// 确认添加
|
|
|
_this.$container.on('click', '.btn-addSpecName', function () {
|
|
|
var $specGroupAdd = $(this).parent().parent()
|
|
|
, $specGroupButton = $specGroupAdd.prev()
|
|
|
var $specAdd = $(this).parent().parent()
|
|
|
, $specButton = $specAdd.prev()
|
|
|
, $specNameInput = _this.$container.find('.input-specName')
|
|
|
, $specValueInput = _this.$container.find('.input-specValue')
|
|
|
, specValueInputValue = $specValueInput.val()
|
...
|
...
|
@@ -93,54 +93,54 @@ |
|
|
$specNameInput.val('') && $specValueInput.val('');
|
|
|
// 记录规格数据
|
|
|
data.spec_attr.push({
|
|
|
group_id: result.data.spec_id,
|
|
|
group_name: specNameInputValue,
|
|
|
spec_items: [{
|
|
|
item_id: result.data.spec_value_id,
|
|
|
spec_id: result.data.spec_id,
|
|
|
spec_name: specNameInputValue,
|
|
|
spec_value_list: [{
|
|
|
spec_value_id: result.data.spec_value_id,
|
|
|
spec_value_name: specValueInputValue
|
|
|
}]
|
|
|
});
|
|
|
// 渲染规格属性html
|
|
|
_this.renderHtml();
|
|
|
// 隐藏添加规格组表单
|
|
|
$specGroupAdd.hide() && $specGroupButton.show();
|
|
|
$specAdd.hide() && $specButton.show();
|
|
|
});
|
|
|
|
|
|
});
|
|
|
},
|
|
|
|
|
|
/**
|
|
|
* 取消新增规格组
|
|
|
* 取消新增规格名
|
|
|
*/
|
|
|
cancelAddSpecGroupEvent: function () {
|
|
|
cancelAddSpecEvent: function () {
|
|
|
this.$container.on('click', '.btn-cancleAddSpecName', function () {
|
|
|
var $specGroupAdd = $(this).parent().parent()
|
|
|
, $specGroupButton = $specGroupAdd.prev();
|
|
|
var $specAdd = $(this).parent().parent()
|
|
|
, $specButton = $specAdd.prev();
|
|
|
// 隐藏添加规格组表单
|
|
|
$specGroupAdd.hide() && $specGroupButton.show()
|
|
|
$specAdd.hide() && $specButton.show()
|
|
|
});
|
|
|
},
|
|
|
|
|
|
/**
|
|
|
* 添加规则元素事件
|
|
|
* 添加规格值事件
|
|
|
*/
|
|
|
addSpecItemEvent: function () {
|
|
|
addSpecValueEvent: function () {
|
|
|
var _this = this;
|
|
|
_this.$container.on('click', '.btn-addSpecItem', function () {
|
|
|
_this.$container.on('click', '.btn-addSpecValue', function () {
|
|
|
var $this = $(this)
|
|
|
, $iptSpecItem = $this.prev('.ipt-specItem')
|
|
|
, specItemInputValue = $iptSpecItem.val()
|
|
|
, $specItemAddContainer = $this.parent()
|
|
|
, $specGroup = $specItemAddContainer.parent().parent();
|
|
|
if (specItemInputValue === '') {
|
|
|
, $iptSpecValue = $this.prev('.ipt-specValue')
|
|
|
, specValueInputValue = $iptSpecValue.val()
|
|
|
, $specValueAddContainer = $this.parent()
|
|
|
, $spec = $specValueAddContainer.parent().parent();
|
|
|
if (specValueInputValue === '') {
|
|
|
layer.msg('规格值不能为空');
|
|
|
return false;
|
|
|
}
|
|
|
// 添加到数据库
|
|
|
var load = layer.load();
|
|
|
$.post('spec/addSpecValue', {
|
|
|
spec_id: $specGroup.data('group-id'),
|
|
|
spec_value_name: specItemInputValue
|
|
|
spec_id: $spec.data('group-id'),
|
|
|
spec_value_name: specValueInputValue
|
|
|
}, function (result) {
|
|
|
layer.close(load);
|
|
|
if (result.code !== 1) {
|
...
|
...
|
@@ -148,9 +148,9 @@ |
|
|
return false;
|
|
|
}
|
|
|
// 记录规格数据
|
|
|
data.spec_attr[$specGroup.data('index')].spec_items.push({
|
|
|
item_id: result.data.spec_value_id,
|
|
|
spec_value_name: specItemInputValue
|
|
|
data.spec_attr[$spec.data('index')].spec_value_list.push({
|
|
|
spec_value_id: result.data.spec_value_id,
|
|
|
spec_value_name: specValueInputValue
|
|
|
});
|
|
|
// 渲染规格属性html
|
|
|
_this.renderHtml();
|
...
|
...
|
@@ -161,9 +161,9 @@ |
|
|
/**
|
|
|
* 删除规则组事件
|
|
|
*/
|
|
|
deleteSpecGroupEvent: function () {
|
|
|
deleteSpecEvent: function () {
|
|
|
var _this = this;
|
|
|
_this.$container.on('click', '.spec-group-delete', function () {
|
|
|
_this.$container.on('click', '.spec-delete', function () {
|
|
|
// 规则组索引
|
|
|
var index = $(this).parent().parent().attr('data-index');
|
|
|
layer.confirm('确定要删除该规则组吗?确认后不可恢复请谨慎操作', function (layerIndex) {
|
...
|
...
|
@@ -177,18 +177,18 @@ |
|
|
},
|
|
|
|
|
|
/**
|
|
|
* 删除规则组事件
|
|
|
* 删除规格值事件
|
|
|
*/
|
|
|
deleteSpecItemEvent: function () {
|
|
|
deleteSpecValueEvent: function () {
|
|
|
var _this = this;
|
|
|
_this.$container.on('click', '.spec-item-delete', function () {
|
|
|
var $item = $(this).parent()
|
|
|
, $specGroup = $item.parent().parent()
|
|
|
, groupIndex = $specGroup.attr('data-index')
|
|
|
, itemIndex = $item.attr('data-item-index');
|
|
|
_this.$container.on('click', '.spec-value-delete', function () {
|
|
|
var $specValue = $(this).parent()
|
|
|
, $spec = $specValue.parent().parent()
|
|
|
, specIndex = $spec.attr('data-index')
|
|
|
, specValueIndex = $specValue.attr('data-item-index');
|
|
|
layer.confirm('确定要删除该规则吗?确认后不可恢复请谨慎操作', function (layerIndex) {
|
|
|
// 删除指定规则组
|
|
|
data.spec_attr[groupIndex].spec_items.splice(itemIndex, 1);
|
|
|
data.spec_attr[specIndex].spec_value_list.splice(specValueIndex, 1);
|
|
|
// 重新渲染规格属性html
|
|
|
_this.renderHtml();
|
|
|
layer.close(layerIndex);
|
...
|
...
|
@@ -213,8 +213,8 @@ |
|
|
}
|
|
|
});
|
|
|
if (!$.isEmptyObject(formData)) {
|
|
|
data.spec_list.forEach(function (item, index) {
|
|
|
data.spec_list[index].form = $.extend({}, data.spec_list[index].form, formData);
|
|
|
data.goods_spec_list.forEach(function (item, index) {
|
|
|
data.goods_spec_list[index].form = $.extend({}, data.goods_spec_list[index].form, formData);
|
|
|
});
|
|
|
// 渲染商品规格table
|
|
|
_this.renderTabelHtml();
|
...
|
...
|
@@ -229,21 +229,21 @@ |
|
|
// 渲染商品规格元素
|
|
|
var html_spec_attr = '';
|
|
|
$.each(data.spec_attr,function(index,value){
|
|
|
html_spec_attr += '<div class="spec-group-item" data-index="'+index+'" data-group-id="'+value.group_id+'">'+
|
|
|
'<div class="spec-group-name">'+
|
|
|
'<span>'+value.group_name+'</span>'+
|
|
|
'<i class="spec-group-delete glyphicon glyphicon-remove" title="点击删除"></i>'+
|
|
|
html_spec_attr += '<div class="spec" data-index="'+index+'" data-group-id="'+value.spec_id+'">'+
|
|
|
'<div class="spec-name">'+
|
|
|
'<span>'+value.spec_name+'</span>'+
|
|
|
'<i class="spec-delete glyphicon glyphicon-remove" title="点击删除"></i>'+
|
|
|
'</div>'+
|
|
|
'<div class="spec-list am-cf">';
|
|
|
$.each(value.spec_items,function(key,item){
|
|
|
html_spec_attr += '<div class="spec-item am-fl" data-item-index="'+key+'">'+
|
|
|
'<div class="spec-value-list am-cf">';
|
|
|
$.each(value.spec_value_list,function(key,item){
|
|
|
html_spec_attr += '<div class="spec-value am-fl" data-item-index="'+key+'">'+
|
|
|
'<span>'+item.spec_value_name+'</span>'+
|
|
|
'<i class="spec-item-delete glyphicon glyphicon-remove-circle" title="点击删除"></i>'+
|
|
|
'<i class="spec-value-delete glyphicon glyphicon-remove-circle" title="点击删除"></i>'+
|
|
|
'</div>';
|
|
|
})
|
|
|
html_spec_attr += '<div class="spec-item-add am-cf am-fl">'+
|
|
|
'<input type="text" class="ipt-specItem am-fl am-field-valid">'+
|
|
|
'<button type="button" class="btn-addSpecItem am-btn am-fl">添加</button>'+
|
|
|
html_spec_attr += '<div class="spec-value-add am-cf am-fl">'+
|
|
|
'<input type="text" class="ipt-specValue am-fl am-field-valid">'+
|
|
|
'<button type="button" class="btn-addSpecValue am-btn am-fl">添加</button>'+
|
|
|
'</div>'+
|
|
|
'</div>'+
|
|
|
'</div>';
|
...
|
...
|
@@ -271,13 +271,13 @@ |
|
|
var html_spec_table = '<tbody>'+
|
|
|
'<tr>';
|
|
|
$.each(data.spec_attr,function(index,value){
|
|
|
html_spec_table += '<th>'+value.group_name+'</th>';
|
|
|
html_spec_table += '<th>'+value.spec_name+'</th>';
|
|
|
})
|
|
|
html_spec_table += '<th>商品价格</th>'+
|
|
|
'<th>库存</th>'+
|
|
|
'<th>重量(kg)</th>'+
|
|
|
'</tr>';
|
|
|
$.each(data.spec_list,function(index,item){
|
|
|
$.each(data.goods_spec_list,function(index,item){
|
|
|
html_spec_table += '<tr data-index="'+index+'" data-sku-id="'+item.spec_sku_id+'">';
|
|
|
$.each(item.rows,function(itemKey,td){
|
|
|
html_spec_table += '<td class="td-spec-value am-text-middle" rowspan="'+td.rowspan+'">'
|
...
|
...
|
@@ -308,47 +308,47 @@ |
|
|
// 规格组合总数 (table行数)
|
|
|
var totalRow = 1;
|
|
|
for (var i = 0; i < data.spec_attr.length; i++) {
|
|
|
totalRow *= data.spec_attr[i].spec_items.length;
|
|
|
totalRow *= data.spec_attr[i].spec_value_list.length;
|
|
|
}
|
|
|
// 遍历tr 行
|
|
|
var spec_list = [];
|
|
|
var goods_spec_list = [];
|
|
|
for (i = 0; i < totalRow; i++) {
|
|
|
var rowData = [], rowCount = 1, specSkuIdAttr = [];
|
|
|
// 遍历td 列
|
|
|
for (var j = 0; j < data.spec_attr.length; j++) {
|
|
|
var skuValues = data.spec_attr[j].spec_items;
|
|
|
var skuValues = data.spec_attr[j].spec_value_list;
|
|
|
rowCount *= skuValues.length;
|
|
|
var anInterBankNum = (totalRow / rowCount)
|
|
|
, point = ((i / anInterBankNum) % skuValues.length);
|
|
|
if (0 === (i % anInterBankNum)) {
|
|
|
rowData.push({
|
|
|
rowspan: anInterBankNum,
|
|
|
item_id: skuValues[point].item_id,
|
|
|
spec_value_id: skuValues[point].spec_value_id,
|
|
|
spec_value_name: skuValues[point].spec_value_name
|
|
|
});
|
|
|
}
|
|
|
specSkuIdAttr.push(skuValues[parseInt(point.toString())].item_id);
|
|
|
specSkuIdAttr.push(skuValues[parseInt(point.toString())].spec_value_id);
|
|
|
}
|
|
|
spec_list.push({
|
|
|
goods_spec_list.push({
|
|
|
spec_sku_id: specSkuIdAttr.join('_'),
|
|
|
rows: rowData,
|
|
|
form: {}
|
|
|
});
|
|
|
}
|
|
|
// 合并旧sku数据
|
|
|
if (data.spec_list.length > 0 && spec_list.length > 0) {
|
|
|
for (i = 0; i < spec_list.length; i++) {
|
|
|
var overlap = data.spec_list.filter(function (val) {
|
|
|
return val.spec_sku_id === spec_list[i].spec_sku_id;
|
|
|
if (data.goods_spec_list.length > 0 && goods_spec_list.length > 0) {
|
|
|
for (i = 0; i < goods_spec_list.length; i++) {
|
|
|
var overlap = data.goods_spec_list.filter(function (val) {
|
|
|
return val.spec_sku_id === goods_spec_list[i].spec_sku_id;
|
|
|
});
|
|
|
if (overlap.length > 0) spec_list[i].form = overlap[0].form;
|
|
|
if (overlap.length > 0) goods_spec_list[i].form = overlap[0].form;
|
|
|
}
|
|
|
}
|
|
|
data.spec_list = spec_list;
|
|
|
data.goods_spec_list = goods_spec_list;
|
|
|
},
|
|
|
|
|
|
/**
|
|
|
* 输入规格信息自动同步更新spec_list
|
|
|
* 输入规格信息自动同步更新goods_spec_list
|
|
|
*/
|
|
|
updateSpecInputEvent: function () {
|
|
|
var _this = this;
|
...
|
...
|
@@ -356,7 +356,7 @@ |
|
|
var $this = $(this)
|
|
|
, dataType = $this.attr('name')
|
|
|
, specIndex = $this.parent().parent().data('index');
|
|
|
data.spec_list[specIndex].form[dataType] = $this.val();
|
|
|
data.goods_spec_list[specIndex].form[dataType] = $this.val();
|
|
|
});
|
|
|
},
|
|
|
|
...
|
...
|
@@ -372,7 +372,7 @@ |
|
|
* @returns {boolean}
|
|
|
*/
|
|
|
isEmptySkuList: function () {
|
|
|
return !data.spec_list.length;
|
|
|
return !data.goods_spec_list.length;
|
|
|
}
|
|
|
|
|
|
};
|
...
|
...
|
|