1. 程式人生 > >jQuery製作樹狀表格

jQuery製作樹狀表格

jQuery(function(){ autoFirstClick(); }) function autoFirstClick(){ jQuery(jQuery("#drillTable").find("a")[0]).click(); } cot = 1; function getTestDate(){ var data = [{"deptId":"root"+ ++cot,"areaNo":"091","deptDesc":"desc"+ ++cot,"isLeaf":"yes"}, {"deptId"
:"root"+ ++cot,"areaNo":"091","deptDesc":"desc"+ ++cot,"isLeaf":"no"}, {"deptId":"root"+ ++cot,"areaNo":"091","deptDesc":"desc"+ ++cot,"isLeaf":"no"}]; return data; } //當前層的開閉 function showClose(deptId){ jQuery("tr[parent_id='" + deptId + "']").each(function
(i){
var obj = jQuery(this); var temp_rank = obj.attr("d_orgrank"); var temp_flag = obj.attr("isShow"); if("yes" == temp_flag){ obj.attr("isShow", temp_flag == 'yes' ? 'no' : 'yes'); jQuery("tr[d_orgrank^='" + temp_rank + "/"
+ "']").hide(); obj.hide(); }else if("no" == temp_flag){ obj.attr("isShow", temp_flag == 'yes' ? 'no' : 'yes'); obj.show(); showCloseChild(jQuery(this)); return; } }); } //子集遞迴 function showCloseChild(obj){ var temp_flag = obj.attr("isShow"); var temp_rank = obj.attr("d_orgrank"); var temp_dept_id = obj.attr("d_dept_id"); if("yes" == temp_flag){ obj.show(); jQuery("tr[parent_id='" + temp_dept_id + "']").each(function(i){ showCloseChild(jQuery(this)); }); }else { return; } } //下鑽 //@auth nayi_224 function drillDept(td){ jQuery(td).find("#img").html(jQuery(td).find("#img").html() == "+" ? "-" : "+"); var objTr = jQuery(td).parent().parent(); var d_orgrank_all = objTr.attr("d_orgrank"); var d_level= objTr.attr("d_level"); var level = parseInt(d_level.substring("d_level_".length)); var deptId = objTr.attr("d_dept_id"); var isExists = jQuery("tr[parent_id='" + deptId + "']"); if(isExists.html()){ showClose(deptId); return; } //動態頁面使用ajax //var url = "<%=contextPath%>/NayiTest!drillTable.action?aa="+Math.random(); //jQuery.ajax({ // url:url, // type:'post', // data:{ // sDept:deptId // }, // success:function(result) { var result = getTestDate(); //獲取測試資料 var tJson = eval(result); var tHtml = ""; var space = ""; for(var i = 0; i < level; i++){ space += "&nbsp&nbsp"; } for(var i = 0; i < tJson.length; i++){ var tempDeptId = tJson[i].deptId; //需要改的地方 tHtml += "<tr d_dept_id='" + tempDeptId + "'"; tHtml += " isShow='yes' "; tHtml += " parent_id='" + deptId + "' "; tHtml += " d_orgrank='" + d_orgrank_all + "/" + tempDeptId; tHtml += "' d_level='d_level_" + (level + 1) + "' >" if("no" == tJson[i].isLeaf){ tHtml += "<td style='text-align:left;'>" + space + "<a onclick='drillDept(this)'><span id='img'>+</span>" + tJson[i].deptDesc + "</a></td>"; //需要改的地方 }else { tHtml += "<td style='text-align:left;'>" + space + tJson[i].deptDesc + "</td>"; //需要改的地方 } tHtml += "<td>" + tJson[i].areaNo + "</td>"; //需要改的地方 tHtml += "<td>" + tJson[i].deptId + "</td>"; //需要改的地方 tHtml += "</tr>"; } objTr.after(tHtml); // } //}); }