【问题标题】:FuelUX Tree: How do I set dataSource with json source?FuelUX 树:如何使用 json 源设置数据源?
【发布时间】:2014-04-08 09:14:55
【问题描述】:

我正在尝试为 FuelUX 树设置一个 json 数据源。我创建了一个回显 json_encoded 数组的 php 文件,结果如下所示:

[{"name":"南非","type":"folder","additionalParameters":{"id":"1"}}]

下面是javascript sn-p:

jQuery(document).ready(function (e) {
    var DataSourceTree = function (options) {
        this.url = options.url;
    }

    DataSourceTree.prototype.data = function (options, callback) {
        var self = this;
        var $data = null;

        var param = null

        if (!("name" in options) && !("type" in options)) {
            param = 0; //load the first level  
        } else if ("type" in options && options.type == "folder") {
            if ("additionalParameters" in options && "children" in options.additionalParameters) {
                param = options.additionalParameters["id"];
            }
        }

        if (param != null) {
            $.ajax({
                url: this.url,
                data: 'id=' + param,
                type: 'POST',
                dataType: 'json',
                success: function (response) {
                    if (response.status == "OK")
                        callback({
                            data: response.data
                        })
                },
                error: function (response) {
                    console.log(response);
                }
            })
        }
    };

    $('#location-tree').tree({
        dataSource: new DataSourceTree({
            url: '/do_json/get_location_tree'
        }),
        multiSelect: false,
    });
});

如果您知道我遗漏了什么或做错了什么,请提供帮助,因为目前没有任何东西被退回。

【问题讨论】:

    标签: php json fuelux


    【解决方案1】:

    您的实现没有按应有的方式读取数据 - 如果您希望它正常工作,您必须更改一些内容:

    DataSourceTree.prototype.data = function (options, callback) {
        var self = this;
        var $data = null;
    
        var param = null
    
        if (!("name" in options) && !("type" in options)) {
            param = 0; //load the first level  
        } else if ("type" in options && options.type == "folder") {
            if ("additionalParameters" in options && "children" in options.additionalParameters) {
                param = options.additionalParameters["id"];
            }
        }
    
        if (param != null) {
            $.ajax({
                url: this.url,
                data: 'id=' + param,
                type: 'POST',
                dataType: 'json',
                success: function (response) {
                    callback(response)
                },
                error: function (response) {
                    console.log(response);
                }
            })
        }
    };
    

    查看ajax调用成功函数的变化。

    这是因为您当前的实现等待这种格式的数据:

    [{status: "OK", data: [{"name":"South Africa","type":"folder","additionalParameters":{"id":"1"}}] }]
    

    【讨论】:

    • 谢谢,当我 console.log(response) 时,现在得到正确数据的响应,但在回调期间出现错误,阻止树渲染:未捕获的类型错误:无法读取属性“长度” '未定义的
    • 您现在是在使用自己的实现还是改编了我的?数据究竟是什么样的?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-22
    • 2021-02-11
    • 2014-06-04
    • 2014-11-23
    • 1970-01-01
    相关资源
    最近更新 更多