attr.js 11.3 KB
/*
 *@time: 2016/2/1
 *@author: chenglong
 */
var $ = require('jquery');
common = require('../common/common');
var sortmenu = require('../common/sortmenu');

// 把Y、N转换成是、否
function convertNorY(val) {
    if (val === 'N') {
        return '否';
    } else {
        return '是';
    }
}

var tableGird = new common.grid({

    el: "#attr-table",
    parms: function () {
        return {
            categoryId: window.sortid
        };
    },
    columns: [
        {display: "属性名称", name: "attributeName"},
        {
            display: "属性类型",

            //name: "attributeValues"
            render: function (item) {
                var propType = item.attributeValues;
                var propsStr = "";
                console.log(item);
                if (common.util.__isJsonString(propType)) {
                    var props = JSON.parse(propType);
                    [].slice.call(props, 0).forEach(function (prop) {
                        propsStr += prop.name + ",";
                    });
                } else {
                    propsStr = propType;
                }
                return '<p> ' + propsStr + '</p>';
            }

        },
        {display: "输入类型", name: "inputType"},
        {
            display: "是否必选",
            name: "isMust",
            render: function (item) {
                return convertNorY(item.isMust);
            }
        }, {
            display: "是否可搜索",
            name: "isSearch",
            render: function (item) {
                return convertNorY(item.isSearch);
            }
        }, {
            display: "操作",
            name: "categoryId",
            render: function (items) {

                if (items.categoryId == window.sortid)
                    return ('<button  data-index="' + items.__index + '" class="btn btn-success btn-xs edit-class-btn">编辑</button>');
            }
        }
    ]
});

// 弹框里面添加属性值控件
var propValueGrid = new common.grid({

    el: "#prop-value-table",
    columns: [
        {
            display: "ID", name: "id",
            render: function (items) {

                //items.id=items.__index+1;
                return ('<input id="id" disabled="true" class="form-control width110 propValueID" value="' + items.id + '" required="required" data-index="' + items.__index + '" type="text">');
            }
        },
        {
            display: "属性值", name: "name",
            render: function (items) {
                return ('<input id="name" class="form-control width110 propValue" value="' + items.name + '" data-index="' + items.__index + '" required="required" type="text" placeholder="属性值">');
            }

        },
        {
            display: "操作",
            name: "",
            render: function (items) {

                var html = [];
                if (items.__index == propValueGrid.__rows.length - 1) {
                    html.push('<button  data-index="' + items.__index + '" class="btn btn-success btn-xs prop-add-btn">添加</button>');
                }
                if (items.__index == 0) {

                    if (propValueGrid.__rows.length != 1) {
                        html.push('<button  data-index="' + items.__index + '" class="btn btn-success btn-xs prop-del-btn">删除</button>');
                    }
                } else {
                    html.push('<button  data-index="' + items.__index + '" class="btn btn-success btn-xs prop-del-btn">删除</button>');

                }
                return html.join("");
            }
        }
    ]
});


$(document).on("change", ".propValueID", function () {
    var item = propValueGrid.__rows[$(this).data("index")];

    if (item) {
        item.id = $(this).val();
    }

});

$(document).on("change", ".propValue", function () {
    var item = propValueGrid.__rows[$(this).data("index")];

    if (item) {
        item.name = $(this).val();
    }

});

//增加属性值
$(document).on('click', '.prop-add-btn', function () {
    propValueGrid.__rows.push({name: "", id: parseInt(propValueGrid.__rows[propValueGrid.__rows.length - 1].id) + 1});
    propValueGrid.reload();
});

//删除属性值
$(document).on('click', '.prop-del-btn', function () {

    var item = propValueGrid.__rows[$(this).data('index')];
    propValueGrid.__rows.splice($(this).data('index'), 1);
    propValueGrid.reload();
});
var timeid;
sortmenu.init(function () {
    clearTimeout(timeid);
    timeid = setTimeout(function () {
        tableGird.init('/product/attr/queryProductAttributeList');
    }, 2000);
});

$('#add-attri').click(function () {
    attributeOp("添加", '/product/attr/add', {categoryId: window.sortid});
});

// 编辑产品属性
$(document).on('click', '.edit-class-btn', function () {
    var item = tableGird.rows[$(this).data('index')];
    attributeOp("修改", '/product/attr/update', item);
    // $("#select2-shopsId-container").val(item.)
});

