sizeInfo.js 8.21 KB
var $ = require('jquery'),
    edit = require("./edit"),
    dialog = require("./dialog"),
    util = require("./util"),
    grid = require('./grid');

/**
 * 界面列表显示
 * @param data
 */
var list = function (data) {
    var headList = [];
    var a = data.productType.replace(/<br>/g, "/");
    data.productType = a.substring(0, a.length - 1);
    var j = 0;//创造一个变量以供赋值
    if (data.sizeRelationsList.length > 0) {
        headList.push({display: "尺码", name: "sizeName"});//尺码
        headList.push({
            display: "参考尺码(" + data.genderName + ")", name: "referenceName", render: function (item1) {//参考尺码列
                var refName = item1.referenceName ? item1.referenceName : "";//非空判断
                var a = refName.split(/[|/]/g);
                var refName1 = a[0] || "", refName2 = a[1] || "";
                return '<div class="form-group"><div class="col-sm-5"><input class="form-control refInfo refInfo1" data-index="' + item1.__index + '"' + 'value="' + refName1 + '"/></div><div class="col-sm-1">/</div><div class="col-sm-5"><input class="form-control refInfo refInfo2" data-index="' + item1.__index + '"' + 'value="' + refName2 + '"/></div></div>';
            }
        });
        for (var i = 0; i < data.sizeRelationsList[0].prdSizeAttributeBoList.length; i++) {//尺码列
            var sizeAttributes = data.sizeRelationsList[0].prdSizeAttributeBoList[i];//参考尺码数组里的数据
            var chkbox = '<label style="cursor: pointer;"><input type="checkbox" name="noMeasureIds" data-index="' + i + '" value="' + sizeAttributes.sizeAttributeId + '"/>无需测量</label>';
            headList.push({
                display: sizeAttributes.sizeAttributeName + "<br>" + chkbox,
                name: "sizeValue",
                render: function (item1) {
                    var prdList = item1.prdSizeAttributeBoList;
                    if (j >= prdList.length) j = 0;//防止越界
                    var sizeVal = prdList[j].sizeValue ? prdList[j].sizeValue : "";
                    return '<input class="form-control sizeInfo ' + prdList[j++].sizeAttributeId
                        + '" data-index="' + item1.__index + '"' + 'value="' + sizeVal + '"/>';
                }
            });
        }
    }
    var g1 = new grid({
        el: "#content-list",
        hash: false,
        columns: headList
    });
    g1.init(data.sizeRelationsList);
};
/**
 *编辑尺码弹窗
 * @param url
 * @param item
 * @param noHead 是否包含表头信息
 * @param gridList 主界面表格
 */
