【问题标题】:Fancytree load data from specific json elementFancytree 从特定的 json 元素加载数据
【发布时间】:2014-07-15 21:18:39
【问题描述】:

我正在使用Laravel 4fancytree

我有一个 api 调用返回 JSON 数据,如下所示:

{
    "error":false,
    "survey_data":"[{
        "id": 1,
        "type": "group",
        "subtype": null,
        "title": "General ",
        "parent": null,
        "children": [{
            "id": 30,
            "type": "question",
            "subtype": "boolean",
            "title": "Did you..?",
            "parent": 1,
            "children": [{
                "id": 31,
                "type": "answer",
                "subtype": null,
                "title": "Yes",
                "parent": 30,
                "children": []
            }]
        }]
    }]
}

我想将元素survey_dataJSON 加载到fancytree 中。

目前,我的代码是这样的:

var tree = function (treeDiv,sourceURL){
     treeDiv.fancytree({
         source : sourceURL
     })
}

在变量sourceURL 中,我传递了api url,并且我用firebug 验证了我得到了正确的JSON 数据。但是我不确定如何将这个survey_dataJSON 作为源加载。

我也试过了:

source: jQuery.ajax({
            url: sourceURL,
            type: 'GET',
            dataType: 'json',
            complete: function(xhr, textStatus) {
            },
            success: function(data, textStatus, xhr) {
                return data.survey_data;
            },
            error: function(xhr, textStatus, errorThrown) {
            }
        })

但出现错误:

Uncaught Error: Assertion failed: source must contain (or be) an array of children 

请注意,我使用的是最新版本的fancytree

谁能帮帮我?

谢谢。

【问题讨论】:

    标签: php ajax laravel-4 fancytree


    【解决方案1】:

    自己解决了:

    treeDiv.fancytree({
        source: {
          url: sourceURL,
        },
    
        postProcess: function(event, data) {
          data.result = $.parseJSON(data.response.survey_data);
        }
    }]
    

    【讨论】:

    • 知道如何为延迟加载执行此操作吗?延迟加载时,不会调用 postProcess 函数。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多