/** * date: * author: neeler */ //test data start // var data_getList = Mock.mock('/basicconfig/company/getList.htm', {code: 0, 'list|3-20': [ // {id: 1, companyNo: 'P812930489012348', name: '郑州博乐信息技术有限公司', phone: '13001300130', ptype: 'tttype', note: '', areaid: '', address: '郑州市AKLFJKLASJFKALSFJK', used: true, legalperson: '刘总', creditno: '878786465', pinyin: '', jianpin: '', helpcode: 'zzbl'} // ], pages: 12}) // var data_doDel = Mock.mock('/basicconfig/company/delItem.htm', {code: 0, errmsg: '删除失败!'}) // var data_doSave = Mock.mock('/basicconfig/company/saveItem.htm', {code: 0, errmsg: '保存失败!'}) // var data_doUsed = Mock.mock('/basicconfig/company/changeUsed.htm', {code: 0, errmsg: '修改失败!'}) // var data_getNewNo = Mock.mock('/basicconfig/prefix/getNewNo.htm', {code: 0, number: 'V8912374891327S'}) //test data end var TYPE = ['供应商', '客户', '往来单位'] window.I = { PZ: 30, keyword: $('#keyword'), searchBtn: $('#searchBtn'), pageValue: !!$('#p').val() ? $('#p').val() - 1 : 0, keywordValue: $('#k').val(), newObj: $('#newObj'), modal: $('#modal'), save: $('#save') } var Obj = { id: 0, companyNo: '', name: '', phone: '', ptype: TYPE[2], note: '', areaid: '', address: '', used: ko.observable(true), legalperson: '', creditno: '', pinyin: '', jianpin: '', helpcode: '', } var ViewModel = function() { var self = this; self.keyword = ko.observable(I.keywordValue); self.list = ko.observableArray(); self.o = ko.observable(ko.mapping.fromJS(Obj)); self.edit = function(item) { doEdit(item); } self.del = function(item) { delConfirm(item); } self.used = function (item) { doUsed(item); } self.tList = ko.observableArray(TYPE); //磅房状态 } $(function () { window.vm = new ViewModel(); ko.applyBindings(vm); doSearch(); pageInit(); }); function pageInit() { I.save.click(function(event) { if (valid()) { doSave(); } }); I.searchBtn.click(function(event) { doOpen(); }); I.keyword.on(ISIE ? 'keydown' : 'keyup', function (event) { if (event.keyCode == 13) { doOpen(); } }); I.newObj.click(function(event) { vm.o(ko.mapping.fromJS(Obj)); getNewNo(); I.modal.modal('show'); }); I.modal.on('hidden.bs.modal', function (e) { clearError(); }); } function doOpen() { var k = $.trim(vm.keyword()); k = !!k ? '?keyword=' + k : ''; window.open('company.jsf' + k, '_self'); } function doSearch() { getList(vm.keyword(), I.pageValue, I.PZ, 1); } function getList(keyword, page, pagesize, pages) { $.post("/basicconfig/company/getList.htm", { keyword: keyword, page: page, pagesize: pagesize, pages: pages }, function (data, textStatus, jqXHR) { if (data.code >= 1) { // parent.showErrmsg(data.errmsg); swal({title:'',text:data.errmsg,type:'error',confirmButtonText:'确定'}); return; } if (data.code == 0) { if (isList(data.list)) { $.each(data.list, function (indexInArray, valueOfElement) { valueOfElement.used = ko.observable(valueOfElement.used); vm.list.push(valueOfElement); }); } if (!!pages) { pages = data.pages; $('#pagdiv').unbind('page').empty(); if (pages > 1) { $('#pagdiv').bootpag({total: pages, maxVisible: 10, page: page + 1}).on('page',function(event,num){ var k = !!I.keywordValue ? '&keyword=' + I.keywordValue : ''; window.open('company.jsf?page=' + num + k, '_self'); }); } } return; } }, "json" ); } function doEdit(item) { vm.o(ko.mapping.fromJS(item)); I.modal.modal('show'); } function delConfirm(item) { $("#modalContent").html('确定要删除【' + item.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" ); doDel(item); } } , { html: " 取消", "class" : "btn btn-minier", click: function() { $( this ).dialog( "close" ); } } ] }); } function doDel(item) { if (!!I.AjaxDelItem) return; I.AjaxDelItem = true; $.post('/basicconfig/company/delItem.htm', {id: item.id}, function(data, textStatus, xhr) { I.AjaxDelItem = false; if (data.code >= 1) { //parent.showErrmsg(data.errmsg); swal({title:'',text:data.errmsg,type:'error',confirmButtonText:'确定'}); return; } if (data.code == 0) { vm.list.remove(item); location.replace(location.href); return; } }, 'json'); } function doSave() { if (!I.AjaxSaveItem &&valid()) { I.AjaxSaveItem = true; var o = ko.mapping.toJS(vm.o()); o.used = $('#objUsed').prop('checked'); $.post('/basicconfig/company/saveItem.htm', o, function(data, textStatus, xhr) { I.AjaxSaveItem = false; if (data.code >= 1) { // parent.showErrmsg(data.errmsg); swal({title:'',text:data.errmsg,type:'error',confirmButtonText:'确定'}); return; } if (data.code == 0) { location.replace(location.href); return; } }, 'json'); } } function doUsed(item) { if (!I.AjaxChangeUsed) { var used = !item.used(); I.AjaxChangeUsed = true; $.post('/basicconfig/company/changeUsed.htm', {id: item.id, used: used}, function(data, textStatus, xhr) { I.AjaxChangeUsed = false; if (data.code >= 1) { //parent.showErrmsg(data.errmsg); swal({title:'',text:data.errmsg,type:'error',confirmButtonText:'确定'}); return; } if (data.code == 0) { item.used(used); return; } }, 'json'); } } function valid(){ var isValid = false; var iF = $('#form'); iF.validate({ rules: { 'companyNo': { required: true }, 'name': { required: true } }, messages: { 'companyNo': { required: '编号不能为空!' }, 'name': { required: '名称不能为空!' } }, errorPlacement: function (err, element) { var p = element.parents('.form-group').eq(0); var d = p.find('.text-error').eq(0); d.css('color', '#D9534F'); err.appendTo(d); } }) isValid = iF.valid(); return isValid; } function clearError() { $('#form input').removeClass('error'); $('#form .text-error').html(''); } function getNewNo() { $.post("/basicconfig/prefix/getNewNo.htm", {type: 'company'}, function (data, textStatus, jqXHR) { if (data.code >= 1) { // parent.showErrmsg(data.errmsg); swal({title:'',text:data.errmsg,type:'error',confirmButtonText:'确定'}); return; } if (data.code == 0) { vm.o().companyNo(data.number); return; } }, "json" ); }