window.I = {} var basic = getpath(); pagesize = 20; var modelview = function() { var self = this; self.filter = ko.observable(); self.role = ko.observable(parseInt($('#roleid').val())); self.role1 = ko.observable(parseInt($('#roleid').val())); self.roles = ko.observableArray(); self.users = ko.observableArray(); self.user = ko.observable({ id : ko.observable(0), username : ko.observable(), email : ko.observable(), role : ko.observable({ id : ko.observable(0), name : ko.observable() }) }); self.edittitle = ko.observable('编辑账户'); self.change = function(item) { var roleid = self.role(); // 更新users users(0, 1, 0, md.filter(), roleid); }; self.valid = function(item) { jQuery.post(basic + '/admin/usercon/valid.htm', { id : item.id }, function(data, textStatus, xhr) { if (data == null) return; if (data.valid == 1 || data.valid == true) item.valid(true); else item.valid(false); }, 'json'); }; self.rm = function(item) { if (confirm("确认要删除吗?")) { jQuery.post(basic + '/admin/usercon/rm.htm', { id : item.id }, function(data, textStatus, xhr) { if (data == null) return; if (data.code == 0) { self.users.remove(item); } else { alert(data.errmsg); } }, 'json'); } }; self.rstpwd = function(item) { jQuery.post(basic + '/admin/usercon/rstpwd.htm', { id : item.id }, function(data, textStatus, xhr) { if (data == null) return; if (data.code == 0) { alert('密码修改成功,新密码为:' + data.npwd); } else { alert(data.errmsg); } }, 'json'); }; self.edit = function(item) { if (item.role() != null) { self.role1(item.role().id()); } self.user(item); self.edittitle('编辑账户'); $('#modal1').modal('show'); } } jQuery(document).ready( function($) { window.mtype = $('#mtype').val(); var md = window.md = new modelview(); ko.applyBindings(md); var tid = $('#tid').val(); users(0, 1, tid, md.filter(), md.role()); roleinit(md.role()); $('#searcha').click(function() { users(0, 1, 0, md.filter(), md.role()); }); $('#ctbtn').click(function() { window.md.edittitle('新建账户'); // window.open(basic+'/administrate/system/user.jsf?roleid='+md.role()); md.user({ id : ko.observable(0), username : ko.observable(), email : ko.observable(), role : ko.observable({ id : ko.observable(0), name : ko.observable() }) }); $('#modal1').modal('show'); }); var fm1 = $('#fm1'); fm1.validate({ rules : { username : { required : true, remote : { url : basic + '/admin/usercon/username.htm', type : 'post', data : { username : function() { return $('#username').val(); }, id : function() { return $('#id').val() } } } }, email : { required : true, remote : { url : basic + '/admin/usercon/email.htm', type : 'post', data : { email : function() { return $('#email').val(); }, id : function() { return $('#id').val(); } } } } }, messages : { username : { required : '不能为空', remote : '不能重复', maxlength : 30, minlength : 6 }, email : { required : '不能为空', maxlength : '最大长度不能超过30', minlength : '最小长度不能小于6', remote : '不能重复' } } }); $('#tjbtn').click( function() { if (fm1.valid() == false) return; if (window.save == 1) return; window.save = 1; var tt = md.user(); var index = md.users().indexOf(tt); var obj = ko.mapping.toJS(tt); obj.role = {}; obj.role.id = md.role1(); if(obj.role.id<=0) { window.save=0; alert('角色不能选所有角色!'); return; } jQuery.post(basic + '/admin/usercon/save.htm', obj, function(data, textStatus, xhr) { window.save=0; $('#modal1').modal('hide'); if (data == null) return; if (data.code == 1) { alert(data.errmsg); } else { var val = data.user; var role = val.role; val = ko.mapping.fromJS(val); if (role != null && typeof (role) == 'object') val.role = ko.observable(val.role); if (tt.id() == 0) { md.users.push(val); } else md.users.splice(index, 1, val); } }, 'json'); }); }); function users(page, pages, tid, filter, roleid) { jQuery.post(basic + '/admin/usercon/users.htm', { page : page, pagesize : pagesize, pages : pages, tid : tid, filter : filter, roleid : roleid }, function(data, textStatus, xhr) { if (data == null) return; if (data.page != null) page = data.page; if (pages > 0) { $('#pagdiv').unbind('page').empty(); if (data.pages > 1) { $('#pagdiv').bootpag({ total : data.pages, maxVisible : 10, page : page + 1 }).on('page', function(event, num) { users(num - 1, 0, 0, filter, roleid); }); } } window.md.users.removeAll(); if (data.items != null) { jQuery.each(data.items, function(index, val) { var role = val.role; val = ko.mapping.fromJS(val); if (role != null && typeof (role) == 'object') val.role = ko.observable(val.role); window.md.users.push(val); }); } }, 'json'); } function roleinit(roleid) { jQuery.post(basic + '/admin/usercon/roleinit.htm', { roleid : roleid }, function(data, textStatus, xhr) { if (data == null) return; window.md.roles.removeAll(); window.md.roles.push({id:0,name:'所有角色'}); if (!!data.items) { jQuery.each(data.items, function(index, val) { window.md.roles.push(val); }); } }, 'json'); }