加载/保存多行JSGrid

时间:2017-01-25 06:28:36

标签: mysql cakephp cakephp-3.0 cakephp-3.x jsgrid

我想使用JSGrid作为从MySQL数据库加载数据的方法,以及通过CakePHP 3保存(新的或编辑过的)多行数据。但是,我不确定如何做那,或者是否可能。

对于加载,如果我想编辑数据,我想从两个连接的表中获取数据。在CakePHP 3中,我使用find检索数据,然后将其设置为视图,但我不确定如何在加载视图时从那里将其放入JSGrid。

为了保存类似,我会保存到两个不同的表。在CakePHP 3中,通常用于多个表,我使用补丁实体保存到各个表,而AJAX将事物保存在另一个页面上而不实际存在于该页面上(每个表的两个添加页面) 。但我不确定你是否可以用多个数据做到这一点。

例如

$.post(".../users/add.json"

给定用户的字段,将执行添加页面通常会执行的操作,并且具有关联表的补丁实体将保存到各种关联表,但一次只能保存一个。

更新:

我的配置/ routes.php:

Router::scope('/', function (RouteBuilder $routes) {
    $routes->extensions(['json', 'xml']);
    $routes->resources('Instrument');
}

我的观点:

<div id="jsGrid"></div>
    <script>
        var resources = [];

        var instruments = [
            {Name: "", Id: 0},
            {Name: "Clarinet", Id: 1},
            {Name: "Guitar", Id: 2},
            {Name: "Piano", Id: 3},
            {Name: "Violin", Id: 4},
        ];

        $("#jsGrid").jsGrid({
            controller: {
                loadData: function (filter) {
                    return $.ajax({
                        type: "GET",
                        url: "<?= $basepath ?>/instrument/view/",
                        data: filter
                    });
                },
                insertItem: function (item) {
                    return $.ajax({
                        type: "POST",
                        url: "<?= $basepath ?>/instrument/add",
                        data: item
                    });
                },
                updateItem: function (item) {
                    return $.ajax({
                        type: "PUT",
                        url: "<?= $basepath ?>/instrument/edit",
                        data: item
                    });
                },
                deleteItem: function (item) {
                    return $.ajax({
                        type: "DELETE",
                        url: "<?= $basepath ?>/instrument/delete",
                        data: item
                    });
                }
            },

            width: "100%",
            height: "400px",

            inserting: true,
            editing: true,
            sorting: true,
            paging: true,

            data: resources,

            fields: [
                        {
                             name: "Instrument",
                             type: "select",
                             items: instruments,
                             valueField: "Id",
                             textField: "Name",
                             validate: "required",
                             width: 175
                        },
                        {name: "Comment", type: "text", width: 150},
                        {type: "control"}
                    ]
        });
    </script>

<?= $basepath ?>是我的AppController中设置的变量,这意味着无论项目名称如何,我仍然可以访问URL。

在视图中,当我尝试保存新行时,会添加该行,但内容为空。当我尝试编辑一行时,我得到一个&#34;请等待&#34;,然后什么都没有保存,我仍然可以编辑这些字段。此外,在控制台中,我收到以下错误:

http://localhost/<?= $basepath ?>/instrument/edit Failed to load resource: net::ERR_CONNECTION_RESET

0 个答案:

没有答案