2015-05-26 71 views
0

我試圖製作一個簡單的LDAP客戶端來從LDAP服務器中檢索數據。我從JSP返回一組JSON對象。在點擊任何值時,我會從在線服務器獲取一些數據。我能夠將第一組數組加載到樹中。下一步得到的數組不會附加到JSTree。我的代碼:無法將Json數組從JSP動態加載到Jstree

function getGroupsStructure(id) { 
console.log("in getGroupsStructure-->"); 
var paramId = ""; 

if(id == '') { 
    console.log("in if-->"); 
    paramId = "c=de"; 
} else { 
    console.log("in else-->"); 
    paramId = id; 

} 

    var params = { 
    "DN" : paramId, 

}; 
    console.log("params-->",params); 
    var getGroupsStructureForUserService = service(webURL + "sendingValues/getGroupsStructureForUser",params,"POST"); 
    getGroupsStructureForUserService.success(function(data) { 
     console.log("in success-->dta-->",data); 
     if(data.errorCode == '0') { 
      console.log("in error code 0-->dta-->",data.treeData); 
      $('.treeNode').jstree({ 
       'core': { 
        'data': function (obj, cb) { 
         cb.call(this, 
           data.treeData); 
       } 
       } 
      }); 
      console.log("Tree Created..."); 
     } else { 
      console.log("error code not 0--data-->",data); 
     } 

     $(document).off('click').on('click', '.treeNode a', function() { 
      console.log("on click of a-->"); 
      var id = $(this).parent().attr('id'); 

      console.log("id-->",id); 

      getGroupsStructure(id); 
      console.log("after getGroupsStructure"); 
     }); 
    }); 
    getGroupsStructureForUserService.error(function(data) { 
     console.log(" empty error"); 
    // console.log(err); 
    }); 
} 

的JSP代碼

def NextLevelLDAP(String DN) { 
     // println "Next Level===>" 
      assert ldap!=null 
      def responseArray=[] 
      def results=ldap.search('objectClass=*',DN,SearchScope.ONE)   //Will be triggered when + is pressed in GUI to get next level of tree 
     // assert results==null 
      if(DN.startsWith("c=")) 
       { 
        JSONObject responseJson1=new JSONObject() 
        responseJson1.put("id", initialDN) 
        responseJson1.put("parent", "#") 
        responseJson1.put("text","Parent") 
        responseArray.add(responseJson1) 
        for(entry in results) { 
        // println entry 
        // println "In NextLevel Using InitialDN" 
         JSONObject responseJson=new JSONObject() 
         responseJson.put("id", entry.dn) 
         responseJson.put("parent", DN) 
         String tempResDN=entry.dn.toString() 
         def tempLength=tempResDN.length() - DN.length() 
     //    println tempResDN 
         String tempName=tempResDN.substring(2,tempLength-1) 
    //     println tempName 
         responseJson.put("text",tempName) 
         responseArray.add(responseJson) 
     //    println entry 
         println responseJson.toString() 
        } 
        return responseArray 
       } 
      if(results.size!=0) 
      { 
       for(entry in results) { 
        println entry 

        JSONObject responseJson=new JSONObject() 
        responseJson.put("id", entry.dn) 
        responseJson.put("parent", DN) 
        String tempResDN=entry.dn.toString() 
        def tempLength=tempResDN.length() - DN.length() 
    //    println tempResDN 
        String tempName=tempResDN.substring(2,tempLength-1) 
        println tempName 
        responseJson.put("text",tempName) 
        responseArray.add(responseJson) 
    //    println entry 

       } 
       return responseArray 
      } 
      } 

請忽略獲取父ID的方式。它的一些共同點。 請幫我看看如何獲​​得動態創建的樹節點。我只是獲得了樹的第一級。點擊其他級別的數據將顯示在控制檯中,但未附加到樹上。

謝謝。

回答

0

你有它周圍的其他方法 - 你需要創建樹,並使其請求你,所以不是這樣的:

'data': function (obj, cb) { 
    cb.call(this, data.treeData); 
} 

使用這樣的事情:

'data': function (obj, cb) { 
    // you probably need to pass the obj.id as a parameter to the service 
    // keep in mind if obj.id is "#" you need to return the root nodes 
    service(...).success(function (data) { 
     cb.call(this, data.treeData); 
    }); 
} 

這種方式你不需要每次都分離和重新附加點擊處理程序,它可以在開箱節點的情況下工作。如果你想開上點擊一個節點,你可以使用這個:

$('#tree').on('select_node.jstree', function (e, data) { 
    data.instance.open_node(data.node); 
}); 

所以,你的整個代碼應該是這個樣子:

function load(id) { 
    var params = { 
     "DN" : id && id !== '#' ? id : "c=de" 
    }; 
    return service(webURL + "sendingValues/getGroupsStructureForUser", params, "POST"); 
} 

$('#tree') 
    .jstree({ 
     'core' : { 
      'data': function (obj, cb) { 
       load(obj.id).success(function (data) { 
        cb.(data.treeData); 
       }); 
      } 
     } 
    }) 
    .on('select_node.jstree', function (e, data) { 
     data.instance.open_node(data.node); 
    }); 

只要確保你標記的節點你的回報具有兒童(將他們的children財產設置爲布爾值true)。

+0

得到了代碼工作。非常感謝。 –

+0

如果擴展樹節點沒有任何孩子,那麼它不應該自行終止?它實際上給出了一個錯誤。如果一個空的Json被髮送回樹,節點會自動終止,那麼有沒有辦法?即一個組沒有一個子節點。 –