Access all of Typerocket. Get Pro.
Tables
( v5 )
- # Tables
- # Making a Table
- # Custom Model
- # Set Limit
- # Set Order
- # Search
- # Customize Search
- # Only Use Custom Search
- # Set Table Columns
- # Column Settings
- # Set Primary Column
- # Bulk Actions
Tables
UI tables are used to list the records of a Model. Tables can be added to admin pages or anywhere else. When a table is created within the context of a resource admin view all the required settings are detected and injected for you. The only thing you need to do is specify what fields you want the table to list.
Making a Table
If you are on the index page for a resource, the limit and model will be automatically bound for you.
$table = tr_table();
echo $table;
Custom Model
You can set the model manually as well manually for totally custom tables.
$table = tr_tables(new \App\Model\Posts);
echo $table;
Set Limit
You can set the number of the item that should be displayed in a table with the setLimit()
method. The setLimit()
method takes one argument:
-
$limit
- The number of the item to list25
(default).
$table->setLimit(35);
Set Order
You can set the order the items should be displayed in a table with the setOrder()
method. The setOrder()
method takes two arguments:
-
$column
- The table column name. -
$direction
- The list orderASC
(default) orDESC
.
$table->setOrder('id', 'DESC');
Search
You can also limit the fields that can be searched. The setSearchColumns()
method handles this and takes one argument:
-
$columns
- Anarray
of the columns with custom labels to be searchable.
$table->setSearchColumns([
'username' => 'Username',
'email' => 'Email Address',
'full_name' => 'Full Name',
'id' => 'ID'
]);
Or, you can remove the search.
$table->removeSearch();
Customize Search
You can also add your own search filters for a select menu using the method appendSearchFormFilter()
or the WordPress action tr_table_search
hook.
$table->addSearchFormFilter(function() {
echo <<<'EOT'
<select name="my_filter">
<optgroup label="Filter By">
<option value="some_value">Some Value</option>
</optgroup>
</select>
EOT;
});
Then, you will need to update the model query.
$table->addSearchModelFilter(function($args, $model, $table) {
/** @var \TypeRocket\Models\Model $model */
$model->where('field_name', '=', $_GET['my_filter'] ?? null);
return $args;
});
Only Use Custom Search
Remove the default search filters and only use custom search.
$table->setCustomSearchOnly();
Set Table Columns
By default, every column will be listed from a model. To limit the list of columns displayed, use the setColumns()
method. The setColumns()
takes two arguments.
-
$columns
- Anarray
of columns and their settings. -
$primary
(optional) - The primary column name.
The primary column should also be the fist listed column under the $columns
argument.
$table->setColumns([
'username' => [
'label' => 'Username',
'sort' => true,
'actions' => ['edit', 'delete', 'view'],
'delete_ajax' => false, // disable AJAX delete
'callback' => function( $text, $result ) {
return $result ? '<strong>'.$text.'</strong>' : '';
},
'view_url' => function($show_url, $result) {
$id = $result->id;
return "/members/{$id}/";
}
],
'id' => [
'label' => 'ID',
'sort' => true
],
], 'username');
Column Settings
Each column can have a set of settings.
-
Label - The
label
setting sets the table's header for that column. -
Sort - The
sort
setting, if set totrue
will make the column sortable. -
Actions - The
actions
setting takes an array and can contain the valuesview
,edit
, anddelete
. Theview
setting links to the show resource page. -
Disable AJAX Delete - There is also a setting to disable delete actions AJAX call and send the user to the delete page instead. The
delete_ajax
setting when set tofalse
will disable the AJAX call. -
View URL - You can change the view URL with the
view_url
setting. This setting takes a callback with two arguments:$show_url
(the URL used for the view link),$result
(the Model of the item for the table row). -
Callback - You can change the text returned for a column with the
callback
setting. This setting takes a callback with two arguments:$text
(the text used for the column),$result
(the Model of the item for the table row).
Set Primary Column
$column = 'id';
$table->setPrimaryColumn($column);
Bulk Actions
This will also add checkboxes to the table and wrap the table in a custom HTML form that will be used.
$actions = [
'Key' => 'value'
];
$form = tr_form([]);
$table->setBulkActions($form, $actions);
You can also use a callback to add your own custom action HTML.
$form = tr_form([]);
$table->setBulkActions($form, function($obj) {
echo '<strong>Some HTML</strong>';
});
Finally, you can customize the Form
object as needed so that when applying bulk actions you can direct the request as needed. However, be sure the page you submit your action to contains the proper business logic to achieve the effect you desire.
$form = tr_form([])->toAdmin('admin.php', ['page' => 'my_page']);;
$table->setBulkActions($form, [
'Delete' => 'delete'
]);
When the form is submitted you will have access to the request POST data bulk
and tr[tr_bulk_action]
. You can use that data to implement your bulk actions. For example, maybe we delete some records:
$action = tr_request()->fields('tr_bulk_action');
$ids = tr_request()->getDataPost('bulk');
if(!empty($ids) && $action === 'delete') {
// Your delete code here
echo 'Records deleted: ' . implode(', ', $ids);
}
Found a typo? Something is wrong in this documentation? Fork and edit it!