/** * date: * author: neeler */ //test data start // var data_getList = Mock.mock('/basicinfo/card/getList.htm', {code: 0, list: [{cardno: 'cn123123123', type: '人员短期卡', state: '丢失', usecount: 234, isuse: true, ltime: '2017-02-11'}], pages: 12}) // var data_doDel = Mock.mock('/basicinfo/card/delItem.htm', {code: 0, errmsg: '删除失败!'}) // var data_doSave = Mock.mock('/basicinfo/card/saveItem.htm', {code: 0, errmsg: '保存失败!'}) //test data end var TYPE = ['门岗用卡', '内部车辆卡', '外来车辆卡', '人员短期卡', '人员长期卡'] var STATE = ['发放', '丢失', '收回'] window.I = { PZ: 30, keyword: $('#keyword'), searchBtn: $('.searchBtn'), moreSearchCondition: $('#moreSearchCondition'), pageValue: !!$('#p').val() ? $('#p').val() - 1 : 0, keywordValue: $('#k').val(), newObj: $('#newObj'), modal: $('#modal'), save: $('#save'), searchBox: $('#searchBox'), cardnoinput:$('#cardnoinput') } var Obj = { cardno: '', // type: TYPE[0], state: STATE[2], usecount: '', isuse: '', ltime: '', } 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.tList = ko.observableArray(TYPE); self.sList = ko.observableArray(STATE); self.type = ko.observable(); self.state = ko.observable(); self.estate=ko.observable(false); self.edit = function(item) { self.estate(true); doEdit(item); } self.del = function(item) { delConfirm(item); } } $(function () { window.vm = new ViewModel(); ko.applyBindings(vm); doSearch(); pageInit(); }); function pageInit() { I.moreSearchCondition.click(function(event) { if ($(this).hasClass('active')) { I._complexSearch = false; $(this).removeClass('active'); $('i', $(this)).addClass('fa-chevron-down').removeClass('fa-chevron-up'); I.searchBox.slideUp(); } else { I._complexSearch = true; $(this).addClass('active'); $('i', $(this)).removeClass('fa-chevron-down').addClass('fa-chevron-up'); I.searchBox.slideDown(); } }); I.save.click(function(event) { if (valid()) { doSave(); } }); I.newObj.click(function(event) { vm.estate(false); obj= { cardno: '', // type: TYPE[0], state: STATE[2], usecount: '', isuse: '', ltime: '', } 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.modal.on('shown.bs.modal', function (e) { I.cardnoinput.focus(); }); I.keyword.on(ISIE ? 'keydown' : 'keyup', function (event) { if (event.keyCode == 13) { doOpen(); } }); I.cardnoinput.on( 'keydown', function (event) { if (event.keyCode == 13) { if (valid()) { doSave(); } return false; } }); } function doOpen() { if (!!I._complexSearch) { getList(vm.keyword(), vm.type(), vm.state(), 0, 1, I.PZ); } else { getList(vm.keyword(), null, null, 0, 1, I.PZ); } } function doSearch() { getList(vm.keyword(),null, null, I.pageValue,1, I.PZ); } function getList(keyword, type, state, page, pages, pagesize) { $.post('/basicinfo/card/getList.htm', { keyword: keyword, type: type, state: state, 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) { 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){ getList(keyword, type, state, num - 1, pages,pagesize); }); } } return; } }, 'json'); } function valid(){ var isValid = false; var iF = $('#form'); iF.validate({ rules: { 'cardno': { required: true, maxlength: 10, minlength:10 } }, messages: { 'cardno': { required: '不能为空!', maxlength: '卡号为10位数字', minlength: '卡号为10位数字' } }, 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.cardno + '】吗?'); $("#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('/basicinfo/card/delItem.htm', {id: item.cardno}, 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.cardno=$('#cardnoinput').val(); if(o.cardno=='') return; $.post('/basicinfo/card/saveItem.htm', o, function(data, textStatus, xhr) { I.AjaxSaveItem = false; I.cardnoinput.select(); if (data.code >= 2) { //parent.showErrmsg(data.errmsg); swal({title:'',text:data.errmsg,type:'error',confirmButtonText:'确定'}); return; } if (data.code == 1) { //location.replace(location.href); vm.list.push(o); return; } }, 'json'); } } function clearError() { $('#form input').removeClass('error'); $('#form .text-error').html(''); }