如何在Javascript函数中添加php变量/数组(在jsGrid({controller})中)

how to add php variable(s)/arrays in Javascript function ( in jsGrid({ controller }) )

本文关键字:jsGrid controller 数组 变量 Javascript 函数 添加 php      更新时间:2024-03-05

我正在使用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>

在上面的代码中,我得到了一个名为controller的属性,它正在渲染db (data)。CCD_ 5来自文件CCD_。文件如下:

(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 $arrayjavaScripts放在我的视图中。

我想要什么:我想将$array作为controller : db调用到javaScripts的部分中。

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

请帮我解决如何在javaScript中呼叫php $variable or $array而不是controller: db提前感谢。

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

您可以在jsgridphp repo中找到实现。