【问题标题】:Save created row in datatable editor via php通过 php 在数据表编辑器中保存创建的行
【发布时间】:2013-08-27 10:48:32
【问题描述】:

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

这是我正在使用的代码

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

如何将表单数据传递到控制器页面。我也给出了名称字段,但它没有添加到元素中。 创建:管理员/保存 这里 admin 是控制器名称,save 是动作名称。

请帮帮我。

【问题讨论】:

    标签: javascript zend-framework2 jquery-datatables jquery-datatables-editor


    【解决方案1】:

    使用带有编辑器扩展的数据表,它将数据发送到服务器以进行处理。客户端发送三个字段:actioniddata。操作可以是createeditdelete。 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
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-14
      • 1970-01-01
      • 2015-03-22
      相关资源
      最近更新 更多