如何在管理员WP_LIST_TABLE中实现管理员AJAX?

时间:2012-02-06 作者:Babo

我是WordPress的新手,如果有人能帮助我,我将非常感激。

在文档中有参数ajax“true”。没有说过如何使用WP\\u List\\u Table类实现Ajax吗?

parent::__construct( array(
            \'singular\'  => \'user\',     //singular name of the listed records
            \'plural\'    => \'users\',    //plural name of the listed records
            \'ajax\'      => true        //does this table support ajax?
        ) );

2 个回复
SO网友:Chris_O

在父构造参数中,如果ajax设置为true,js\\u vars将输出到页面的页脚。

/**
     * Constructor. The child class should call this constructor from it\'s own constructor
     *
     * @param array $args An associative array with information about the current table
     * @access protected
     */
    function __construct( $args = array() ) {
        $args = wp_parse_args( $args, array(
            \'plural\' => \'\',
            \'singular\' => \'\',
            \'ajax\' => false
        ) );

        $screen = get_current_screen();

        add_filter( "manage_{$screen->id}_columns", array( &$this, \'get_columns\' ), 0 );

        if ( !$args[\'plural\'] )
            $args[\'plural\'] = $screen->base;

        $args[\'plural\'] = sanitize_key( $args[\'plural\'] );
        $args[\'singular\'] = sanitize_key( $args[\'singular\'] );

        $this->_args = $args;

        if ( $args[\'ajax\'] ) {
            // wp_enqueue_script( \'list-table\' );
            add_action( \'admin_footer\', array( &$this, \'_js_vars\' ) );
        }
    }
在页脚中获得输出的默认javascript变量在js\\u vars函数中定义:

/**
     * Send required variables to JavaScript land
     *
     * @access private
     */
    function _js_vars() {
        $current_screen = get_current_screen();

        $args = array(
            \'class\'  => get_class( $this ),
            \'screen\' => array(
                \'id\'   => $current_screen->id,
                \'base\' => $current_screen->base,
            )
        );

        printf( "<script type=\'text/javascript\'>list_args = %s;</script>\\n", json_encode( $args ) );
    }

SO网友:kaiser

你已经服用了吗a look on the WP_List_Table class Methods? 有ajax_response()...

  • ajax_response() - 使用它来实现自定义权限规则集

    public function ajax_response()
    {
        return current_user_can( \'manage_options\' );
    }
    
  • ajax_response() - 处理ajax回调/操作
  • _js_vars() - 要在自定义ajax回调中使用的每个var。工程类似于wp_localize_script().

结束