0

I've tried the custom WP_List_Table example from the given example and I've encountered 2 issues:

  1. The re-order doesnt work: I can the see the requests in the URL of 'orderby' & 'order' but the table doesn't change.
  2. I have a creator of a Team in the database as an ID and I'd like to display his name instead, so I tried changing the array to show his name instead of ID using:

    $teams = $wpdb->get_results("SELECT * FROM {$gp_prefix}gp_teams", ARRAY_A); $creator = get_userData($teams['creator_id']); $teams['creator_id'] = $creator->user_login;

The entire Class:

class GP_Admin_List_Table_Teams extends WP_List_Table {

function __construct(){
    global $status, $page;

    //Set parent defaults
    parent::__construct( array(
        'singular'  => 'team',     //singular name of the listed records
        'plural'    => 'teams',    //plural name of the listed records
        'ajax'      => false        //does this table support ajax?
    ) );
}

function column_default($item, $column_name){
    switch($column_name){
        case 'name':
        case 'slug':
        case 'status':
        case 'members':
        case 'creator_id':
        case 'date_created':
            return $item[$column_name];
        default:
            return print_r($item,true); //Show the whole array for troubleshooting purposes
    }
}

function column_title($item){

    //Build row actions
    $actions = array(
        'edit'      => sprintf('<a href="?page=%s&action=%s&movie=%s">Edit</a>',$_REQUEST['page'],'edit',$item['id']),
        'delete'    => sprintf('<a href="?page=%s&action=%s&movie=%s">Delete</a>',$_REQUEST['page'],'delete',$item['id']),
    );

    //Return the title contents
    return sprintf('%1$s <span style="color:silver">(id:%2$s)</span>%3$s',
        /*$1%s*/ $item['name'],
        /*$2%s*/ $item['id'],
        /*$3%s*/ $this->row_actions($actions)
    );
}

function column_cb($item){
    return sprintf(
        '<input type="checkbox" name="%1$s[]" value="%2$s" />',
        /*$1%s*/ $this->_args['singular'],  //Let's simply repurpose the table's singular label ("movie")
        /*$2%s*/ $item['id']                //The value of the checkbox should be the record's id
    );
}

function get_columns(){
    $columns = array(
        'cb'            => '<input type="checkbox" />', //Render a checkbox instead of text
        'name'          => 'Name',
        'slug'          => 'Slug',
        'status'        => 'Status',
        'members'       => 'Members',
        'creator_id'    => 'Created by',
        'date_created'  => 'Date Created'

    );
    return $columns;
}

function get_sortable_columns() {
    $sortable_columns = array(
        'name'            => array('name', false),     //true means it's already sorted
        'status'        => array('status', false),
        'members'       => array('members', false),
        'creator_id'    => array('creator_id', false),
        'date_created'  => array('date_created', false)
    );
    return $sortable_columns;
}

function get_bulk_actions() {
    $actions = array(
        'delete'    => 'Delete'
    );
    return $actions;
}

function process_bulk_action() {

    //Detect when a bulk action is being triggered...
    if( 'delete'===$this->current_action() ) {
        wp_die('Items deleted (or they would be if we had items to delete)!');
    }

}

function prepare_items() {
    global $wpdb; //This is used only if making any database queries

    $per_page = 5;

    $columns = $this->get_columns();
    $hidden = array();
    $sortable = $this->get_sortable_columns();

    $this->_column_headers = array($columns, $hidden, $sortable);

    $this->process_bulk_action();

    $gp_prefix = gp_core_get_table_prefix();

    $teams = $wpdb->get_results("SELECT * FROM {$gp_prefix}gp_teams", ARRAY_A);
    $current_page = $this->get_pagenum();

    $creator = get_userData($teams['creator_id']);
    $teams['creator_id'] = $creator->user_login;

    function usort_reorder($a,$b){
        $orderby = (!empty($_REQUEST['orderby'])) ? $_REQUEST['orderby'] : 'title';
        $order = (!empty($_REQUEST['order'])) ? $_REQUEST['order'] : 'asc'; 
        $result = strcmp($a[$orderby], $b[$orderby]); 
        return ($order==='asc') ? $result : -$result; 
    }

    $total_items = count($teams);

    $teams = array_slice($teams,(($current_page-1)*$per_page),$per_page);

    $this->items = $teams;

    $this->set_pagination_args( array(
        'total_items' => $total_items,                  //WE have to calculate the total number of items
        'per_page'    => $per_page,                     //WE have to determine how many items to show on a page
        'total_pages' => ceil($total_items/$per_page)   //WE have to calculate the total number of pages
    ) );
}
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.