Skip to main content
Code Tutorial

Adding Bulk Actions To Custom TypeRocket Resource Tables

In the previous tutorial, we covered creating a custom flight resource table. Let's extend the functionality of that flight table and add bulk actions. In this tutorial, we will add bulk deleting.

Add Bulk Actions

To add bulk actions, first, locate the flights/index.php view file and open it. Then, add the following to the table object.

$table = tr_table();

// Add Bulk Actions
$table->setBulkActions(tr_form()->useConfirm(), [
    'Delete' => 'delete'
]);

$table->setColumns([
    // ...
], 'customer.display_name');

$table->render();

Once added, you will see checkboxes on the listed records, and a bulk actions select list at the bottom of the table with an "Apply" button.

Add Bulk To The Flight Controller

Now, add a bulk() method to the \App\Controllers\FlightController.

public function bulk(Request $request)
{
        $action = $request->fields('tr_bulk_action');
        $ids = $request->getDataPost('bulk');

        $redirect = tr_redirect()->toPage('flight', 'index');

        if(!empty($ids) && $action === 'delete') {
            Flight::new()->delete($ids);
            return $redirect->withFlash('Records deleted: ' . implode(', ', $ids));
        }

        return $redirect->withFlash('No bulk action selected.', 'error');
}

Map PUT Requests In Admin Page

Next, we need to map the index route's PUT request to the bulk method on the controller.

tr_resource_pages('Flight@\App\Controllers\FlightController', 'Flights')
            ->setIcon('dashicons-airplane')
            ->mapAction('PUT', 'bulk');

And, that's all there is to do.

Access More TypeRocket

Join our community mailing list and get notified before anyone else. Get videos and more.

By using our website you agree to our Cookie policy.