wpbones/ wptables
WordPress WP List Table 对 WP Bones 的流畅实现
1.0.5
2024-09-19 17:47 UTC
README
WordPress WP List Table 对 WP Bones 的流畅实现
要求
此软件包与使用 WP Bones 框架库 编写的 WordPress 插件兼容。
安装
您可以使用以下命令安装第三方软件包
php bones require wpbones/wptables
我建议使用此命令而不是 composer require
,因为这样做会自动重命名。
您可以使用 composer 安装此软件包
composer require wpbones/wptables
您还可以将 "wpbones/wptables": "^1.0"
添加到您的插件 composer.json
文件中
"require": { "php": ">=7.4", "wpbones/wpbones": "~0.8", "wpbones/wptables": "~1.0" },
然后运行
composer install
如何使用
您可以使用 WP Tables 作为子类或者作为流畅类实例。
子类化
作为子类实例,您可能需要创建自己的类,如下所示
<?php namespace WPKirk\Http\Controllers; use WPKirk\WPTables\Html\WPTable; class ExampleTable extends WPTable { protected $name = 'Discos'; public function getColumnsAttribute() { return [ 'id' => 'Name', 'description' => 'Description', ]; } public function getItems( $args = [] ) { $fake = []; for( $i = 0; $i < 20; $i++ ) { $fake[] = [ 'id' => "Example {$i}", 'description' => 'Some description...' ]; } return $fake; } }
在您的视图控制器中,您必须使用 load
方法来注册屏幕选项
... public function load() { ExampleTable::registerScreenOption(); } public function index() { $table = new ExampleTable(); return WPKirk() ->view( 'dashboard.table' ) ->with( 'table', $table ); } ...
在您的 ExampleTable
中,您可以重写
public function getCheckBoxValueAttribute( $item ) { return $item[ 'my_colum' ]; } // or public function getCheckBoxColumnNameAttribute() { return 'my_colum'; }
这将是复选框值使用的值。
流畅
如果您想将 WPTable
作为流畅实例使用,您必须设置列两次。
... public function loadFluentExample() { WPTable::name( 'Books' ) ->columns( [ 'id' => 'Name', 'description' => 'Description', ] ) ->screenOptionLabel( 'Rows' ) ->registerScreenOption(); } public function indexFluentExample() { $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 ); }