通过 PHP 在数据表编辑器中保存创建的行

问题描述 投票:0回答:1

我使用数据表编辑器来显示行

这是我正在使用的代码

var editor; 
$(document).ready( function () {
editor = new $.fn.dataTable.Editor( {
"ajaxUrl": {
                    "create":        "admin/save",
                    
                },
                "domTable": "#example",
                "fields": [ {
                        "label": "username:",
                        "name": "username"
                    }, {
                        "label": "password:",
                        "name": "password",
                        "type":"password"
                    }, {
                        "label": "fname:",
                        "name": "fname"
                    }, {
                        "label": "lname:",
                        "name": "lname"
                    }, {
                        "label": "email:",
                        "name": "email"
                    },{
                        "label": "address:",
                        "name": "address"
                    }
                ]
            } );

            $('#example').dataTable( {
                
                "sDom": "Tfrtip",

                        "aoColumns": [
                        { "mData": "username"},
                        { "mData": "password" },
                        { "mData": "fname" },
                        { "mData": "lname" },
                        { "mData": "email" },
                        { "mData": "address" }
                    ],
                
                "oTableTools": {
                    "sRowSelect": "single",
                    "aButtons": [
                        { "sExtends": "editor_create", "editor": editor },
                        { "sExtends": "editor_edit",   "editor": editor },
                        { "sExtends": "editor_remove", "editor": editor }
                    ]
                }
            } );
        } );

如何将表单数据传递到控制器页面?我还给出了名称字段,但它没有添加到元素中。

create : admin/save

这里 admin 是控制器名称,save 是操作名称。

javascript zend-framework2 datatables jquery-datatables-editor
1个回答
1
投票

使用具有编辑器扩展的数据表,它将数据发送到服务器进行处理。客户端发送三个字段:

action
id
data
。动作可以是
create
edit
delete
。 id只填写
edit

所以简而言之,你可以使用这个控制器:

<?php
namespace MyModule\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\JsonModel;

class DatatablesController extends AbstractActionController
{
    public function saveAction()
    {
        if (!$this->getRequest()->isPost()) {
            $response = $this->getResponse();
            $response->setStatusCode(405); // Method not allowed
            return $response;
        }

        $action = $this->params()->fromPost('action', null);
        $data   = array();
        switch ($action) {
            case 'create':
                $data = $this->createRow();
                break;
            case 'edit':
                $data = $this->editRow();
                break;
            case 'delete':
                $data = $this->deleteRow();
                break;
            default:
                $response = $this->getResponse();
                $response->setStatusCode(422); // Unprocessable entity
                return $response;
        }

        $model = new JsonModel($data);
        return $model;
    }

    protected function createRow()
    {
        $data = $this->params()->fromPost('data', array());

        // Create a new entity with $data

        // Return the properties from the new entity
        return array();
    }

    protected function editRow()
    {
        $id   = $this->params()->fromPost('id');
        $data = $this->params()->fromPost('data', array());

        // Fetch the entity with id $id
        // Update the entity with $data

        // Return the properties from the entity
        return array();
    }

    protected function deleteRow()
    {
        $ids = $this->params()->fromPost('data', array());

        // Remove all entities with an id in the array $ids
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.