jquery.lgytable.js 16.3 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
// JavaScript Document
(function($){
	$.fn.lgytable = function (options) {//options 参数 
     var defaultVal = { 
	      tbtopheight:30,
		  tbconheight:30,
		  tbinfo:"客户",
		  tbtopbgcolor:"#fbfbfb",
		  tbpageshow:true,
		  tbsetnumshow:true,
		  tbsetpageshow:true,
		  tbtj:true,
          tbtitle:['<input type=checkbox class=cus_slAll \/>|center', '企业名称', '邮件地址', '联系人','联系电话', '状 态','地 区', '分 类', '最后联系'], 
		  tbwidth:['30', '200', '220', '10%', '10%', '10%', '10%', '10%', '10%'],  
          tbcon:[{"tbcon":"","dqpage":0,"tolpage":0,"tolcount":0}],
		  homepage:function(){},
		  prepage:function(){},
		  nextpage:function(){},
		  lastpage:function(){},
		  tbsetnumtxt:'<select id=lgy_setnum><option>10</option><option>20</option><option>30</option><option>40</option><option>50</option></select>',
		  tbsetnum:function(){alert($("#lgy_setnum").val())},
		  tbsetpagetxt:'<select id=lgy_setpage><option>1</option><option>2</option><option>3</option><option>4</option><option>5</option></select>',
        }; 
      //默认值 
      var obj = $.extend(defaultVal, options); 
	   return this.each(function () {
           var wo = $(this);//获取当前对象
			var tbzm=obj.tbtitle;
			var tbwd=obj.tbwidth;
			var tbconw=obj.tbcon[0].tbcon;
			var center="";
			//拼接表格
			var q="<table cellpadding=0 cellspacing=0 width=100%><tr class=tlbg style=\"background-color:"+obj.tbtopbgcolor+"\">";
			for(var i=0;i<tbzm.length;i++){
				var wdh=tbwd[i];
				var tbnm="";
				
				if(tbzm[i].indexOf("|")>=0){
					var tbname=tbzm[i].split("|");
					tbnm=tbname[0];
					if(tbname[1]=="center"){
					center="align=center";
					}
					}else{
						tbnm=tbzm[i];
						center="";
						}
				var wdh1,wdh2="";
				if(wdh.indexOf("%")>=0){
					wdh1="width="+tbwd[i];
					wdh2="";
					}else{
						wdh2="style=\"width:"+tbwd[i]+"px\"";
						wdh1="";
						}
				
				q+="<td "+wdh1+" "+center+" height="+obj.tbtopheight+" class=tbbl ><div class=whp "+wdh2+">"+tbnm+"</div></td>";
				}
				q+="</tr>";
				q+="<tbody id=tbycon>";
				if(tbconw.length>0){
				
				for(var i=0;i<tbconw.length;i++){
				q+="<tr>";
			   
				 for(var j=0;j<tbconw[i].length;j++){
					 var wdh=tbwd[j];
					 var tbnm2="";
			
	           var tnum=tbconw[i][j];

                if(tnum!=""&&tnum!=null&&tnum!=undefined){
					var tbname=tnum.split("|");
					if(tbname.length>0){
					     tbnm2=tbname[0];
						  if(tbname[1]=="center"){
						  center="align=center";
	  
						  }else{
							  tbnm2=tnum;
							  center="";
						 }
					}
				}else{
						tbnm2=tnum;
						center="";
				}
				var wdh1,wdh2="";
				if(wdh.indexOf("%")>=0){
					wdh1="width="+tbwd[j];
					wdh2="";
					}else{
						wdh2="style=\"width:"+tbwd[j]+"px\"";
						wdh1="";
						}
					
				     q+="<td "+wdh1+" "+center+" height="+obj.tbconheight+" class=unline><div class=whp "+wdh2+">"+tbnm2+"</div></td>";
				 }
				q+="</tr>";
				}
					}else{
	            q+="<tr><td class=\"pd20\" height="+obj.tbconheight+" colspan="+tbzm.length+"><div class=lgytable_null><div class=lgym1><img src=\"images/hc.png\" /></div><div class=lgym2>暂无数据</div></div></td></tr>";
				}
				q+="</tbody>";
				q+="</table>";
				if(tbconw.length>0){
				if(obj.tbpageshow){
				q+="<div class=lgy_z>";
				if(obj.tbtj){
				q+="<div class=lgy_tj_z> 合计:<span id=tolcount>"+obj.tbcon[0].tolcount+"</span> 个"+obj.tbinfo+"</div>";
				}
				if(obj.tbsetnumshow){
				q+="<div class=lgy_tj_z> 列表显示条数:  <span class=tb-num>"+obj.tbsetnumtxt+"</span></div>";
				}
				q+="<div class=lgy_tj_y><div class=\"lgy_home ypr\">首页</div><div class=\"lgy_pre ypr\">上一页</div><div class=\"lgy_next ypr\">下一页</div><div class=\"lgy_last ypr\">末页</div><div class=ypr>第 <span id=dqpage>"+obj.tbcon[0].dqpage+"</span>/<span id=tolpage>"+obj.tbcon[0].tolpage+"</span> 页</div>";
				if(obj.tbsetpageshow){
				q+="<div class=ypr> <span class=tb-num2>"+obj.tbsetpagetxt+"</span></div>";
				}
				q+="</div></div>";
				}
				}
				wo.html(q);
			    wo.find("tr:gt(0)").hover(function(){
				$(this).css("background-color","#fbfbfb");
				},function(){
				$(this).css("background-color","#ffffff");
				});
				//全选
				$(".cus_slAll").click(function(){
				  if($(this).attr("checked")=="checked"){
					    wo.find("input:checkbox[name='cus_utex']").attr("checked",true);
					  }else{
						   wo.find("input:checkbox[name='cus_utex']").attr("checked",false);
						  }
				});
				//列表数
				wo.find(".tb-num select").change(function(){
					obj.tbsetnum();
					});
				//翻页
				wo.find(".tb-num2 select").change(function(){
					obj.tbsetpage();
					});
			     var tbcontent="";
                $(".lgy_home").click(function(){
					 tbcontent=obj.homepage();
					  //alert(tbcon+"\n"+tbcontent);
					  if(tbcontent!=undefined && tbcontent.length>0){
					  pushpage(tbcontent[0].dqpage,tbcontent[0].tolpage,tbcontent[0].tolcount);
					  pushtable(tbcontent[0].tbcon);
					  }
					 });
				 $(".lgy_pre").click(function(){
					  tbcontent=obj.prepage();
			  
					if(tbcontent!=undefined && tbcontent.length>0){
					
					  pushpage(tbcontent[0].dqpage,tbcontent[0].tolpage,tbcontent[0].tolcount);
					  pushtable(tbcontent[0].tbcon);
					  }
					 });
				  $(".lgy_next").click(function(){
					 tbcontent=obj.nextpage();
					  //alert(tbcon+"\n"+tbcontent);
					  if(tbcontent!=undefined && tbcontent.length>0){
					  pushpage(tbcontent[0].dqpage,tbcontent[0].tolpage,tbcontent[0].tolcount);
					  pushtable(tbcontent[0].tbcon);
					   }
					  });
				   $(".lgy_last").click(function(){
					  tbcontent=obj.lastpage();
					  //alert(tbcon+"\n"+tbcontent);
					  if(tbcontent!=undefined && tbcontent.length>0){
					  pushpage(tbcontent[0].dqpage,tbcontent[0].tolpage,tbcontent[0].tolcount);
					  pushtable(tbcontent[0].tbcon);
					  }
					 });
		//当前页更换
		function pushpage(dqpage,tolpage,tolcount){
			$("#dqpage").html(dqpage);
			$("#tolpage").html(tolpage);
			$("#tolcount").html(tolcount);
			
			}
				   
		//内容更换	   
		function pushtable(tbcontent){
			$(".cus_slAll").attr("checked",false);
			var op="";
			 if(tbcontent=="" && tbcontent==null){
				 op+="<tr><td>暂无信息</td></tr>";
				 }else{
			 for(var i=0;i<tbcontent.length;i++){
				op+="<tr>";
				//alert(tbcontent[1].valueOf());
				 for(var j=0;j<tbcontent[i].length;j++){
					 var wdh=tbwd[j];
					 var tbnm2="";
				
				if(tbcontent[i][j].indexOf("|")>=0){
					var tbname=tbcontent[i][j].split("|");
					tbnm2=tbname[0];
					if(tbname[1]=="center"){
					center="align=center";
					}
					}else{
						tbnm2=tbcontent[i][j];
						center="";
						}
					 var wdh1,wdh2="";
					if(wdh.indexOf("%")>=0){
					wdh1="width="+tbwd[j];
					wdh2="";
					}else{
						wdh2="style=\"width:"+tbwd[j]+"px\"";
						wdh1="";
						}
				     op+="<td "+wdh1+" "+center+" height="+obj.tbconheight+" class=unline><div class=whp "+wdh2+">"+tbnm2+"</div></td>";
				 }
				op+="</tr>";
				}
			   $("#tbycon").html(op);
			      }
			}	
        });
	

}


//级联方法
$.fn.lgycascade = function (options) {//options 参数 
    var defaultVal = { 
         cjson:{"subDepts":[{"id":1,"name":"选择企业","subDepts":[{"id":11,"name":"科飞时速"},{"id":12,"name":"网易邮箱"}]},{"id":2,"name":"选择部门","subDepts":[{"id":13,"name":"第一级部门1","pid":11,"subDepts":[{"id":16,"name":"第二级部门2","pid":11,"subDepts":[]}]},{"id":14,"name":"第一级部门2","pid":12,"subDepts":[]}]},{"id":3,"name":"选择员工","pid":13,"subDepts":[{"id":1,"name":"张三","pid":14,"cid":12,"subDepts":[]},{"id":2,"name":"李四","pid":13,"cid":11,"subDepts":[]},{"id":3,"name":"王二麻子","pid":13,"cid":11,"subDepts":[]}]}]},
		 comevent:function(id){ 
		     alert("企业id:"+id);
		 },
		 depevent:function(id){
			 alert("部门id:"+id);
			 },
		 userevent:function(id){
			 alert("用户id:"+id);
			 }
}; 
      //默认值 
      var obj = $.extend(defaultVal, options); 
	   return this.each(function () {
           var wo = $(this);//获取当前对象
		   var q="";
		   var myjson=obj.cjson;
		   if(myjson!=""){
			  
			var q=showall(myjson.subDepts);
             wo.html(q);
			 $(".jl_c1").unbind("click").click(function(event){
				 event.stopPropagation();
				  var b=$(this).attr("button");
				  $(".jl_con").hide();
				 $(this).parent().find(".jl_con").show();
				 });

			 $(".jl_con ul li").unbind("click").click(function(){
				 var i=$(this).attr("role");
				  var id=$(this).attr("pid");
						  if(i==0){
							next1(myjson.subDepts,id); 
							obj.comevent(id);
						
						  }else if(i==1){
							 
							  next2(myjson.subDepts,id); 
							  obj.depevent(id);
							  }else{
								 obj.userevent(id); 
								  }
					});
				
		   }
		   
		  //递归数据
          function showall(listjson) {
			    for (var i in listjson) {
                  //如果有子节点,则遍历该子节点
                q+='<div class="jl_c0">';
				q+='<div class="jl_c1" button='+i+'>';
				q+='<div class="jl_m_1">'+listjson[i].name+'</div>';
				q+=' <div class="jl_m_4"></div>';
				q+=' </div>';

				q+=' <div class="jl_con">';
				q+='  <ul id=jl_c_'+i+'>';
				
					 for (var j in listjson[i].subDepts) {
                  //如果有子节点,则遍历该子节点
			
				      q+='<li pid='+listjson[i].subDepts[j].id+' role='+i+'>'+listjson[i].subDepts[j].name+'</li>';
					
							 for (var k in listjson[i].subDepts[j].subDepts) {
								 q+='<li class=bmjl pid='+listjson[i].subDepts[j].subDepts[k].id+' role='+i+'>'+listjson[i].subDepts[j].subDepts[k].name+'</li>'; 
								 }	
				 
				   }
				  q+='   </ul>';
				  q+='  </div>';
		
				//q+=showall2(listjson[i].subDepts);
				q+='</div>';
				
				}
				
				
				 return q;
				}
				
				function next1(listjson,id){
					var q="";
					var p="";
	                  for (var j in listjson[1].subDepts) {
						  var pid=listjson[1].subDepts[j].pid;
					    if(pid==id){
				        q+='<li pid='+listjson[1].subDepts[j].id+' role='+1+'>'+listjson[1].subDepts[j].name+'</li>';
					 
							 for (var k in listjson[1].subDepts[j].subDepts) {
								 q+='<li class=bmjl pid='+listjson[1].subDepts[j].subDepts[k].id+' role='+1+'>'+listjson[1].subDepts[j].subDepts[k].name+'</li>'; 
								 }	
						
						 
                   
					    }
					  $("#jl_c_1").html(q);
				  }
				  
				 
				   for (var j in listjson[2].subDepts) {
						  var cid=listjson[2].subDepts[j].cid;
					    if(cid==id){
				        p+='<li pid='+listjson[2].subDepts[j].id+' role='+2+'>'+listjson[2].subDepts[j].name;
					    p+='  </li>';	 
                   
					    }
					 
				  }
				   $("#jl_c_2").html(p);
				  
				   $(".jl_con ul li").unbind("click").click(function(){
				 var i=$(this).attr("role");
				  var id=$(this).attr("pid");
						  if(i==0){
						
							next1(myjson.subDepts,id); 
							obj.comevent(id);
						     }else if(i==1){
		
							  next2(myjson.subDepts,id); 
							   obj.depevent(id);
							  }else{
								 obj.userevent(id); 
								  }
					});
				}
				
				function next2(listjson,id){
					var q='';

	                  for (var j in listjson[2].subDepts) {
						  var pid=listjson[2].subDepts[j].pid;
					    if(pid==id){
				        q+='<li pid='+listjson[2].subDepts[j].id+' role='+2+'>'+listjson[2].subDepts[j].name;
					    
					    q+='  </li>';	 
                   
					    }
				  }
				   $("#jl_c_2").html(q);
				   $(".jl_con ul li").unbind("click").click(function(){
				 var i=$(this).attr("role");
				  
				  var id=$(this).attr("pid");
						  if(i==0){
							next1(myjson.subDepts,id);
							obj.comevent(id); 
						     }else if(i==1){
							  next2(myjson.subDepts,id); 
							   obj.depevent(id);
							  }else{
								 obj.userevent(id); 
								  }
					});
				}
	   });


}


//级联方法树型
$.fn.lgytree = function (options) {//options 参数 
    var defaultVal = { 
         cjson:{"subDepts":[{"id":1,"name":"科飞时速1","type":"0","subDepts":[{"id":11,"name":"科飞时2速","type":"1","subDepts":[{"id":123,"name":"科飞时速3","type":"2","subDepts":[]},{"id":124,"name":"科飞时速23","type":"2","subDepts":[]}]},{"id":12,"name":"部门2","type":"1","subDepts":[{"id":125,"name":"科飞时速3","type":"2","subDepts":[]},{"id":126,"name":"科飞时速34","type":"2","subDepts":[]}]}]},{"id":2,"name":"网易邮箱1","type":"0","subDepts":[]}]},
		 cnum:2,//从0开始算
		 cistrue:false,
		 cisone:true,
		 cidname:"tobody2",
		 onok:function(z){ 
			
			//ids=ids.substring(0,ids.length-1);

		     alert("返回例子用户id:"+z[0][0]+z[0][1]);	
		 }

}; 
      //默认值 
      var obj = $.extend(defaultVal, options); 
	 
	   return this.each(function () {
           var wo = $(this);//获取当前对象
		   var myjson=obj.cjson.subDepts;
		   var q='<div class="jl_c0">';
				q+='<div class="jl_c1">';
				q+='<div class="jl_m_1">请选择</div>';
				q+=' <div class="jl_m_4"></div>';
				q+=' </div>';

		  q+="<div class=jl_cs><ul class=prt30>";

		   for (var i in myjson) {
			   		var p=myjson[i].type;
					q+='<li><span class="itcn icn2" rol=1></span>'+myjson[i].name+' <span class=js_right><input type=checkbox p='+p+' value='+myjson[i].id+' /></span>';
					if(myjson[i].subDepts.length>0){
				
						q+="<ul>";
						
						q+=showall(myjson[i].subDepts);
						q+="</ul></li>";
						}
					}
		   q+="</ul>";
		   q+="<div class=jl_button><div class=jl_cannel>取消</div><div class=jl_ok>确定</div></div></div></div>";
           wo.html(q);
		    
		  
		   $(".jl_c1").unbind("click").click(function(event){
				 event.stopPropagation();
				 $(".jl_cs ul input:checkbox").attr("checked",false);
				  $(".jl_cs").hide();
				 $(this).parent().find(".jl_cs").show();
				 });
			$("#"+obj.cidname).unbind("click").click(function(event){
				event.stopPropagation();
				 $(".jl_cs").hide();
				});
			
		   $(".jl_cannel").unbind("click").click(function(event){
				event.stopPropagation();
				 $(".jl_cs").hide();
				});
			$(".jl_cs").unbind("click").click(function(event){
				event.stopPropagation();	
				$(".jl_cs").show();
			});
		   $(".jl_cs .itcn").unbind("click").click(function(event){
				event.stopPropagation();
				 var rol= $(this).attr("rol");
				 if(rol==0){
					 $(this).removeClass("icn1");
					 $(this).addClass("icn2");
					 $(this).attr("rol",1);
					$(this).parent().find("ul:first").children(0).removeClass("hide"); 
					 }else{
					 $(this).removeClass("icn2");
					 $(this).addClass("icn1");
					 $(this).attr("rol",0);
					 $(this).parent().find("ul:first").children(0).addClass("hide"); 
						 }
				});
				
			
		   //checkbox事件
		   if(obj.cistrue){
		   $(".jl_cs ul input:checkbox").click(function(event){
			   event.stopPropagation();
			   if($(this).attr("checked")=="checked"){
				  
				  $(this).parent().parent().find("ul input:checkbox").attr("checked",true);
				   }else{
					    $(this).parent().parent().find("ul input:checkbox").attr("checked",false);
					   }
			  
			   });
		   }
		    //checkbox事件
		   if(obj.cisone){
		   $(".jl_cs ul input:checkbox").click(function(event){
			   event.stopPropagation();
			   $(".jl_cs ul input:checkbox").attr("checked",false);
			   $(this).attr("checked","checked");
		   });

		   }
		   $(".jl_ok").unbind("click").click(function(event){
				event.stopPropagation();	
			   var z=new Array();
			  
			   var i=0;
			    $(".jl_cs ul input:checkbox").each(function() {
					
                    if($(this).attr("checked")=="checked"){
						 var r=new Array();
						var p=$(this).attr("p");
						
						  r[0]=p;
						  r[1]=$(this).val();
						  z[i]=r;
						  i=i+1;
						}
                });
			    obj.onok(z);
				close();
			   });
			   
           function close() {
		      $(".jl_cs").hide();
		     }
		   //递归方法
		     function showall(myjson) {
			    var q="";
				if(myjson.length>0){
			      for (var menu in myjson) {
					var qml="";
						var hd="";
					if(myjson[menu].type!=obj.cnum){
						qml='<span class="itcn icn1" rol=0></span>';
						}else{
							hd='class=hide';
							}
					q+='<li '+hd+'>'+qml+myjson[menu].name+'<span class=js_right><input type=checkbox p='+myjson[menu].type+' value='+myjson[menu].id+' /></span>';
				
					if(myjson[menu].subDepts.length>0){
					
						q+='<ul>';
						q+=showall(myjson[menu].subDepts);
						q+="</ul></li>";
						}
					}
				}
			    return q;
		} 
	   });
	   
	
	   
}

})(jQuery);