grails树形结构图

用到的插件式dhtmlx,xml为

<?xmlversion='1.0'encoding='iso-8859-1'?>

<treeid="0">

<itemtext="MyComputer"id="1"child="1"im0="my_cmp.gif"im1="my_cmp.gif"im2="my_cmp.gif"call="true"select="yes">

<userdataname="system">true</userdata>

<itemtext="Floppy(A:)"id="11"child="0"im0="flop.gif"im1="flop.gif"im2="flop.gif"/>

<itemtext="LocalDisk(C:)"id="12"child="0"im0="drv.gif"im1="drv.gif"im2="drv.gif"/>

</item>

<itemtext="RecycleBin"id="4"child="0"im0="recyc.gif"im1="recyc.gif"im2="recyc.gif"/>

</tree>

controller中代码为:

defKnowledgeTree={

StringBuffersb=newStringBuffer();

response.setCharacterEncoding("UTF-8")

response.setContentType("text/xml");

defloginUser=UtilsController.getLoginUser(session)

if(loginUser){

defc=KnowledgeType.createCriteria()

defmyKnowledge=c.list(){

and{

isNull("parent")

}

order("dateCreated","desc")

}

try{

sb.append(treeXML(myKnowledge))

}catch(Exceptione){

e.printStackTrace()

}

}else{

sb.append(nologinTreeXML())

}

rendersb.toString()

}

defknowledgeType_tree={

}//建页面时建立knowledgeType_tree。从这个页面调用defknowledgeType_listTree这个方法

defknowledgeType_listTree={

StringBuffersb=newStringBuffer();

response.setCharacterEncoding("UTF-8")

response.setContentType("text/xml");

defloginUser=UtilsController.getLoginUser(session)

if(loginUser){

defc=KnowledgeType.createCriteria()

defmyKnowledge=c.list(){

and{

isNull("parent")

}

order("dateCreated","desc")

}

try{

sb.append(treeXML(myKnowledge))

}catch(Exceptione){

e.printStackTrace()

}

}else{

sb.append(nologinTreeXML())

}

rendersb.toString()

}

privateStringtreeXML(defknowledgeType){

response.setCharacterEncoding("UTF-8")

response.setContentType("text/xml");

StringBuffersb=newStringBuffer();

sb.append("<?xmlversion='1.0'encoding='UTF-8'?>")

sb.append('<treeid="0">')

try{

for(KnowledgeTypeud:knowledgeType){

sb.append(makeKnowledgeTreeXML(ud))

}

}catch(Exceptione){

e.printStackTrace()

}

sb.append('</tree>')

returnsb.toString()

}

privateStringnologinTreeXML(){

response.setCharacterEncoding("UTF-8")

response.setContentType("text/xml");

StringBuffersb=newStringBuffer();

sb.append("<?xmlversion='1.0'encoding='UTF-8'?>")

sb.append('<treeid="0">')

sb.append("<itemtext='\u4F60\u767B\u5F55\u5DF2\u7ECF\u8D85\u65F6!'id='no_login'aCol='red'/>")

sb.append('</tree>')

returnsb.toString()

}

privateStringmakeKnowledgeTreeXML(KnowledgeTypedepartment){

defresult=""

if(department.childrens){

result='<itemtext="'+department.sortname+'"id="'+department.id+'"im0="folderClosed.gif">'

defc=KnowledgeType.createCriteria()

defchildren=c.list(){

eq("parent",department)

order("dateCreated","desc")

}

for(KnowledgeTypeud:children){

if(ud!=department){

result+=makeKnowledgeTreeXML(ud);

}

}

result+='</item>'

}else{

if(!department.parent&&!department.childrens){

result='<itemtext="'+department.sortname+'"id="'+department.id+'"im0="folderClosed.gif"></item>'

}else{

result+='<itemtext="'+department.sortname+'"id="'+department.id+'"/>'

}

}

returnresult;

}

}

gsp页面:tree.loadXML("${request.getContextPath()}/knowledgeType/knowledgeType_listTree?"+newDate().valueOf());

相关推荐