$(document).on('change', '#inputType', function () {
    $('#attributeValues').val('');
    showORHidePropValue();
});


function attributeOp(prefix, url, item) {

    var a = new common.edit(".modal-body");

    if (item.attributeValues) {


        if (common.util.__isJsonString(item.attributeValues)) {
            propValueGrid.__rows = JSON.parse(item.attributeValues);
        } else {
            propValueGrid.__rows = [{'name': "", 'id': "1"}];
        }
    } else {
        propValueGrid.__rows = [{'name': "", 'id': "1"}];
    }

    var d=new common.dialog({
        title:prefix + "属性",
        width:'80%',
        content:common.util.__template($("#template").html(), item),
        button:[
            {
                value: "确定",
                callback: function(){
                    var inputType = $('#inputType').val();
                    var propVlaueArr = propValueGrid.__rows.map(function (v, index) {
                        return {
                            id: v.id,
                            name: v.name
                        }
                    });

                    if (inputType == 'textarea' || inputType == 'text' || inputType == "") {
                        // 不显示属性列表
                        a.submit(url, function (option) {
                            option.data.categoryId = item.categoryId;
                            option.data.attributeId = item.attributeId;

                            // 检查所有属性值是否为空

                            console.log(inputType);
                            // 单行文本 或者 多行文本 时不显示表格
                            if (inputType == 'textarea' || inputType == 'text' || inputType == "") {

                            } else {
                                option.data.attributeValues = JSON.stringify(propVlaueArr);
                            }

                            console.log(propVlaueArr);
                            //option.debug = true;

                            propValueGrid.__rows = [];
                            option.success = function (res) {

                                res = res.data;
                                if (res.code == "200") {
                                    a.$tip("提交成功", function () {
                                        tableGird.reload();
                                        d.close();
                                    }, 'growl-success');
                                } else {
                                    a.$tip(res.message);
                                }
                                return false;
                            },
                                option.error = function (res) {
                                    a.$tip(res.message);
                                }
                        });

                    } else {
                        // 显示属性列表
                        if (propVlaueArr) {
                            var count=0;
                            propVlaueArr.forEach(function (pv) {
                                if (!pv.name) {
                                    count++;
                                    
                                }
                            });
                            if(!count){
                                a.submit(url+"?_datetime_="+Date.now(), function (option) {
                                    option.data.categoryId = item.categoryId;
                                    option.data.attributeId = item.attributeId;

                                    // 检查所有属性值是否为空

                                    console.log(inputType);
                                    // 单行文本 或者 多行文本 时不显示表格
                                    if (inputType == 'textarea' || inputType == 'text' || inputType == "") {

                                    } else {
                                        option.data.attributeValues = JSON.stringify(propVlaueArr);
                                    }

                                    console.log(propVlaueArr);
                                    //option.debug = true;

                                    propValueGrid.__rows = [];
                                    option.success = function (res) {

                                        res = res.data;
                                        if (res.code == "200") {
                                                tableGird.reload();
                                                d.close();
                                        } else {
                                            a.$tip(res.message);
                                        }
                                        return false;
                                    },
                                        option.error = function (res) {
                                            a.$tip(res.message);
                                        }
                                });
                            }else {
                                a.$tip('属性值不能为空');
                                return false;
                            }
                             
                        } else {
                            a.$tip('属性值不能为空');
                            return false;
                        }

                    }
                    return false;
                },
                css: "btn-primary"
            }
        ]
    });

    a.init();

    // 属性值列表的初始化


    sortPropValuesArray();
    console.log(propValueGrid.__rows);

    propValueGrid.init(propValueGrid.__rows);
    showORHidePropValue();
}


function showORHidePropValue() {
    var inputType = $('#inputType').val();
    // 单行文本 或者 多行文本 时不显示表格
    if (inputType == 'textarea' || inputType == 'text' || inputType == "") {
        $('#attributeValues').show().attr("required",true);
        $('#prop-value-table').hide();
        $('.propValue').removeAttr("required");

    } else {
        $('#attributeValues').hide().removeAttr("required");
        $('#prop-value-table').show();
        $('.propValue').attr("required");

    }
}

// 给属性值数组按照id排序
function sortPropValuesArray() {

    propValueGrid.__rows.sort(function (a, b) {
        return a.id - b.id
    });
}

function checkPropName() {
    propValueGrid.__rows.forEach(function (item) {
        if (!item.name) return false;
    });

    return true;
}