/**
edittable 选择人员插件
caratename:chenxihu
caratetime:2014-04-06 21:40:00
email:admin@rockoa.com
homepage:www.rockoa.com
*/
(function ($) {
function _getstyles(){
var s='';
return s;
}
function chnageuser(sobj, options){
var obj = sobj;
var rand = ''+parseInt(Math.random()*9999999)+'';
var me = this;
this.rand = rand;
this.changesel = '';
this.firstpid = 0;
this._init = function(){
for(var i in options)this[i]=options[i];
this.oveob = false;
if(this.showview!='' && get(this.showview))this.oveob=true;
if(!this.oveob){
window.onhashchange=function(){
var has = location.hash;
if(has.indexOf('#changeuser')==-1)me.hide();
}
js.location('#changeuser');
}
this.userarr = [];
this.deptarr = [];
this.grouparr= [];
if(isempt(this.changerange) && isempt(this.changerangeno)){
var us = js.getoption('userjson');
if(us)this.userarr = js.decode(us);
us = js.getoption('deptjson');
if(us)this.deptarr = js.decode(us);
us = js.getoption('groupjson');
if(us)this.grouparr = js.decode(us);
}
this.show();
};
this.creatediv=function(){
var type='checkbox';
if(this.changetype.indexOf('check')==-1)type='radio';
this.inputtype = type;
$('#changeuser_'+rand+'').remove();
var hei = $(window).height(),jhei=50,atts='position:fixed;';
if(this.oveob){
hei = $('#'+this.showview+'').height();
atts='';
}
var s='
';
if(this.titlebool){
s+='
'+this.title+'
';
jhei+=50;
}
if(this.changetype.indexOf('user')>=0){
s+='
';
jhei+=50;
}
s+='
';
s+='';
s+='';
s+='
';
var s3= '
';
if(type!='checkbox1')s3='';
if(1==1){
s3='
';
}
if(type=='checkbox'){
s+='
∨
';
}
var cold = window['maincolor'];if(!cold)cold='#1389D3';
s+='
';
s+=_getstyles();
s+='
';
if(atts==''){
$('#'+this.showview+'').html(s);
}else{
obj.append(s);
}
$('#changecancl_'+this.rand+'').click(function(){
me._clickcancel();
});
$('#changereload_'+this.rand+'').click(function(){
me._loaddata();
});
$('#changeok_'+this.rand+'').click(function(){
me.queding();
});
$('#changesoubtn_'+this.rand+'').click(function(){
me._searchkey(true);
});
$('#changekey_'+this.rand+'').keydown(function(e){
me._searchkeys(e)
});
$('#changeboxs_'+this.rand+'').click(function(){
me._changboxxuan(this)
});
$('#changesel_'+this.rand+'').change(function(){
me._changesel(this)
});
if(type=='checkbox'){
$('#yixuanbtn_'+rand+'').click(function(){
$('#yixuan_'+rand+'').toggle();
});
this._initmrvel();
}
rchanguserclick=function(o1){
me._changexuan(o1);
}
rchanguserquxiao=function(o1){
me._changequxiao(o1);
}
};
this.showlist=function(pid,oi){
var type=this.inputtype,hw=24;
var s='',ssu='',s1='';
var sel = this.changesel;
var dob = this.changetype.indexOf('dept')==-1;
var uob = this.changetype.indexOf('user')>=0;
this.fid = 1;
if(sel=='1'){
ssu = this._showuser(0,'',type,sel);
}else if(sel=='2'){
s = this._showdept(pid,oi,s1,type,sel,dob,uob);
}else if(sel=='3'){
s = this._showgorup(type);
}else{
s1='';
s = this._showdept(pid,oi,s1,type,sel,dob,uob);
if(uob){
ssu+=this._showuser(pid,s1,type,sel);
}
}
var xud = (oi==0)?'0' : pid;
$('#showdiv'+rand+'_'+xud+'').html(ssu+s).attr('show','true');
if(sel==''){
if(oi==0)this.showlist(this.fid, 1);
$('#showdiv'+rand+'_0 [deptxu]').unbind('click').click(function(){
me._deptclicks(this);
});
}
if(sel=='3'){
$('#showdiv'+rand+'_0 [groupxu]').unbind('click').click(function(){
me._groupclicks(this);
});
}
};
this._showuser=function(pid,s1,type,sel){
var a,len,i,ssu='',dids,zoi=0,ids,chk='';
a=this.userarr;
len=a.length;
for(i=0;i-1 || sel=='1') && sel!='3')
||
(sel=='3' && (','+a[i].groupname+',').indexOf(','+pid+',')>-1)//显示组下人员
){
chk='';
if(get('show'+rand+'_u'+a[i].id+''))chk='checked ';
ssu+='';
zoi++;
if(zoi>=200)break;
}
}
return ssu;
};
this._showdept=function(pid,oi,s1,type,sel,dob,uob){
var a,len,i,wwj,s2='',s='';
a=this.deptarr;
len=a.length;
for(i=0;i';
if(dob)s2='';
if(s2!='' && !this._isdeptcheck(a[i]))s2='';
s+='';
s+='
'+s1+' | '+a[i].name+' | '+s2+' |
';
s+='
';
s+='';
}
}
return s;
};
this._showgorup=function(type){
var a,len,i,ssu='',s1;
a=this.grouparr;
len=a.length;
for(i=0;i';
if(this.changetype.indexOf('deptuser')==-1)s1='';
ssu+='';
ssu+='
| '+a[i].name+' ('+a[i].usershu+'人) | '+s1+' |
';
ssu+='
';
ssu+='';
}
return ssu;
};
this._groupclicks=function(o){
if(this.changetype.indexOf('user')==-1)return;
var sxu = $(o).attr('groupxu');
var a = this.grouparr[sxu];
var o1 = $('#showgroup'+rand+'_'+a.id+'');
var lx = o1.attr('show');
if(lx=='false'){
var s1='';
var s = this._showuser(a.id,s1,this.inputtype, this.changesel);
o1.html(s).attr('show','true');
}else{
o1.toggle();
}
};
this._changexuan=function(o1){
if(this.inputtype!='checkbox')return;
var o = $(o1),xls=o.attr('xls'),val=o.val();
var sid = 'show'+rand+'_'+xls+''+val+'';
if(o1.checked){
var str='';
str+=''+o.attr('xname')+'';
str+='';
if(!get(sid))$('#yixuan_'+rand+'').append(str);
}else{
$('#'+sid+'').remove();
}
};
this._changequxiao=function(o1){
var o = $(o1),xls=o.attr('xls'),xvl=o.attr('xvl');
o.remove();
$("input[name='changeuserinput_"+rand+"'][xls='"+xls+"'][value='"+xvl+"']").attr('checked', false);
};
this._initmrvel=function(){
var sid ='',sna='',i,str='',xls,val;
if(this.idobj)sid=this.idobj.value;
if(this.nameobj)sna=this.nameobj.value;
if(!sid || !sna)return;
var sida = sid.split(','),snaa=sna.split(',');
var ob1=this.changetype.indexOf('dept')>-1,ob2=this.changetype.indexOf('user')>-1;
for(i=0;i';
str+=''+snaa[i]+'';
str+='';
}
$('#yixuan_'+rand+'').html(str);
};
this._changesel=function(o1){
var val = o1.value;
this.changesel = val;
this.showlist(this.firstpid, 0);
};
this._searchkeys=function(e){
clearTimeout(this._searchkeystime);
this._searchkeystime=setTimeout(function(){
me._searchkey(false);
},500);
};
this._isdeptcheck=function(a){
if(this.inputtype=='checkbox' && this.changetype.indexOf('user')>=0 && this.changetype.indexOf('dept')>=0){
var stotal,i,nstotal=0,len=this.userarr.length,spath;
stotal = parseFloat(a.stotal);
for(i=0;i=0)nstotal++;
}
return nstotal>=stotal;
}else{
return true;
}
},
this._clickcheckbox=function(o1){
var o = $(o1),xu,a,stotal,i,nstotal=0,len=this.userarr.length,spath;
if(o.attr('xls')!='d')return;
xu = parseFloat(o.attr('xu'));
a = this.deptarr[xu];
stotal = parseFloat(a.stotal);
for(i=0;i=0)nstotal++;
}
if(nstotal-1 || a[i].pingyin.indexOf(key)==0 || a[i].deptname.indexOf(key)>-1 || a[i].ranking.indexOf(key)>-1){a[i].xu=i;d.push(a[i])};
len = d.length;
for(i=0;i';
s+='';
s+='';
}
if(bo && s=='' && key!='')js.msg('msg','无相关['+key+']的记录', 2);
$('#showdiv'+rand+'_search').html(s);
var o1 = $('#showdiv'+rand+'_0');
if(s==''){o1.show();}else{o1.hide();}
};
this._clickcancel=function(){
if(!this.oveob)history.back();
this.hide();
};
this.hide=function(){
$('#changeuser_'+rand+'').remove();
this.oncancel();
};
this.show=function(){
this.creatediv();
if(this.deptarr.length>0){
this.firstpid = this.deptarr[0].pid;
this.showlist(this.firstpid,0);
}else{
this._loaddata();
}
};
this._deptclicks=function(o){
var sxu = $(o).attr('deptxu').split('_');
var a = this.deptarr[sxu[0]];
var o1 = $('#showdiv'+rand+'_'+a.id+'');
var lx = o1.attr('show');
if(lx=='false'){
this.showlist(a.id, parseFloat(sxu[1])+1);
}else{
o1.toggle();
}
};
this._loaddata=function(){
var o1 = $('#showdiv'+rand+'_0'),url;
o1.html('
');
var url = 'index.php?a=deptuserjson&m=dept&d=system&ajaxbool=true&changerange='+this.changerange+'&changerangeno='+this.changerangeno+'>ype=change';
$.getJSON(url, function(ret){
if(ret.code==200){
ret = ret.data;
me._loaddatashow(ret);
}else{
o1.html(ret.msg);
}
});
};
this._loaddatashow=function(ret){
if(isempt(this.changerange) && isempt(this.changerangeno)){
js.setoption('deptjson', ret.deptjson);
js.setoption('userjson', ret.userjson);
js.setoption('groupjson', ret.groupjson);
}
this.userarr = js.decode(ret.userjson);
this.deptarr = js.decode(ret.deptjson);
this.grouparr = js.decode(ret.groupjson);
this.firstpid = 0;
if(this.deptarr[0])this.firstpid = this.deptarr[0].pid;
this.showlist(this.firstpid, 0);
};
this._changboxxuan=function(os){
var ns= 'changeuserinput_'+rand+'';
if($('#showdiv'+rand+'_search').html()!='')ns+='_soukey';
var ob = os.checked,o=$("input[name='"+ns+"']"),i;
for(i=0;i';
s+='';
s+='
'+this.title+'
';
s+='
';
s+='
';
s+='
';
s+='
';
s+='';
s+=_getstyles();
$('body').append(s);
this.showdata(this.data);
var o = this._getobj('main');
var l = ($(window).width()-o.width())*0.5,t = ($(window).height()-o.height())*0.5;
o.css({'left':''+l+'px','top':''+t+'px'});
$('#changecancl_'+this.rand+'').click(function(){
me._clickcancel();
});
$('#changeok_'+this.rand+'').click(function(){
me.queding();
});
$('#changereload_'+this.rand+'').click(function(){
me.reload();
});
$('#changesoubtn_'+this.rand+'').click(function(){
me._searchkey(true);
});
$('#changekey_'+this.rand+'').keydown(function(e){
me._searchkeys(e)
});
$('#changekey_'+this.rand+'').keyup(function(e){
me._searchkeys(e)
});
};
this._getobj=function(lx){
var o = $('#selectdata_'+rand+'').find("[tsid='"+lx+"']");
return o;
};
this._clickcancel=function(){
this.hide();
};
this.hide=function(){
$('#selectdata_'+rand+'').remove();
this.oncancel();
};
this.queding=function(){
var ns= 'changeuserinput_'+rand+'';
var o = $("input[name='"+ns+"']");
var i,len=o.length,o1,xna,xu,xal,sid='',sna='',seld=[];
for(i=0;i0){
this.showselbo=true;
}
$(o).change(function(){
me._changeselval(this);
}).show();
};
this._changeselval=function(o){
this.selvalue = o.value;
this.loaddata(o.value);
};
this.showhtml=function(a,ks){
this.nowdata = a;
var i,len=a.length,s='',s2,s1='',atr,oldvel='',d;
if(this.nameobj)oldvel=this.nameobj.value;
if(this.idobj)oldvel=this.idobj.value;
var type='checkbox',ched='';
if(!ks)ks=0;
if(!this.checked)type='radio';
oldvel = ','+oldvel+',';
for(i=ks;i-1)ched='checked';
if(d.disabled)ched+=' disabled';
s2 = '';
atr = '';
if(d.padding)atr='style="padding-left:'+d.padding+'px"';
if(!d.iconswidth)d.iconswidth=18;
if(d.iconsimg)s2+='
';
s+='';
}
if(len>i){
s+='还有'+(len-i)+'条,点我加载
';
setTimeout(function(){$('#moreadd_'+rand+'').click(function(){me.moregegd(i);});},10);
}
return s;
};
this.moregegd=function(i1){
$('#moreadd_'+rand+'').remove();
var s = this.showhtml(this.nowdata, i1);
if(s)$('#selectlist_'+rand+'').append(s);
};
this.reload=function(){
this.loaddata(this.selvalue);
};
this.loaddata=function(svel){
var url = this.url;
if(svel)url+='&selvalue='+svel+'';
if(url=='')return;
$('#selectlist_'+rand+'').html('
');
$.ajax({
type:'get',dataType:'json',url:url,
success:function(ret){
me.data = ret;
me.onloaddata(ret);
me.showdata(ret, true);
},
error:function(e){
$('#selectlist_'+rand+'').html('加载错误:
');
}
});
};
this._searchkeys=function(e){
clearTimeout(this._searchkeystime);
this._searchkeystime=setTimeout(function(){
me._searchkey(false);
},500);
};
this._searchkey = function(bo){
var key = $('#changekey_'+this.rand+'').val(),a=[],d=[],d1,len,i,oi=0,s;
a=this.data;
if(a.rows)a=a.rows;
len=a.length;if(len==0)return;
if(key)key = key.toLowerCase();
if(key!='')for(i=0;i-1 || d1.name.toLowerCase().indexOf(key)>-1 || d1.value==key || (d1.subname && d1.subname.indexOf(key)>-1)){
d.push(d1);
oi++;
if(oi>20)break;//最多显示搜索
}
}
len = d.length;
if(len==0){
if(key){
s='无相关['+key+']的记录
';
}else{
s=this.showhtml(a);
}
}else{
s=this.showhtml(d);
}
$('#selectlist_'+rand+'').html(s);
if(bo && len==0 && key!='')js.msg('msg','无相关['+key+']的记录', 2);
};
}
$.fn.chnageuser = function(options){
var defaultVal = {
'title' : '请选择...',
'titlebool':true,
'showview':'',
'changerange':'', //从哪些人员中选择
'changerangeno':'', //不从哪些人选择
'changetype' : 'user',
'idobj':false,'nameobj':false,
'onselect':function(){},
'oncancel':function(){}
};
var can = $.extend({}, defaultVal, options);
var funcls = new chnageuser($(this), can);
funcls._init();
return funcls;
};
$.selectdata = function(options){
var defaultVal = {
'showview': '',
'title' : '请选择...',
'maxshow' : 100, //最多显示防止卡死浏览器
'data' : [], 'url' : '',
'checked' : false,
'idobj' : false, 'nameobj':false,
'onselect': function(){},
'oncancel': function(){},
'onloaddata':function(){}
};
var can = $.extend({}, defaultVal, options);
var funcls = new selectdata(false, can);
funcls._init();
return funcls;
};
})(jQuery);