Using dynatree.js lazy loading, how to remove null child nodes( every last node in a branch is null)

StackOverflow https://stackoverflow.com/questions/22574746

Question

I am new dynatree usage. Everything is cool and lazyload except showing last child as null for every branch.

$("#tree").dynatree({
    title: "Lazy loading sample",
    fx: { height: "toggle", duration: 200 },
    autoFocus: false,

    initAjax: {
        url: "/get_top_level_nodes",
        data: { mode: "funnyMode" }
    },

    onActivate: function(node) {
        $("#echoActive").text("" + node + " (" + node.getKeyPath()+ ")");
    },

    onLazyRead: function(node){
        node.appendAjax({
            url: "/get_children",
            data: { key: node.data.key,mode: "funnyMode"}
            });
    }
});

If the json result is not null and like below. Its working correctly. Json : [ { "title": "Node1", "isLazy": true, "key": "BC13B21636CD6D5C"}, { "title": "Node1", "isLazy": true, "key": "BC13B21636CD6D5C"}]

If ajax result is an empty [], I am returning as null. Yet I see a child node with null in that branch. how can I remove this? If there are no children, I don't want to display null or anything.

Was it helpful?

Solution 2

If result is [] array instead of [ { "title": "Node1", "is Lazy": true, "key": "BC13B21636CD6D5C"}, { "title": "Node1", "is Lazy": true, "key": "BC13B21636CD6D5C"}],

Return nil if there are no child for a parent. I have format method in controller which I used to format the children array to son format,

OTHER TIPS

If ajax result is an empty [], I am returning as null.

You should return [] instead ;-)

mar10's answer is correct. Here's the code I use for a tree using lazy loading. If there are no children nodes to return, I echo an empty JSON object "[]":

$res = mysql_query($req) or die(mysql_error());
$i=0;
while($cat = mysql_fetch_assoc($res)) {
  $cats[] = $cat;
  $i = 1;
}
if ($i)
  echo json_encode($cats);
else
  echo "[]";
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top