Enhancing Filament Tables with Manual Filtering, Searching, Sorting, and Pagination

Introduction

When working with Filament, especially when displaying data from sources other than Eloquent models, you might encounter situations where the built-in features for filtering, searching, sorting, and pagination don't work out of the box. This post describes how to manually implement these features for array-based report tables within Filament admin panels.

The Challenge: Array Data and Filament's records()

Filament's records() method is designed to work seamlessly with Eloquent models, providing automatic filtering, searching, sorting, and pagination. However, when you're dealing with data retrieved as an array (for example, from an API or a complex database query), these features require manual handling. The core issue is that Filament expects an Eloquent Collection to apply its logic, and with array data we need to provide that functionality ourselves.

Implementing Manual Handling

To implement manual filtering, searching, sorting, and pagination, you'll need to intercept the request parameters passed to the table and apply them to your array data. Here's a breakdown of the process:

  1. Receive Parameters: Filament passes the filter, search, sort, and pagination parameters to the closure you define in records().
  2. Apply Filters: Manually filter the array data based on the received filter parameters. You'll likely need to iterate through the array and apply conditional checks.
  3. Implement Search: Implement a search function to filter the array data based on a search term. This usually involves checking if any of the relevant fields in each array element contain the search term.
  4. Apply Sorting: Use a sorting algorithm (e.g., usort in PHP) to sort the array data based on the specified column and direction.
  5. Create Pagination: Use Laravel's LengthAwarePaginator to paginate the filtered, searched, and sorted array data. You'll need to slice the array into chunks and create a paginator instance.

Code Example

Here's an example of how to manually paginate an array of data within a Filament table:

use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Collection;

// Assuming you have an array of $data

$perPage = 10; // Number of items per page
$currentPage = request()->get('page', 1); // Get the current page from the request

// Slice the collection to get the items for the current page
$currentPageItems = array_slice($data, ($currentPage * $perPage) - $perPage, $perPage);

$items = new Collection($currentPageItems);

// Create a LengthAwarePaginator instance
$paginator = new LengthAwarePaginator($items, count($data), $perPage, $currentPage, [
    'path' => request()->url(),
    'query' => request()->query(),
]);

return $paginator;

This code snippet demonstrates how to create a LengthAwarePaginator instance from an array of data. The $data array is sliced to get the items for the current page, and a new LengthAwarePaginator instance is created with the sliced items, the total number of items, the number of items per page, and the current page number.

Benefits and Considerations

Implementing manual filtering, searching, sorting, and pagination provides flexibility when working with array data in Filament tables. However, it also requires more effort and consideration. You'll need to carefully implement each feature and ensure that it works correctly with your data. Also, this approach might be less performant than using Eloquent models with built-in features, especially for large datasets. Consider caching if performance becomes an issue.

Conclusion

Manually implementing filtering, searching, sorting, and pagination for array-based report tables in Filament allows you to handle data from diverse sources effectively. While it requires more effort, it provides the flexibility needed when the default Eloquent integration isn't sufficient. Remember to optimize your implementation for performance and consider the trade-offs compared to using Eloquent models directly.

Enhancing Filament Tables with Manual Filtering, Searching, Sorting, and Pagination
GERARDO RUIZ

GERARDO RUIZ

Author

Share: