/** * date: * author: neeler */ //test data start // var data_getList = Mock.mock('/enterprise/systemdepart/getList.htm', {code: 0, list: [{id: 3, bianhao: 'depart00011', name: '生产部', used: false, note: 'nnnn222'}], pages: 12}) // var data_doDel = Mock.mock('/enterprise/systemdepart/delItem.htm', {code: 0, errmsg: '此岗位正在使用中不能删除!'}) // var data_doSave = Mock.mock('/enterprise/systemdepart/saveItem.htm', {code: 0, errmsg: '此岗位正在使用中不能删除!'}) // var data_doUsed = Mock.mock('/enterprise/systemdepart/changeUsed.htm', {code: 0, errmsg: '修改失败!'}) //test data end 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, bianhao: '', name: '', used: true, note: '', } var ViewModel = function() { var self = this; self.o = ko.observable(ko.mapping.fromJS(Obj)); self.keyword = ko.observable(I.keywordValue); self.list = ko.observableArray(); self.dList = ko.observableArray(); self.edit = function(item) { doEdit(item); } self.del = function(item) { delConfirm(item); } self.used = function (item) { doUsed(item); } } $(function () { window.vm = new ViewModel(); ko.applyBindings(vm); doSearch(); 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) { doOpen(); }); I.modal.on('hidden.bs.modal', function (e) { clearError(); }); I.keyword.on(ISIE ? 'keydown' : 'keyup', function () { if (event.keyCode == 13) { doOpen(); } }); } function doOpen() { var k = $.trim(vm.keyword()); k = !!k ? '?keyword=' + k : ''; window.open('sysDepart.jsf' + k, '_self'); } function doSearch() { getList(vm.keyword(), I.pageValue, I.PZ, 1); } function getList(keyword, page, pagesize, pages) { $.post('/enterprise/systemdepart/getList.htm', { keyword: keyword, page: page, pagesize: pagesize, pages: pages }, function(data, textStatus, xhr) { vm.list.removeAll(); if (data.code >= 1) { console.error(data.errmsg); return; } if (data.code == 0) { if (isList(data.list)) { $.each(data.list, function(index, val) { val.used = ko.observable(val.used); 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('sysDepart.jsf?page=' + num + k, '_self'); }); } } return; } }, 'json'); } function valid(){ var isValid = false; var iF = $('#form'); iF.validate({ rules: { 'bianhao': { required: true } }, messages: { 'bianhao': { 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('/enterprise/systemdepart/delItem.htm', {id: item.id}, function(data, textStatus, xhr) { I.AjaxDelItem = false; if (data.code >= 1) { // 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('/enterprise/systemdepart/saveItem.htm', o, function(data, textStatus, xhr) { I.AjaxSaveItem = false; if (data.code >= 1) { console.error(data.errmsg); return; } if (data.code == 0) { location.replace(location.href); 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) { vm.dList.removeAll(); if (data.code >= 1) { console.error(data.errmsg); return; } if (data.code == 0) { if (isList(data.list)) { $.each(data.list, function (indexInArray, valueOfElement) { vm.dList.push(valueOfElement); }); } if (!!I.idv) { getItem(I.idv); } return; } }, 'json'); } function doUsed(item) { if (!I.AjaxChangeUsed) { var used = !item.used(); I.AjaxChangeUsed = true; $.post('/enterprise/systemdepart/changeUsed.htm', {id: item.id, used: used}, function(data, textStatus, xhr) { I.AjaxChangeUsed = false; if (data.code >= 1) { console.error(data.errmsg); return; } if (data.code == 0) { item.used(used); return; } }, 'json'); } }