代码如下
else if (p.dataType == 'xml') { i = 1; //flexigrid 树形改造 function initnode(node,levarr){ var ht=new Array(); var arrlen=levarr.length; var hasChild = $(node).attr('hasChild'); ht.push("<span class='treegrid_folder' hidden='",!(p.isOpen),"' name='",levarr,"'>"); for(var i=0;i<arrlen-1;i++){ if(levarr[i]==1){ ht.push("<img alt='' src='",p.themePath,"elbow-line.gif","' width=16px></img>"); } else{ ht.push("<img alt='' src='",p.themePath,"s.gif","' width=16px></img>"); } } if(arrlen>0){ if(levarr[arrlen-1]==1){ if(p.isOpen){ if(hasChild=='true'){ ht.push("<img alt='' class='treegrid_folder EMinus' src='",p.themePath,"elbow-minus.gif","'></img>"); } else{ ht.push("<img alt='' src='",p.themePath,"elbow.gif","'></img>"); } } else{ if(hasChild=='true'){ ht.push("<img alt='' class='treegrid_folder EMinus' src='",p.themePath,"elbow-plus.gif","'></img>"); } else{ ht.push("<img alt='' src='",p.themePath,"elbow.gif","'></img>"); } } } else{ if(p.isOpen){ if(hasChild=='true'){ ht.push("<img alt='' class='treegrid_folder EEndMinus' src='",p.themePath,"elbow-end-minus.gif","'></img>"); } else{ ht.push("<img alt='' src='",p.themePath,"elbow-end.gif","'></img>"); } } else{ if(hasChild=='true'){ ht.push("<img alt='' class='treegrid_folder EEndMinus' src='",p.themePath,"elbow-end-plus.gif","'></img>"); } else{ ht.push("<img alt='' src='",p.themePath,"elbow-end.gif","'></img>"); } } } } if(hasChild=='true'){ if(p.isOpen){ ht.push("<img alt='' class='folderisopen' src='",p.themePath,"folder-open.gif","'></img>"); } else{ ht.push("<img alt='' class='folderisopen' src='",p.themePath,"folder.gif","'></img>"); } }else{ ht.push("<img alt='' imgname='",levarr,"' src='",p.themePath,"leaf.gif","'></img>"); } ht.push("</span>"); return ht.join(""); }
function buildnode(node,dataxml,levarr){//递归建立树 //当前节点,处理一个node 在xml中清理一个数据之后的数组 var robj = node; var arrdata = new Array(); var arrdatach = new Array(); var nid = $(node).attr('id'); var parentid = $(node).attr('parent'); arrdatach.push(nid); arrdatach.push(parentid); $("cell", robj).each(function() { arrdata.push($(this).text()); arrdatach.push($(this).text()); }); tbhtml.push("<tr id='", "row", nid, "'"); if (p.rowbinddata) { tbhtml.push("ch='", arrdatach.join("_FG$SP_"), "'"); } tbhtml.push(">"); var trid = nid; $(ths).each(function(j) { tbhtml.push("<td align='", this.align, "'"); if (this.hide) { tbhtml.push(" style='display:none;'"); } var tdclass = ""; var tddata = ""; var idx = $(this).attr('axis').substr(3);
if (p.sortname && p.sortname == $(this).attr('abbr')) { tdclass = 'sorted'; } var width = thsdivs[j].style.width; var div = []; div.push("<div style='text-align:", this.align, ";width:", width, ";"); if (p.nowrap == false) { div.push("white-space:normal"); } div.push("'>");
if (idx == "-1") { //checkbox,treenode div.push("<input type='checkbox' id='chk_", nid, "' class='itemchk' value='", nid, "'/>"); if (tdclass != "") { tdclass += " chboxtd"; } else { tdclass += "chboxtd"; } } else if(j==1){ var divInner = arrdata[idx] || " "; if (p.rowbinddata) { tddata = arrdata[idx] || ""; } if (this.process) { divInner = this.process(divInner, trid); } //添加树图标 div.push(initnode(node,levarr),divInner); } else { var divInner = arrdata[idx] || " "; if (p.rowbinddata) { tddata = arrdata[idx] || ""; } if (this.process) { divInner = this.process(divInner, trid); } div.push(divInner); } div.push("</div>"); if (tdclass != "") { tbhtml.push(" class='", tdclass, "'"); } tbhtml.push(" axis='", tddata, "'", ">",div.join(""), "</td>"); }); tbhtml.push("</tr>"); var arr=new Array(); var newdataxml=new Array(); $(dataxml).each(function(){ var parentid = $(this).attr('parent'); if(parentid==nid){ arr.push($(this)); }else{ newdataxml.push($(this)); } }); if(arr.length>0){ level++; }//有子节点 层++ else if(arr.length==0 &&levpos[level]==0){//无子节点,并且为本层最后一个节点 层-- 数组pop while(arr.length==0 &&levpos[level]==0){ level--; levpos.pop(); } } var arrindex=0; $(arr).each(function(){ arrindex++; if(arr.length==arrindex){ levpos[level]=0; buildnode($(this),newdataxml,levpos); }//最后一个 else{ levpos[level]=1; buildnode($(this),newdataxml,levpos); }//非最后一个
}); //************************ } ///
加载数据结构
<?xml version="1.0" encoding="utf-8" ?><grid> <page>1</page> <total>88</total> <rows> <row id='1' parent='0' hasChild='true'> <cell> <![CDATA[1]]> </cell> <cell> <![CDATA[苹果汁]]> </cell> <cell> <![CDATA[每箱24瓶]]> </cell> <cell> <![CDATA[18.0000]]> </cell> <cell> <![CDATA[39]]> </cell> <cell> <![CDATA[0]]> </cell> <cell> <![CDATA[1]]> </cell> </row> <row id='2' parent='0' hasChild='true'> <cell> <![CDATA[2]]> </cell> <cell> <![CDATA[牛奶]]> </cell> <cell> <![CDATA[每箱24瓶]]> </cell> <cell> <![CDATA[19.0000]]> </cell> <cell> <![CDATA[17]]> </cell> <cell> <![CDATA[40]]> </cell> <cell> <![CDATA[2]]> </cell> </row> <row id='22' parent='2' hasChild='false'> <cell> <![CDATA[22]]> </cell> <cell> <![CDATA[牛奶]]> </cell> <cell> <![CDATA[每箱24瓶]]> </cell> <cell> <![CDATA[19.0000]]> </cell> <cell> <![CDATA[17]]> </cell> <cell> <![CDATA[40]]> </cell> <cell> <![CDATA[2]]> </cell> </row> <row id='3' parent='1' hasChild='false'> <cell> <![CDATA[3]]> </cell> <cell> <![CDATA[牛奶]]> </cell> <cell> <![CDATA[每箱24瓶]]> </cell> <cell> <![CDATA[19.0000]]> </cell> <cell> <![CDATA[17]]> </cell> <cell> <![CDATA[40]]> </cell> <cell> <![CDATA[3]]> </cell> </row> <row id='4' parent='1' hasChild='false'> <cell> <![CDATA[4]]> </cell> <cell> <![CDATA[牛奶]]> </cell> <cell> <![CDATA[每箱24瓶]]> </cell> <cell> <![CDATA[19.0000]]> </cell> <cell> <![CDATA[17]]> </cell> <cell> <![CDATA[40]]> </cell> <cell> <![CDATA[3]]> </cell> </row> <row id='5' parent='1' hasChild='false'> <cell> <![CDATA[5]]> </cell> <cell> <![CDATA[牛奶]]> </cell> <cell> <![CDATA[每箱24瓶]]> </cell> <cell> <![CDATA[19.0000]]> </cell> <cell> <![CDATA[17]]> </cell> <cell> <![CDATA[40]]> </cell> <cell> <![CDATA[3]]> </cell> </row> <row id='6' parent='1' hasChild='false'> <cell> <![CDATA[6]]> </cell> <cell> <![CDATA[牛奶]]> </cell> <cell> <![CDATA[每箱24瓶]]> </cell> <cell> <![CDATA[19.0000]]> </cell> <cell> <![CDATA[17]]> </cell> <cell> <![CDATA[40]]> </cell> <cell> <![CDATA[3]]> </cell> </row> <row id='7' parent='1' hasChild='true'> <cell> <![CDATA[7]]> </cell> <cell> <![CDATA[牛奶]]> </cell> <cell> <![CDATA[每箱24瓶]]> </cell> <cell> <![CDATA[19.0000]]> </cell> <cell> <![CDATA[17]]> </cell> <cell> <![CDATA[40]]> </cell> <cell> <![CDATA[3]]> </cell> </row> <row id='8' parent='7' hasChild='false'> <cell> <![CDATA[8]]> </cell> <cell> <![CDATA[牛奶]]> </cell> <cell> <![CDATA[每箱24瓶]]> </cell> <cell> <![CDATA[19.0000]]> </cell> <cell> <![CDATA[17]]> </cell> <cell> <![CDATA[40]]> </cell> <cell> <![CDATA[3]]> </cell> </row> <row id='9' parent='7' hasChild='false'> <cell> <![CDATA[9]]> </cell> <cell> <![CDATA[牛奶]]> </cell> <cell> <![CDATA[每箱24瓶]]> </cell> <cell> <![CDATA[19.0000]]> </cell> <cell> <![CDATA[17]]> </cell> <cell> <![CDATA[40]]> </cell> <cell> <![CDATA[3]]> </cell> </row> <row id='10' parent='7' hasChild='true'> <cell> <![CDATA[10]]> </cell> <cell> <![CDATA[牛奶]]> </cell> <cell> <![CDATA[每箱24瓶]]> </cell> <cell> <![CDATA[19.0000]]> </cell> <cell> <![CDATA[17]]> </cell> <cell> <![CDATA[40]]> </cell> <cell> <![CDATA[3]]> </cell> </row> <row id='11' parent='0' hasChild='false'> <cell> <![CDATA[11]]> </cell> <cell> <![CDATA[牛奶]]> </cell> <cell> <![CDATA[每箱24瓶]]> </cell> <cell> <![CDATA[19.0000]]> </cell> <cell> <![CDATA[17]]> </cell> <cell> <![CDATA[40]]> </cell> <cell> <![CDATA[3]]> </cell> </row> <row id='13' parent='10' hasChild='false'> <cell> <![CDATA[13]]> </cell> <cell> <![CDATA[牛奶]]> </cell> <cell> <![CDATA[每箱24瓶]]> </cell> <cell> <![CDATA[19.0000]]> </cell> <cell> <![CDATA[17]]> </cell> <cell> <![CDATA[40]]> </cell> <cell> <![CDATA[3]]> </cell> </row> <row id='14' parent='1' hasChild='true'> <cell> <![CDATA[14]]> </cell> <cell> <![CDATA[牛奶]]> </cell> <cell> <![CDATA[每箱24瓶]]> </cell> <cell> <![CDATA[19.0000]]> </cell> <cell> <![CDATA[17]]> </cell> <cell> <![CDATA[40]]> </cell> <cell> <![CDATA[3]]> </cell> </row> <row id='15' parent='14' hasChild='false'> <cell> <![CDATA[15]]> </cell> <cell> <![CDATA[牛奶]]> </cell> <cell> <![CDATA[每箱24瓶]]> </cell> <cell> <![CDATA[19.0000]]> </cell> <cell> <![CDATA[17]]> </cell> <cell> <![CDATA[40]]> </cell> <cell> <![CDATA[3]]> </cell> </row> <row id='16' parent='14' hasChild='false'> <cell> <![CDATA[16]]> </cell> <cell> <![CDATA[牛奶]]> </cell> <cell> <![CDATA[每箱24瓶]]> </cell> <cell> <![CDATA[19.0000]]> </cell> <cell> <![CDATA[17]]> </cell> <cell> <![CDATA[40]]> </cell> <cell> <![CDATA[3]]> </cell> </row> </rows></grid>
未完成:
1.树的异步加载
2.树节点选择 其子节点 同时选择