WP Tables for WP Bones
[](https://packagist.org/packages/wpbones/wptables)
[](https://packagist.org/packages/wpbones/wptables)
[](https://packagist.org/packages/wpbones/wptables)
[](https://packagist.org/packages/wpbones/wptables)
[](https://packagist.org/packages/wpbones/wptables)
, (*1)
, (*2)
A fluent implementation of WordPress WP List Table for WP Bones, (*3)
Requirements
This package works with a WordPress plugin written with WP Bones framework library., (*4)
Installation
You can install third party packages by using:, (*5)
```sh copy
php bones require wpbones/wptables, (*6)
I advise to use this command instead of `composer require` because doing this an automatic renaming will done.
You can use composer to install this package:
```sh copy
composer require wpbones/wptables
You may also to add "wpbones/wptables": "^1.0"
in the composer.json
file of your plugin:, (*7)
```json copy filename="composer.json" {4}
"require": {
"php": ">=7.4",
"wpbones/wpbones": "~0.8",
"wpbones/wptables": "~1.0"
},, (*8)
and run
```sh copy
composer install
How to
You can use WP Tables either like subclass or like fluent class instance., (*9)
Subclassing
As subclass class instance you may create a your own class as show below:, (*10)
```php copy
<?php, (*11)
namespace WPKirk\Http\Controllers;, (*12)
use WPKirk\WPTables\Html\WPTable;, (*13)
class ExampleTable extends WPTable
{, (*14)
protected $name = 'Discos';, (*15)
public function getColumnsAttribute()
{
return [
'id' => 'Name',
'description' => 'Description',
];
}, (*16)
public function getItems( $args = [] )
{, (*17)
$fake = [];
for( $i = 0; $i < 20; $i++ ) {
$fake[] = [
'id' => "Example {$i}",
'description' => 'Some description...'
];
}
return $fake;
}
}, (*18)
In your view controller you have to use the `load` method in order to register the screen options:
```php copy
...
public function load()
{
ExampleTable::registerScreenOption();
}
public function index()
{
$table = new ExampleTable();
return WPKirk()
->view( 'dashboard.table' )
->with( 'table', $table );
}
...
In your ExampleTable
you may override:, (*19)
public function getCheckBoxValueAttribute( $item )
{
return $item[ 'my_colum' ];
}
// or
public function getCheckBoxColumnNameAttribute()
{
return 'my_colum';
}
This will be the value used in the checkbox value., (*20)
Fluent
If you like use the WPTable
as fluent instance, you have to set the columns twice., (*21)
```php copy
...
public function loadFluentExample()
{
WPTable::name( 'Books' )
->columns(
[
'id' => 'Name',
'description' => 'Description',
]
)
->screenOptionLabel( 'Rows' )
->registerScreenOption();
}, (*22)
public function indexFluentExample()
{, (*23)
$items = [];
for ( $i = 0; $i < 20; $i++ ) {
$items[] = [
'id' => "Book {$i}",
'description' => 'Some description...',
];
}
$table = WPTable::name( 'Books' )
->singular( 'Book' )
->plural( 'Books' )
->columns(
[
'id' => 'Name',
'description' => 'Description',
]
)
->setItems( $items );
return WPKirk()
->view( 'dashboard.table' )
->with( 'table', $table );
}, (*24)
```, (*25)