How-to implement admin Ajax inside an admin WP_List_Table?

In the parent __construct args if ajax is set to true the js_vars will be output in the footer of the page.

/**
     * 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' ) );
        }
    }

The default javascript variables that get output in the footer are defined in the js_vars function:

/**
     * 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 ) );
    }

Leave a Comment