<?php
namespace Hasinur\Library\Admin;
use WP_List_Table;
class BookListTable extends WP_List_Table {
public $books = [];
public function __construct() {
parent::__construct();
$this->set_books();
}
public function prepare_items() {
$per_page = 2;
$current_page = $this->get_pagenum();
$offset = ( $current_page - 1 ) * $per_page;
$total_items = count( $this->books );
$this->set_pagination_args( [
'total_items' => $total_items,
'per_page' => $per_page,
] );
$columns = $this->get_columns();
$this->items = $this->get_items( [
'offset' => $offset,
'limit' => $per_page,
] );
$this->_column_headers = [$columns];
}
public function get_bulk_actions() {
return [
'delete' => __('Delete', 'library'),
];
}
public function get_items( $args ) {
$defaults = [
'offset' => 0,
'limit' => 2,
];
$args = wp_parse_args( $args );
return array_slice( $this->books, $args['offset'], $args['limit'] );
}
public function get_columns() {
return [
'cb' => '<input type="checkbox"/>',
'title' => __( 'Title', 'library' ),
'author' => __( 'Author', 'library' ),
'published' => __( 'Published', 'library' ),
];
}
public function column_default( $item, $column_name ) {
return isset( $item[$column_name] ) ? $item[$column_name] : '';
}
public function column_cb( $item ) {
return '<input type="checkbox" name="books[]" value="' . $item['id'] . '">';
}
public function handle_row_actions( $item, $column_name, $primary ) {
if ( $primary != $column_name ) {
return;
}
$actions['edit'] = sprintf( '<a href="%1$s">%2$s</a>', 'https://google.com', __( 'Edit', 'library' ) );
$actions['delete'] = sprintf( '<a href="%1$s">%2$s</a>', 'https://google.com', __( 'Delete', 'library' ) );
$actions = apply_filters('books_row_actions', $actions);
return $this->row_actions($actions);
}
private function set_books() {
$items = [
[
'id' => 1,
'title' => __( 'History Of Bangladesh', 'library' ),
'author' => __( 'Hasinur Rahman', 'library' ),
'published' => '2021',
],
[
'id' => 2,
'title' => __( 'Three years of Programming', 'library' ),
'author' => __( 'John Doe', 'library' ),
'published' => '1985',
],
[
'id' => 3,
'title' => __( 'Population Problem', 'library' ),
'author' => __( 'Jahangir Alam', 'library' ),
'published' => '1920',
],
[
'id' => 4,
'title' => __( 'Nature of Bangladesh', 'library' ),
'author' => __( 'Anderson Rasel', 'library' ),
'published' => '1935',
],
];
$this->books = $items;
}
}
Created
September 16, 2022 14:23
-
-
Save hasinur1997/d1615e00c5470248bfcb63c59b89dbef to your computer and use it in GitHub Desktop.
WordPress List Table Example
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment