0

我将http://js-grid.com用于我的 php 应用程序。我选择这个库进行内联编辑/更新/添加/删除。现在我想查看来自数据库的数据$variables$array这是脚本

<script>
    $(function() {
        $("#jsGrid").jsGrid({
            pageButtonCount: 5,
            deleteConfirm: "Do you really want to delete the client?",
            controller: db,
            fields: [
                { name: "Name", type: "text", width: 150 },
                { name: "Age", type: "number", width: 50 },
                { type: "control" }
            ]
        });

    });
</script>

在上面的代码中,我得到了一个名为controllerrendering的属性db (data)db来自一个文件db.js。该文件如下所示:

(function() {
var db = {
    loadData: function(filter) {
        return $.grep(this.clients, function(client) {
            return (!filter.Name || client.Name.indexOf(filter.Name) > -1)
                && (!filter.Age || client.Age === filter.Age)
                && (!filter.Address || client.Address.indexOf(filter.Address) > -1)
                && (!filter.Country || client.Country === filter.Country)
                && (filter.Married === undefined || client.Married === filter.Married);
        });
    },
    insertItem: function(insertingClient) {
        this.clients.push(insertingClient);
    },
    updateItem: function(updatingClient) { },
    deleteItem: function(deletingClient) {
        var clientIndex = $.inArray(deletingClient, this.clients);
        this.clients.splice(clientIndex, 1);
    }
};

window.db = db;

db.countries = [
    { Name: "", Id: 0 },
    { Name: "United States", Id: 1 },
];

db.clients = [
    {
        "Name": "Otto Clay",
        "Age": 61,
    },
    {
        "Name": "Connor Johnston",
        "Age": 73,
    }
];

}()); 

我也关注了 github 文档https://github.com/tabalinas/jsgrid-php。但我不知道如何将我php $variable or $array的观点以及javaScripts.

我想要什么: 我想调用$arrayjavaScripts 部分作为controller : db.

错误: 当我使用controller: <?php echo $array; ?>', its returning null cause I can not call as they called as default fromdb.js 时

请帮助我,我该如何打电话php $variable or $array而不是controller: db提前javaScript 致谢。

4

1 回答 1

0

controller选项定义行为,您不能将静态数据放在那里。但是有一个选项数据,您可以在其中放置静态值(例如在您的 php 示例中)。

您的另一个选择是定义controller.loadData

controller: {
    loadData: function() {
        return $.ajax({
            type: "GET",
            url: "/clients/"
        });
    },

并在服务器上提供所请求的信息

switch($_SERVER["REQUEST_METHOD"]) {
    case "GET":
        $result = $clients->getAll();
        break;
    ...
}

header("Content-Type: application/json");
echo json_encode($result);

您可以在jsgrid-php 存储库中找到实现。

于 2016-04-07T07:42:58.000 回答