add.js 2.55 KB
var $ = require('jquery');
var common=require('../common/common');
var RESPONSEDATA={};
var param = location.href.match(/(new|edit)\/(\d+)/);
console.log(param);
if(param){
    common.util.__ajax({
    url:'/product/class/getProductSort',
    async:false,
    data:{param:param[2]}
    },function(res){
        RESPONSEDATA=res.data;
        console.log("RESPONSEDATA",RESPONSEDATA);
    },true);
}

$("#new-class-form").html(common.util.__template2($("#template").html(),RESPONSEDATA));
var NUMN={
    columns: [{
            state: "prov",
            text: "一级分类",
            hide: false,
            addclass: ""
        }, {
            state: "city",
            text: "二级分类",
            hide: false,
            addclass: ""
        }, {
            state: "area",
            text: "三级分类",
            hide: false,
            addclass: ""
        }],
    datas: [{
            sortName: RESPONSEDATA.firstSortName,
            code: "",
            id: RESPONSEDATA.firstSortId
        }, {
            sortName: RESPONSEDATA.secondSortName,
            code: "",
            id: RESPONSEDATA.secondSortId
        }, {
            sortName: RESPONSEDATA.threeSortName,
            code: "",
            id: RESPONSEDATA.threeSortId
        }]
}
var sort=new common.tabTree("#sortTree",NUMN);
sort.init();

var e=new common.edit("#new-class-form");

e.on("bind",function(){
    $("#orderBy").keyup(function(){
         $(this).val($(this).val().replace(/^\D+$/g,''));
    });
});

e.on("validate",function(){
    if(!(/^\d+$/g.test($("#orderBy").val()))){
        return "排序字段请输入0~100的正整数";
    }
});

e.init();

$(document).on("click",".btn-success",function(){
    e.submit($("#new-class-form").attr("action"),function(option){
        var sortArr=sort.getAddress();
        option.data.firstSortId=sortArr[0]?sortArr[0].id:-1;
        option.data.secondSortId=sortArr[1]?sortArr[1].id:-1;
        option.data.threeSortId=sortArr[2]?sortArr[2].id:-1;

        console.log(option.data);
        // option.debug=true;
        option.success=function(res){
            res=res.data;
            if(res.code=="200"){
                e.$tip('提交成功',function(){
                    location.href="/erpproduct/sort/index";
                },'growl-success');
            }else{
                e.$tip(res.message);
            }
            return false;
        }
        option.error=function(res){
            e.$tip("提交失败");
        };
    });
});