1

There is an example on the website on how to construct child nodes from custom data:

$("#tree").dynatree({
    […]
    onLazyRead: function(node){
        $.ajax({
            url: […],
            success: function(data, textStatus){
                // In this sample we assume that the server returns JSON like
                // { "status": "...", "result": [ {...}, {...}, ...]}
                if(data.status == "ok"){
                    // Convert the response to a native Dynatree JavaScipt object.
                    var list = data.result;
                    res = [];
                    for(var i=0, l=list.length; i<l; i++){
                        var e = list[i];
                        res.push({title: "" + i + ": " + e.fcurr + "-" + e.tcurr + ":" + e.ukurs,
                            icon: false});
                    }
                    // PWS status OK
                    node.setLazyNodeStatus(DTNodeStatus_Ok);
                    node.addChild(res);
                }else{
                    // Server returned an error condition: set node status accordingly
                    node.setLazyNodeStatus(DTNodeStatus_Error, {
                        tooltip: data.faultDetails,
                        info: data.faultString
                    });
                }
            }
        });
    […]
});

But there is no mention on how to do this for the initialization of the tree. I tried the following:

        initAjax: {
                type: "POST",
                url: "/doSomething",
                data: ...
                contentType: "application/json; charset=utf-8"
                success: function(data, textStatus){
                // In this sample we assume that the server returns JSON like
                // { "status": "...", "result": [ {...}, {...}, ...]}
                if(data.status == "ok"){
                    // Convert the response to a native Dynatree JavaScipt object.
                    var list = data.result;
                    res = [];
                    for(var i=0, l=list.length; i<l; i++){
                        var e = list[i];
                        res.push({title: "" + i + ": " + e.fcurr + "-" + e.tcurr + ":" + e.ukurs,
                            icon: false});
                    }
                    // PWS status OK
                    node.setLazyNodeStatus(DTNodeStatus_Ok);
                    node.addChild(res);
                }else{
                    // Server returned an error condition: set node status accordingly
                    node.setLazyNodeStatus(DTNodeStatus_Error, {
                        tooltip: data.faultDetails,
                        info: data.faultString
                    });
                }
            }
        },

But then I get an error saying success doesn't work and to use some other method, but there is no documentation on how to use the other method? Can anyone help me out here? I tried using dataFilter to filter out my json string that is being returned, but that didn't work; I tried to use onPostInit and postProcess but don't know exactly what to do since there is no documentation: Do I return the data string after its been reformated, do I return the json version of the data? do I just do data = format(data)?

Any help would be greatly appreciated.

I will be having a lot of status codes and need to do different things based on the code; such as if I have code 1 it means I need to change the class of the node to have red text; or if I return code 2 it means there was an internal error and I need to add that to the node text; etc.

Robert Cardona
  • 1,068
  • 5
  • 18
  • 31

0 Answers0