FuelUX树:如何使用json源设置dataSource?

时间:2014-04-08 09:14:55

标签: php json fuelux

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

[{“name”:“South Africa”,“type”:“folder”,“additionalParameters”:{“id”:“1”}}]

以下是javascript代码段:

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,
    });
});

如果您知道我遗失或做错了,请提供帮助,因为目前没有任何回复。

1 个答案:

答案 0 :(得分:0)

您的实施不是按原样读取数据 - 如果您希望这样做,您必须改变一些事项:

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"}}] }]