/** * date: * author: neeler */ //test data start // var data_doDel = Mock.mock('/basicconfig/logisticals/del.htm', {code: 0, errmsg: '此岗位正在使用中不能删除!'}) // var data_doSave = Mock.mock('/basicconfig/logisticals/save.htm', {code: 0, errmsg: '此岗位正在使用中不能删除!'}) // var data_getList = Mock.mock('/basicconfig/logisticals/getList.htm', {code: 0, 'list|3-10': [{id: 0, name: 'name', pinyin: 'pinyin', jianpin: 'jianpin', helpcode: 'helpcode', address: 'address', state: true, departNo: '0105'}]}) //test data end window.I = { _dDB: {}, 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, name: '', pinyin: '', jianpin: '', helpcode: '', address: '', state: '', // departNo: '', } var ViewModel = function() { var self = this; self.o = ko.observable(ko.mapping.fromJS(Obj)); self.keyword = ko.observable(I.keywordValue); // self.departNo = ko.observable(); self.list = ko.observableArray(); self.dList = ko.observableArray(); self.edit = function(item) { doEdit(item); } self.del = function(item) { delConfirm(item); } self.state = function(item) { doUsed(item); } } $(function () { window.vm = new ViewModel(); ko.applyBindings(vm); //getDepartList(); pageInit(); }); function pageInit() { I.save.click(function(event) { if (valid()) { doSave(); } }); I.newObj.click(function(event) { vm.o(ko.mapping.fromJS(Obj)); I.modal.modal('show'); }); I.searchBtn.click(function(event) { doSearch(); }); I.modal.on('hidden.bs.modal', function (e) { clearError(); }); I.keyword.on(ISIE ? 'keydown' : 'keyup', function (event) { if (event.keyCode == 13) { doSearch(); } }); getList(null, I.pageValue, I.PZ, 1) ; } function doOpen() { var k = $.trim(vm.keyword()); k = !!k ? '?keyword=' + k : ''; window.open('lauZone.jsf' + k, '_self'); } function doSearch() { getList(vm.keyword(),I.pageValue, I.PZ, 1); } function doUsed(item) { if (!I.AjaxChangeUsed) { var state = !item.state(); I.AjaxChangeUsed = true; $.post('/basicconfig/logisticals/used.htm', {id: item.id, state: state}, 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.state(state); return; } }, 'json'); } } function getList(name, page, pagesize, pages) { $.post('/basicconfig/logisticals/getList.htm', { name: name, page: page, pagesize: pagesize, pages: pages }, function(data, textStatus, xhr) { vm.list.removeAll(); 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(index, val) { val.state = ko.observable(val.state); vm.list.push(val); }); } 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('lauZone.jsf?page=' + num + k, '_self'); getList(name, num - 1, pagesize, 0); }); } } return; } }, 'json'); } function valid(){ var isValid = false; var iF = $('#form'); iF.validate({ rules: { 'name': { required: true } }, messages: { '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 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/logisticals/del.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()); $.post('/basicconfig/logisticals/save.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) { showErrmsg('保存成功!'); setTimeout(function() { location.replace(location.href); }, 1500); return; } }, 'json'); } } function clearError() { $('#form input').removeClass('error'); $('#form .text-error').html(''); } function getDepartList(keyword, page, pagesize, pages) { $.post('/enterprise/depart/getList.htm', { keyword: keyword, page: page, pagesize: pagesize, pages: pages }, function(data, textStatus, xhr) { var allId = []; vm.dList.removeAll(); 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 (index, value) { I._dDB[value.bianhao] = value.name; vm.dList.push(value); }); } doSearch(); return; } }, 'json'); }