Repository navigation
Ajax ListTable
dualjack edited this page Sep 6, 2017
·
12 revisions
Creating native admin tables was my pain in the ass.
I decided to normalize this step, by extending base WP_List_Table class and wrapping it
with well-documented framework.
What you need
- ShellPress's namespace autoloading.
- Knowledge about filters and actions.
Why you should use my "List Table" framework
- It's made with ajax. It means yoour implementation will not collide with table's requests.
- You can have many tables in one admin page!
- It just works.
- It has filters for everything, which is perfect for creating extensible plugins.
- You have to enqueue script placed in
ShellPress/assets/js/AjaxListTable.js - Create new class which extends
shellpress\vX_X_X\src\Shared\AjaxListTable - Declare all filters and settings in method
protected function setUp(){ ... } - Create your new object and pass it's slug into constructor like this:
$listTable = new ListTable_SubscribersAjax( 'my_fancy_table );- Echo display root where you want it to appear.
echo $listTable->getDisplayRoot();All filter tags are built with table's slug ( defined in class constructor ).
If you declare your filters inside class, you can easly get it with method getSlug().
$headers is an array of arrays with key names.
Column header arguments:
| Name of argument | Type | Description |
|---|---|---|
| isSortable | boolean | Enable sorting of column? |
| realColumnName | string | You can use it in further query. |
| title | string | Title of column. |
| isHidden | boolean | Hide column? |
Example of filter method:
/**
* Define columns headers.
*
* @param array $headers
*
* @return array
*/
public function defineHeaders( $headers ) {
$headers = array(
'cb' => array(
),
'email' => array(
'isSortable' => true,
'realColumnName' => 'title',
'title' => "E-mail"
),
'id' => array(
'isHidden' => true,
'isSortable' => true,
'realColumnName' => 'term_id'
),
'lists' => array(
'title' => __( "Lists", 'mailboo' )
),
'status' => array(
'title' => __( "Status", 'mailboo' )
),
'createdOn' => array(
'isSortable' => true,
'realColumnName' => 'date',
'title' => __( "Created on", 'mailboo' ),
),
'lastOpen' => array(
'title' => __( "Last open", 'mailboo' )
),
'statistics' => array(
'title' => __( "Statistics", 'mailboo' )
)
);
return $headers;
}$views is an array of keyed strings which represent clickable labels.
Example of filter method:
/**
* Define list of views.
*
* @param array $views
*
* @return array
*/
public function defineViews( $views ) {
// Counting shit
$countObj = wp_count_posts( 'subscriber' );
$countAll = (int) array_sum( array(
$countObj->publish,
$countObj->future,
$countObj->draft,
$countObj->private,
$countObj->pending,
$countObj->trash,
) );
$countSubscribed = (int) $countObj->publish;
$countUnsubscribed = (int) $countObj->private;
$countTrash = (int) $countObj->trash;
// Defining views
$views['default'] = sprintf( '%1$s <span class="count">(%2$s)</span>', __( 'All' ), $countAll );
$views['subscribed'] = sprintf( '%1$s <span class="count">(%2$s)</span>', __( 'Subscribed' ), $countSubscribed );
$views['unsubscribed'] = sprintf( '%1$s <span class="count">(%2$s)</span>', __( 'Unsubscribed' ), $countUnsubscribed );
$views['trash'] = sprintf( '%1$s <span class="count">(%2$s)</span>', __( 'Trash' ), $countTrash );
return $views;
}
