将带有编辑器的jQuery数据表集成到WordPress管理中

时间:2014-11-27 作者:Rizzim

我正在尝试将jQuery数据库集成到我的Wordpress插件中http://editor.datatables.net/

我已经创建了一个数据库表

CREATE TABLE `id` (
`id` int(10) NOT NULL auto_increment,
`first` varchar(255) default NULL,
`last` varchar(255) default NULL,
`age` varchar(255) default NULL,
PRIMARY KEY  (`id`)
);
我已将所需文件排入队列

    $this->enqueueStyle(
        dirname( APFDEMO_FILE ) . \'/datatables/css/demo.css\',
        \'apf_first_page\'    // page slug
    );
    $this->enqueueStyle(
        dirname( APFDEMO_FILE ) . \'/datatables/css/jquery.dataTables.css\',
        \'apf_first_page\'    // page slug
    );
    $this->enqueueStyle(
        dirname( APFDEMO_FILE ) . \'/datatables/css/dataTables.tableTools.css\',
        \'apf_first_page\'    // page slug
    );
    $this->enqueueStyle(
        dirname( APFDEMO_FILE ) . \'/datatables/css/dataTables.editor.css\',
        \'apf_first_page\'    // page slug
    );

    $this->enqueueScript(
         dirname( APFDEMO_FILE ) . \'/datatables/js/jquery.dataTables.min.js\', // source url or path
         \'apf_first_page\',     // page slug
         \'\',     // tab slug
         array(
             \'handle_id\'     => \'my_dataTables_script\', // this handle ID also is used as the object name for the translation array below.
         )
    );

    $this->enqueueScript(
         dirname( APFDEMO_FILE ) . \'/datatables/js/dataTables.tableTools.min.js\', // source url or path
         \'apf_first_page\',     // page slug
         \'\',     // tab slug
         array(
             \'handle_id\'     => \'my_dataTablesTool_script\', // this handle ID also is used as the object name for the translation array below.
         )
    );

    $this->enqueueScript(
         dirname( APFDEMO_FILE ) . \'/datatables/js/dataTables.editor.js\', // source url or path
         \'apf_first_page\',     // page slug
         \'\',     // tab slug
         array(
             \'handle_id\'     => \'my_dataTableseditor_script\', // this handle ID also is used as the object name for the translation array below.
         )
    );
我已经为表声明了datatables和html

    $(document).ready(function() {
        var editor = new $.fn.dataTable.Editor( {
            "ajax": "admin-ajax.php?action=datatables",
            "table": "#id",
            "fields": [
                {
                    "label": "first",
                    "name": "first",
                    "type": "text"
                },
                {
                    "label": "last",
                    "name": "last",
                    "type": "text"
                },
                {
                    "label": "age",
                    "name": "age",
                    "type": "text"
                }
            ]
        } );

        $(\'#id\').dataTable( {
            "dom": "Tfrtip",
            "ajax": "admin-ajax.php?action=datatables",
            "columns": [
                {
                    "data": "first"
                },
                {
                    "data": "last"
                },
                {
                    "data": "age"
                }
            ],
            "tableTools": {
                "sRowSelect": "os",
                "aButtons": [
                    { "sExtends": "editor_create", "editor": editor },
                    { "sExtends": "editor_edit",   "editor": editor },
                    { "sExtends": "editor_remove", "editor": editor }
                ]
            }
        } );
    } );
            <div class="container">
            <h1>DataTables Editor - id</h1>
            <table cellpadding="0" cellspacing="0" border="0" class="display" id="id" width="100%">
                <thead>
                    <tr>
                        <th>first</th>
                        <th>last</th>
                        <th>age</th>
                    </tr>
                </thead>
            </table>
        </div>
声明了ajax函数

add_action( \'wp_ajax_datatables\', \'my_datatables_callback\' );

function my_datatables_callback() {

     include( APFDEMO_DIRNAME . \'/datatables/php/table.id.php\' );

     die();
}
我可以让这些表在插件的页面中正确加载,但当我尝试编辑/创建/更新时,它不会做任何事情。

有没有人有将数据表集成到wordpress插件的经验。

我认为主要问题在于

var editor = new $.fn.dataTable.Editor( {
            "ajax": "admin-ajax.php?action=datatables",
真的很有帮助

谢谢

2 个回复
SO网友:Josh Koberstein

DataTables Editor和WordPress都试图使用相同的保留参数“action”。

WordPress正在使用$\\u REQUEST[\'action\']在admin ajax中路由请求。php第86行。

DataTables Editor正在使用$\\u POST[\'action\']告诉服务器端处理脚本要应用的操作(插入/删除/更新-请参阅:https://editor.datatables.net/manual/server )

如果$\\u REQUEST[\'action\']设置为“datatables”,这将很好地工作。然而,WordPress迫使$\\u请求在wp includes/load的第598行获得+POST。php

function wp_magic_quotes() {
  // If already slashed, strip.
  if ( get_magic_quotes_gpc() ) {
    $_GET    = stripslashes_deep( $_GET    );
    $_POST   = stripslashes_deep( $_POST   );
    $_COOKIE = stripslashes_deep( $_COOKIE );
  }

  // Escape with wpdb.
  $_GET    = add_magic_quotes( $_GET    );
  $_POST   = add_magic_quotes( $_POST   );
  $_COOKIE = add_magic_quotes( $_COOKIE );
  $_SERVER = add_magic_quotes( $_SERVER );

  // Force REQUEST to be GET + POST.
  $_REQUEST = array_merge( $_GET, $_POST );
}
因此,永远不会调用ajax回调函数。

您需要做的是添加以下内容:

add_action(\'admin_init\', \'fix_request_action\');

function fix_request_action() {
  global $pagenow;
  if($pagenow == \'admin-ajax.php\' && isset($_GET[\'action\']) && isset($_POST[\'action\'])) {
    $_REQUEST[\'action\'] = $_GET[\'action\'];
  }
}
这将检测当前页面为admin ajax时同时设置$\\u GET[\'action\']和$\\u POST[\'action\']的情况。php。它将强制$\\u REQUEST[\'action\']为正确的操作(在您的示例中为“datatables”-$u GET操作),以便正确调用回调。

我希望这有帮助。

SO网友:Sarah K

您不需要函数:fix\\u request\\u action

只需像这样设置javascript

$(\'#id\').dataTable({
    "ajax": {
        url: "admin-ajax.php",
        "data": function ( d ) {
            d.action = "json_basic_list";
        }
    }
    });
进行处理并符合wordpress参考https://codex.wordpress.org/AJAX_in_Plugins

结束

相关推荐

来自jQuery AJAX的WP API删除请求

我正在使用rest json API插件http://wp-api.org/ 创建一个小应用程序。我试图用jquery ajax删除一个自定义的帖子条目,但缺少一些东西,下面是我的代码:... //make the request var dataArray = {}; var urlRequest = WP_API_Settings.root+\"/posts/\"+postID; var typeRequest = \'D