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

Save created row in datatable editor via php

本文关键字:存在 数据表 编辑器 保存 php 创建 通过      更新时间:2023-09-26

I使用数据表编辑器显示行

这是我正在使用的代码

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

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

请帮帮我。

使用带有Editor扩展的Datatables,它将数据发送到要处理的服务器。客户端发送三个字段: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
    }
}