/** * date: * author: neeler */ // test data start // var data_getList = Mock.mock('/area/getList.htm', {"rowCount":0,"pagesize":0,"pages":0,"page":0,"items":null, // "list":[ // {"name":"河南省","bianhao":"01","xsbianhao":"01","mx":false,'jishu':1}, // {"name":"河南省郑州市","bianhao":"0101","xsbianhao":"01","mx":true,'jishu':2}, // {"name":"河南省洛阳市","bianhao":"0102","xsbianhao":"02","mx":false,'jishu':2}, // {"name":"河南省洛阳市偃师市","bianhao":"010201","xsbianhao":"01","mx":true,'jishu':3}, // {"name":"山东省","bianhao":"02","xsbianhao":"02","mx":false,'jishu':1} // ],"hint":null,"errmsg":null,"code":0}) // var data_gecmtList = Mock.mock('/area/getallCMlist.htm', {"rowCount":0,"pagesize":0,"pages":0,"page":0,"items":null, // "list":[ // {"name":"郑州鑫玉升运输有限公司","companyNo":"001"}, // {"name":"河南天海物流有限公司","companyNo":"002"}, // {"name":"洛阳市汇宇汽车运输有限公司","companyNo":"003"}, // {"name":"河南龙威货运有限公司","companyNo":"004"}, // {"name":"王学寨","companyNo":"005"} // ],"hint":null,"errmsg":null,"code":0}) // var date_getsecmlist = Mock.mock('/area/getcmList.htm', {code: 0, item: '001,003' }) // var data_delItem = Mock.mock('/area/delItem.htm', {code: 0, errmsg: '删除失败!'}) // var data_saveItem = Mock.mock('/area/saveItem.htm', {code: 0, errmsg: '保存成功!'}) window.I = { PZ: 0, addSibling: $('#addSibling'), addChild: $('#addChild'), search: $('#search'), save: $('#save'), DB_ep: {}, GLXJ:$('#GLXJ') } function EnterpriseObj() { return { name: '', bianhao: '',//编号 jishu:'',//级数 xsbianhao: '',//显示的编号 pbianhao:'',//父节点 isadd:false//是否是新增 } } var ViewModel = function() { var self = this; //待编辑对象 self.o = ko.observable(ko.mapping.fromJS(EnterpriseObj())); //是否可以删除 self.canDelete = ko.observable(false); //是否可以添加子节点 self.canAddChild = ko.observable(false); //是否修改下级 self.canGLXJ=ko.observable(false); //删除操作 self.del = function() { delConfirm(); } //已选择的物流公司 self.selectedList=ko.observableArray(); //全部的物流公司 self.allCMlist=ko.observableArray(); } $(function () { window.vm = new ViewModel(); ko.applyBindings(vm); getList(); pageInit(); getallCMlist(); }); function pageInit() { I.save.click(function(event) { saveItem(); }); I.addSibling.click(function(event) { var o = EnterpriseObj(); o.jishu = I.DB_ep[I.selectedId] && I.DB_ep[I.selectedId].jishu; o.pbianhao=I.DB_ep[I.selectedId] && I.DB_ep[I.selectedId].bianhao.substring(0,I.DB_ep[I.selectedId].bianhao.length-2); vm.o(ko.mapping.fromJS(o)); vm.selectedList([]); $('#dp').val('').trigger("chosen:updated"); I.GLXJ.attr("checked",false); vm.canGLXJ(false); }); I.addChild.click(function(event) { var o = EnterpriseObj(); o.jishu = I.DB_ep[I.selectedId] && I.DB_ep[I.selectedId].jishu; o.jishu+=1; o.pbianhao=I.DB_ep[I.selectedId] && I.DB_ep[I.selectedId].bianhao; vm.o(ko.mapping.fromJS(o)); vm.selectedList([]); $('#dp').val('').trigger("chosen:updated"); I.GLXJ.attr("checked",false); vm.canGLXJ(false); }); I.search.keyup(function(event) { var to = false; if(to) { clearTimeout(to); } to = setTimeout(function () { var v = I.search.val(); $('#jstree_Enterprise').jstree(true).search(v); }, 250); }); I.GLXJ.change(function(event){ if(I.GLXJ.prop("checked")){ I.GLXJ.attr("checked",true); }else{ I.GLXJ.attr("checked",false); } }); } function getList(keyword, page, pagesize, pages) { $.post('/area/getList.htm', { search: keyword, page: page, pagesize: pagesize, pages: pages }, function(data, textStatus, xhr) { if (data.code >= 1) { swal({title:'',text:data.errmsg,type:'error',confirmButtonText:'确定'}); return; } if (data.code == 0) { if (!!$('#jstree_Enterprise').jstree(true)) { $('#jstree_Enterprise').jstree(true).refresh(); } if (isList(data.list)) { $.jstree.destroy(); vm.canAddChild(true); var Arr = []; var level = 0; $.each(data.list, function(index, val) { I.DB_ep[val.bianhao] = val; //顶级 if (val.jishu==1) { Arr.push({ text: val.xsbianhao + ' ' + val.name, id: val.bianhao, children: [], state: index === 0 ? {selected: true} : '' }); } else { var j = 2; var children = Arr[Arr.length - 1].children; while (j < val.jishu) { children = children[children.length - 1].children; j++; } children.push({ text: val.xsbianhao + ' ' + val.name, id: val.bianhao, children: [] }) } }); $('#jstree_Enterprise') .jstree({ core: { check_callback : true, data: Arr, themes: { // stripes: true } }, types: { "default" : { "icon" : false // 删除默认图标 }, }, plugins : [ 'search', 'types', 'themes' ] }) .on("changed.jstree", function (e, data) { if(data.selected.length) { var selected = data.instance.get_node(data.selected[0]); var id = selected.id; I.selectedId = id; clearValid(); vm.o(ko.mapping.fromJS(I.DB_ep[id])); vm.canDelete(!selected.children.length); vm.canGLXJ(selected.children.length); getselectedCMlist(id); I.GLXJ.attr("checked",false); } }) .on("ready.jstree", function(e, data) { data.instance.open_all(-1); $('.page-content').height($('#jstree_Enterprise').height()+50); }) .on("after_open.jstree",function(e, data){ $('.page-content').height($('#jstree_Enterprise').height()+50); }) .on("after_close.jstree",function(e, data){ $('.page-content').height($('#jstree_Enterprise').height()+50); }) } return; } }, 'json'); } //获取全部的物流公司 function getallCMlist() { $.post("/area/getallCMlist.htm", function (data, textStatus, jqXHR) { vm.allCMlist.removeAll(); if (data.code >= 1) { swal({title:'',text:data.errmsg,type:'error',confirmButtonText:'确定'}); return; } if (data.code == 0) { if (isList(data.list)) { $.each(data.list, function(index, val) { vm.allCMlist.push(val); }); $('.chosen-select').chosen({search_contains: true}); } return; } }, "json" ); } //获取已选择的物流公司 function getselectedCMlist(bianhao) { $.post('/area/getcmList.htm', {bianhao:bianhao}, function(data, textStatus, xhr) { vm.selectedList.removeAll(); if (data.code >= 1) { swal({title:'',text:data.errmsg,type:'error',confirmButtonText:'确定'}); return; } if (data.code == 0) { if (data.item) { vm.selectedList(data.item.split(',')); } else { vm.selectedList([]); } $('#dp').val(vm.selectedList()).trigger("chosen:updated"); return; } }, 'json'); } function delItem() { var id = I.selectedId; if (!id && !!I.AjaxDelItem) return; I.AjaxDelItem = true; $.post('/area/delItem.htm', {bianhao: id}, function(data, textStatus, xhr) { I.AjaxDelItem = false; if (data.code >= 1) { swal({title:'',text:data.errmsg,type:'error',confirmButtonText:'确定'}); return; } if (data.code == 0) { var ref = $('#jstree_Enterprise').jstree(true), sel = ref.get_selected(); if(!sel.length) { return false; } ref.select_node(ref.get_prev_dom(sel)); ref.delete_node(sel); } }, 'json'); } function saveItem() { if (!I.AjaxSaveItem && valid()) { I.AjaxSaveItem = true; var o = ko.mapping.toJS(vm.o()); if(o.bianhao==null||o.bianhao==''){ if(o.pbianhao==''&&o.jishu!=1){ swal({title:'',text:'数据错误,请刷新页面重新操作!',type:'error',confirmButtonText:'确定'}); return } if(o.pbianhao==''){ o.bianhao=o.xsbianhao; }else{ o.bianhao=o.pbianhao+o.xsbianhao; } } o.companyNos=''; if(!!vm.selectedList()){ for(var i=0;i= 1) { swal({title:'',text:data.errmsg,type:'error',confirmButtonText:'确定'}); return; } if (data.code == 0) { parent.showErrmsg('保存成功!'); getList(); return; } }, 'json'); } } function valid(){ var isValid = false; var iF = $('#form'); iF.validate({ rules: { 'xsbianhao': { required: true, rangelength:[2,2], }, 'name': { required: true } }, messages: { 'xsbianhao': { required: '编号不能为空!', rangelength:'编号必须是两位数字', }, 'name': { required: '名称不能为空!' } }, errorPlacement: function (err, element) { var p = element.parents('.col-sm-9').eq(0); var d = p.children('.text-error').eq(0); d.css('color', '#D9534F'); err.appendTo(d); } }) isValid = iF.valid(); return isValid; } function clearValid() { $('input[type="text"]').removeClass('error'); $('span.text-error').text(''); } function delConfirm() { $("#modalContent").html('确定要删除【' + I.DB_ep[I.selectedId].name + '】吗?'); $("#dialog-confirm").removeClass('hide').dialog({ resizable: false, width: '320', modal: true, title: "

确认信息

", title_html: true, position: { my: "center", at: "center", of: window }, buttons: [ { html: "  确定", "class" : "btn btn-danger btn-minier", click: function() { $( this ).dialog( "close" ); delItem(); } } , { html: "  取消", "class" : "btn btn-minier", click: function() { $( this ).dialog( "close" ); } } ] }); }