attr.js 8.09 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="";
                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');
    },600);
});

$('#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(".confirm");

    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"}];
    }

    common.dialog.confirm(prefix + "属性", common.util.__template($("#template").html(), item), function () {
        return a.submit(url, function (option) {
            option.data.categoryId = item.categoryId;
            option.data.attributeId = item.attributeId;

            // 检查所有属性值是否为空
            //if (!checkPropName()) {
            //    alert()
            //}


            var inputType = $('#inputType').val();

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

            } else {
                var propVlaueArr = propValueGrid.__rows.splice(0).map(function(item,inex){
                    return {id:item.id,
                        name:item.name
                    }
                });
                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();
                    }, 'growl-success');
                } else {
                    a.$tip(res.message);
                }
                return false;
            },
                option.error = function (res) {
                    a.$tip(res.message);
                }
        });

    });


    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();
        $('#prop-value-table').hide();
        $('.propValue').removeAttr("required");

    } else {
        $('#attributeValues').hide();
        $('#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;
}