var toast = function (url, item, noHead, gridList) {
    var e = new edit("#nidaye");
    var content = "";
    if (noHead) {
        content = '<div id="nidaye"><input type="hidden" id="noMeasureIds" for="checkbox">'
            + '<div id="faGrid" style="height: 600px;overflow:auto;"></div></div>';
    } else {
        content = '<div id="nidaye"><input type="hidden" id="noMeasureIds" for="checkbox">'
            + util.__template2($("#modifySize-template").html(), item)
            + '<div id="faGrid" style="height: 600px;overflow:auto;"></div></div>';
    }
    var d = new dialog({
        title: "编辑尺码信息",
        content: content,
        width: "90%",
        button: [{
            value: "提交",
            css: 'btn-info',
            callback: function () {
                var noMeasureIds = JSON.stringify($("#noMeasureIds").val() ? $("#noMeasureIds").val().split("|") : []);//无需测量的列
                //尺码信息列表
                var sizeInfoList = [];
                for (var i = 0; i < $(".sizeInfo").length; i++) {
                    var info = $($(".sizeInfo")[i]);//当前尺码input对象
                    var prdList = g.rows[info.data("index")].prdSizeAttributeBoList;//当前列的对象的尺码列表
                    var attrIndex = parseInt(i % prdList.length);//在当前列的索引
                    sizeInfoList[i] = ({
                        productSkn: item.productSkn,
                        sizeId: g.rows[info.data("index")].sizeId,
                        sizeAttributeId: prdList[attrIndex].sizeAttributeId,
                        sizeValue: info.val()
                    });
                }
                //商品参考尺码
                var productSizeReferList = [];
                for (var i = 0; i < $(".refInfo").length / 2; i++) {
                    var ref1 = $($(".refInfo1")[i]);
                    var ref2 = $($(".refInfo2")[i]);
                    productSizeReferList.push({
                        sizeId: g.rows[ref1.data("index")].sizeId,
                        gender: item.gender,
                        referenceName: ref1.val() + '/' + ref2.val()
                    });
                }
                var data = {
                    productSkn: item.productSkn,
                    noMeasureIds: noMeasureIds,
                    sizeInfoList: JSON.stringify(sizeInfoList),
                    productSizeReferList: JSON.stringify(productSizeReferList)
                };
                util.__ajax({
                    url: url,
                    data: data
                }, function () {
                    gridList.reload();
                    d.close();
                });
            }
        },
            {
                value: "取消",
                css: 'btn-primary'
            }]
    });
    var headList = [];
    var j = 0;//创造一个变量以供赋值
    if (item.sizeRelationsList.length > 0) {
        headList.push({display: "尺码", name: "sizeName"});//尺码
        headList.push({
            display: "参考尺码(" + item.genderName + ")", name: "referenceName", render: function (item1) {//参考尺码列
                var refName = item1.referenceName ? item1.referenceName : "";//非空判断
                var a = refName.split(/[,|/]/g);
                var refName1 = a[0] || "", refName2 = a[1] || "";
                return '<div class="form-group"><div class="col-sm-5"><input class="form-control refInfo refInfo1" data-index="' + item1.__index + '"' + 'value="' + refName1 + '"/></div><div class="col-sm-1">/</div><div class="col-sm-5"><input class="form-control refInfo refInfo2" data-index="' + item1.__index + '"' + 'value="' + refName2 + '"/></div></div>';
            }
        });
        for (var i = 0; i < item.sizeRelationsList[0].prdSizeAttributeBoList.length; i++) {//尺码列
            var sizeAttributes = item.sizeRelationsList[0].prdSizeAttributeBoList[i];//参考尺码数组里的数据
            var chkbox = '<label style="cursor: pointer;"><input type="checkbox" data-index="' + i + '" name="noMeasureIds" value="' + sizeAttributes.sizeAttributeId + '"/>无需测量</label>';
            headList.push({
                display: sizeAttributes.sizeAttributeName + "<br>" + chkbox,
                name: "sizeValue",
                render: function (item1) {
                    var prdList = item1.prdSizeAttributeBoList;
                    if (j >= prdList.length) j = 0;//防止越界
                    var sizeVal = prdList[j].sizeValue ? prdList[j].sizeValue : "";
                    return '<input class="form-control sizeInfo ' + prdList[j++].sizeAttributeId
                        + '" data-index="' + item1.__index + '" value="' + sizeVal + '"/>';
                }
            });
        }
    }
    var g = new grid({
        el: "#faGrid",
        hash: false,
        columns: headList
    });
    g.init(item.sizeRelationsList);
    $("#noMeasureIds").val(item.noMeasureIds ? item.noMeasureIds.join("|") : "");
    e.init();
};

/**
 * 当点击“无需测量”,该列输入框不可编辑
 */
var check = function () {
    $(document).on("change", "input[name='noMeasureIds']", function () {
        var _index = $(this).data("index");
        var tbody = $(this).parents('table').children('tbody');
        if ($(this).is(':checked')) {
            tbody.children('tr').each(function () {
                $(this).find('td').eq(_index + 2).children().attr("readonly", true);
            });
        }
        else {
            tbody.children('tr').each(function () {
                $(this).find('td').eq(_index + 2).children().attr("readonly", false);
            });
        }
    });
};

module.exports = {
    list: list,
    toast: toast,
    check: check
};