index.js 5.48 KB
/**
 * 创建供应商
 */
'use strict';
var $ = require('jquery'),
    dropDown=require('../common/dropDown');
var grid=require('../common/grid');
require('../util/jquery.gritter');
var select=require('../common/dropDown');

exports.menu=function(callback){

    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'));
            }

        } 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');
            }
        }
        window.menuid=thisVal;
        //获取文案
        // var c=[];
        // function aa(a){
        //     var b=a.prev();
        //     if(b.length>0&&b.find(".glyphicon-minus")){
                
        //         aa(b);
        //     }
        //     c.push($.trim(a.text()));
        // }
        // aa($(".attr-item-active[data-val="+thisVal+"]"));
        window.menuname=$.trim($(".attr-item-active[data-val="+thisVal+"]").text());//c.join('/');
        callback&&callback();
    });

}

exports.init = function() {
   

    new select({
        el:"#sizeId",
        ajax:"sortsize"
    });

    var g=new grid({
    el:"#basicTable",
    parms:function(){
        return {
            sortId:window.menuid?window.menuid:"",
        };
    },
    columns:[
    //分类ID  分类名称    尺码ID    尺码名 排序  操作
        {display:"分类ID",name:"sortId"},
        {display:"分类名称",name:"sortName"},
        {display:"尺码ID",name:"sizeId"},
        {display:"尺码名",name:"sizeName"},
        {display:"排序",name:"orderBy"},
        {display:"操作",name:'id',render:function(item){
            return '<a data-sortid="'+item.sortId+'" href="javascript:void(0)" class="btn btn-success btn-xs delbtn">删除</a>';
        }}
        ]
    });



    exports.menu(function(){
        g.init('/erpproduct/sortsize/ajax/index');
        $("#sortName").val(window.menuname);
    });

    function __ajax(options,callback){
        $.ajax({
            type: 'POST',
            url: options.url,
            dataType: 'json',
            data:options.data||{},
            success: function(res) {
                res=res.data;
                if (res.code === 200) {
                    $.gritter.add({
                        title: res.message,
                        class_name: 'growl-success',
                        sticky: false,
                        time:1000,
                        after_open:setTimeout(function() {
                            callback&&callback();
                        },1000) 
                     });
                } else {
                    $.gritter.add({
                        title: res.message,
                        class_name: 'growl-danger',
                        sticky: false,
                        time:1000
                     });
                }
            }
        });
    }

    $(document).on("click",".delbtn",function(){
        var _sortid=$(this).data("sortid"),
            _sizeid=$(this).data("sizeid");
        __ajax({
            url:'/erpproduct/sortsize/ajax/delete',
            data:{sortId:_sortid,sizeId:_sizeid}
        },function(){
            g.reload();
            $("#addAttr").modal('hide');
        });
    });

    $(document).on("click",".add-attr-btn",function(){
        var data={sortId:window.menuid,sizeId:$("#sizeId").val(),orderBy:$("#orderBy").val()};
        if(!data.sortId){
            alert("尺码不可为空");
            return;
        }
        if(!~data.sizeId){
            alert("尺码属性不可为空");
            return;
        }
        if(!data.orderBy){
            alert("排序不可为空");
            return;
        }
        __ajax({
            url:'/erpproduct/sortsize/ajax/add',
            data:data
        },function(){
            g.reload();
            $("#addAttr").modal('hide');
        });
    });
}