grails树形结构图

用到的插件式 dhtmlx,  xml为
<?xml version='1.0' encoding='iso-8859-1'?>
          <tree id="0">
              <item text="My Computer" id="1" child="1" im0="my_cmp.gif" im1="my_cmp.gif" im2="my_cmp.gif" call="true" select="yes">
                  <userdata name="system">true</userdata>
                  <item text="Floppy (A:)" id="11" child="0" im0="flop.gif" im1="flop.gif" im2="flop.gif"/>
                  <item text="Local Disk (C:)" id="12" child="0" im0="drv.gif" im1="drv.gif" im2="drv.gif"/>
              </item>
              <item text="Recycle Bin" id="4" child="0" im0="recyc.gif" im1="recyc.gif" im2="recyc.gif"/>
      </tree>
controller中代码为:
def KnowledgeTree = {
StringBuffer sb = new StringBuffer();
response.setCharacterEncoding("UTF-8")
response.setContentType("text/xml");
def loginUser =UtilsController.getLoginUser(session)
if(loginUser){
def c=KnowledgeType.createCriteria()
def myKnowledge=c.list(){

and{
isNull("parent")

}
order("dateCreated","desc")
}
try{
sb.append(treeXML(myKnowledge))
}catch (Exception e) {
e.printStackTrace()
}
}else{
sb.append(nologinTreeXML ())
}
render sb.toString()
}
def knowledgeType_tree={

}//建页面时建立knowledgeType_tree。从这个页面调用def knowledgeType_listTree这个方法
def knowledgeType_listTree = {

StringBuffer sb = new StringBuffer();
response.setCharacterEncoding("UTF-8")
response.setContentType("text/xml");
def loginUser = UtilsController.getLoginUser(session)
if(loginUser){
def c=KnowledgeType.createCriteria()
def myKnowledge=c.list(){

and{
isNull("parent")

}
order("dateCreated","desc")
}

try{
sb.append(treeXML (myKnowledge))
}catch (Exception e) {
e.printStackTrace()
}
}else{
sb.append(nologinTreeXML ())
}
render sb.toString()
}
private String treeXML (def knowledgeType) {
response.setCharacterEncoding("UTF-8")
response.setContentType("text/xml");
StringBuffer sb = new StringBuffer();
sb.append("<?xml version='1.0' encoding='UTF-8'?>")
sb.append('<tree id="0">')
try{
for(KnowledgeType ud:knowledgeType){
sb.append(makeKnowledgeTreeXML(ud))
}
}catch (Exception e) {
e.printStackTrace()
}
sb.append('</tree>')
return sb.toString()
}
private String nologinTreeXML () {
response.setCharacterEncoding("UTF-8")
response.setContentType("text/xml");
StringBuffer sb = new StringBuffer();
sb.append("<?xml version='1.0' encoding='UTF-8'?>")
sb.append('<tree id="0">')
sb.append("<item text='\u4F60\u767B\u5F55\u5DF2\u7ECF\u8D85\u65F6!' id='no_login' aCol='red' />")
sb.append('</tree>')
return sb.toString()
}
private String makeKnowledgeTreeXML(KnowledgeType department){
def result = ""
if(department.childrens){
result='<item text="' + department.sortname + '" id="' + department.id + '" im0="folderClosed.gif" >'
def c=KnowledgeType.createCriteria()
def children=c.list(){
eq("parent", department)
order("dateCreated","desc")
}
for(KnowledgeType ud:children){
if(ud!=department){
result+=makeKnowledgeTreeXML(ud);
}
}
result+='</item>'
}else{
if(!department.parent&&!department.childrens){
result='<item text="' + department.sortname + '" id="' + department.id + '" im0="folderClosed.gif" ></item>'
}else{

result += '<item text="' + department.sortname  + '" id="' + department.id + '" />'
}
}
return result;
}
}



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

你可能感兴趣的:(grails)