attr.js 6.41 KB
/*
 *@time: 2016/2/1
 *@author: chenglong
 */


var $ = require('jquery');
var edit = require('../common/edit');
var dropDown = require('../common/dropDown');
var grid=require('../common/grid');
var baseNum = 1;


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



var tableGird = new grid({

    el:"#attr-table",
    parms:function(){

        console.log(window.categoryId);

        return {
            categoryId: window.categoryId * baseNum
        };
    },
    columns:[
        {display: "属性名称", name: "attributeName"}, 
        {display: "属性类型",name: "attributeValues"},
        {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){

                return ('<button attribute-id="' + items.attributeId + '" class="btn btn-success btn-xs edit-class-btn">编辑</button>');
            }
        }
    ]
});


var $attributeName = $('#attributeName');
var $saleType = $('#saleType');
var $inputType = $('#inputType');
var $isMust = $('#isMust');
var $isSearch = $('#isSearch');
var $maxValueLen = $('#maxValueLen');
var $isAllowAlias = $('#isAllowAlias');
var $orderBy = $('#orderBy');
var $attributeValues = $('#attributeValues');
var $displayPosition = $('#displayPosition');
var $remark = $('#remark');
var $attributeId = $('#attributeId');

function removeAttr() {
    $attributeName.val('');
    $saleType.val('');
    $inputType.val('');
    $isMust.val('');
    $isSearch.val('');
    $maxValueLen.val('');
    $isAllowAlias.val('');
    $orderBy.val('');
    $attributeValues.val('');
    $displayPosition.val('');
    $remark.val('');
    $attributeId.val('');
}


    //产品属性列表三级导航

    var $navItem = $('.list-group-item');
    var $navIcon = $navItem.find('span.icon');

    $navItem.click(function (event) {

        var $this = $(this),
            $thisNext = $this.nextAll(),
            thisVal = $(this).attr('data-val'),
            $target = $(event.target);

        if (!$this.attr('data-active')) {

            if ($target.hasClass('list-group-item')) {

                $navItem.removeClass('attr-item-active');

                $(this).addClass('attr-item-active').attr('data-active', 'data-active');

                $('.model-attr-btn').removeAttr('disabled');

                $('input#categoryId').val($this.attr('data-val'));

                //列表渲染
                //queryAttrList($this.attr('data-val'));
                window.categoryId = $this.attr('data-val');
                tableGird.init('/product/attr/queryProductAttributeList');

            }

        } else {

            if ($target.hasClass('list-group-item')) {

                $(this).removeClass('attr-item-active').removeAttr('data-active');

                $('.model-attr-btn').attr('disabled', 'disabled');
            }

        }

        if ($this.attr('data-status') === 'close') {

            if ($target.hasClass('icon')) {

                $thisNext.filter(function (index) {

                    return $(this).attr('data-parent') === thisVal;

                }).removeClass('hidden');

                $this.attr('data-status', 'open').find('span')
                    .removeClass('glyphicon-plus').addClass('glyphicon-minus');
            }

        } else {

            if ($target.hasClass('icon')) {

                $thisNext.filter(function (index) {

                    var thisR = $(this).attr('data-parent') === thisVal;

                    if (thisR && $(this).attr('data-status') === 'open') {
                        $(this).find('span.icon').trigger('click');
                    }

                    return thisR;

                }).addClass('hidden');

                $this.attr('data-status', 'close').find('span')
                    .removeClass('glyphicon-minus').addClass('glyphicon-plus');
            }
        }

    });


    //添加属性
    //表单验证
    var addAttrVerification = new edit("#add-attr-form");

    $('.add-attr-btn').click(function(){

        if (!!$('#attributeName').attr('update')) {
            $('#add-attr-form').attr('action','/product/attr/update');
        }

        addAttrVerification.submit($("#add-attr-form").attr("action"), function(option,that) {
            option.success=function(res){

                console.log(res);

                $('#addAttr').modal('toggle');

                removeAttr();
                tableGird.reload();
            };
            option.error=function(res){
                console.log(res);
            }
        });
        return false;
    });


    // 编辑产品属性
    $(document).on('click', '.edit-class-btn', function () {

        var $this = $(this);
        var attrId = $this.attr('attribute-id');

        $.ajax({
            type: 'POST',
            dataType: 'json',
            url: '/product/attr/getProductAttribute',
            data: {
                attributeId: attrId*baseNum
            }
        }).then(function (d) {
            var data = d.data.data;
            $attributeName.val(data.attributeName).attr('update', 'true');
            $saleType.val(data.saleType);
            $inputType.val(data.inputType);
            $isMust.val(data.isMust);
            $isSearch.val(data.isSearch);
            $maxValueLen.val(data.maxValueLen);
            $isAllowAlias.val(data.isAllowAlias);
            $orderBy.val(data.orderBy);
            $attributeValues.val(data.attributeValues);
            $displayPosition.val(data.displayPosition);
            $remark.val(data.remark);
            $attributeId.val(attrId);

            // 显示dialog
            $('#addAttr').modal('toggle');

            tableGird.reload();
            console.log(d);
        });
    });


    $('.add-attr-btn-close').click(function () {

        removeAttr();
    });