query() api which allows you to modify the underlying eloquent query. This means you can “filter” a table’s data without needing to have that filter on your table.
However, while being able to modify the underlying eloquent query is powerful, and in some cases might be the only way to filter a table, Advanced Tables v3 introduces a new defaultFilters() api which allows you to apply values to your table filters. This, in turn, offers a better UX for your end-users as they will then see filter indicators in the table and will better understand how a Preset View is modifying the data.
For more information on the difference between User Views and Preset Views please refer to Core Concept: User Views vs Preset Views
Filament Filter Tabs
Filament v3 introducedFilter Tabs which also allows developers to programmatically filter their data in tabs. While similar, Advanced Tables’ Preset Views offers multiple additional features:
- Allow users to hide, favorite, and/or reorder Preset Views (New)
- Combine with User Views for one consistent UI
- Relation manager support
- Table builder support
- Multiple themes
- Include filters (New)
- Include toggled columns
- Include sorting column and direction
- Include reordered columns (New)
- Persist the active Preset View to the session
- Color options
- Visibility options
Creating a Preset View
To create a Preset View, add thegetPresetView() method to your List*, Manage*, or table widget class.
modifyQueryUsing() method modifies your original eloquent query by applying the scopes and conditions you configure.
Customizing the Preset View label
By default the array keys will be used as the labels for each Preset View. This may be configured by passing a label into themake() method:
label() method:
Adding a Preset View to the Favorites Bar
By default, Preset Views are added to the View Manager in the Preset View section. To add a Preset View to the Favorites Bar use thefavorite() method:
Adding an icon
Similar to User Views, Preset Views may have icons:Display the Preset View with a color
Similar to User Views, Preset Views may be displayed in a color.primary, success, info, warning, danger, gray.
You may also include any extra colors you have previously registered in Filament:
Adding a badge (New)
Preset Views can display a badge after the label by passing a string into thebadge() method:
Changing the badge color (New)
The color of a badge may be changed using thebadgeColor() method:
Tip: If you want to display multiple badges, you should generate one query separately and then use Laravel collections to filter and count them.
Adding a tooltip (New)
Preset Views can display a tooltip when hovered over in the Favorites Bar by passing a string into thetooltip() method.:
Showing or hiding
You may conditionally show or hide Preset Views for certain users using either thevisible() or hidden() methods, passing a closure:
OrderPolicy:
Tip: If your policy is not working, be sure to register it in AuthServiceProvider as sometimes Laravel does not successfully auto-register policies.
Applying filters (New)
You can apply values to your table filters from your Preset Views with thedefaultFilters() method:
defaultFilters() api gives your users a better understanding of how a Preset View is filtering the data by turning on Filament’s filter indicators.
Tip: The easiest way to know how to properly form your filter array is to apply the desired filter to your table and thendd($this->tableFilters)at the top of thegetPresetViews()method.
Applying filters with Filter Builder (New)
If you are using Advanced Filter Builder, you should use the following syntax to define your default filters:new orders made this month OR cancelled orders made last month.
Tip: The easiest way to know how to properly form your filter array is to apply the desired filter to your table and thenIf you are using the builder’s Column Filters, then you will need to add the applicable values. For example:dd($this->tableFilters)at the top of thegetPresetViews()method.
Tip: Many of the column filters have multiplekeyssuch asdate_start,date_end, etc. You only need to add the values that you are setting for the filter.
Applying default grouping (New)
You can apply one of your table groupings to your Preset View with thedefaultGrouping() method:
Toggling and reordering columns (New)
Preset Views can toggle columns, and, if Reorderable Columns are enabled, can reorder them as well using thedefaultColumns() method:
- If Reorderable Columns are enabled, Advanced Tables will sort the columns in the order they are included in the
defaultColumns()array. - If a column is not included in the array and is
toggleable(), it will be hidden. - If a column is not included in the array and is not
toggleable(), it will be added to the end of the table.
Setting a default table sort
If a column is sortable, you may choose it as the default sort column for your table using thedefaultSort() method:
->defaultSort('total_price', 'desc').
Tip: While it is possible to addorderBy()to your query to sort your table, usingdefaultSort()is recommended as it will correctly show the sorting indicator on the table column.
Setting a default table multi-sort (New)
If Multi-Sort is enabled, you may multi-sort your preset views through the samedefaultSort() method. Just pass an array of columns and their sort direction to defaultSort():
Loading a default Preset View
You may choose one of your Preset Views as the default view when loading the page by using thedefault() method:
default() can take a callback which can allow you to dynamically choose which Preset View is the default based on the conditions you choose. The first Preset View that returns true will be the view that is loaded by default.
Preserving user selected filters, toggled columns, sort column, and sort direction
By default, when an end-users clicks a Preset View, the filters, toggled columns, sort column, and sort direction that a user has already applied to a table will be removed in favor of the Preset View’s configuration. This is usually the desired behavior as Preset Views are meant to be customized views into data. However in some instances, you may wish to preserve the user’s selected filters, columns, etc. To do this you may usepreserveAll().
Note: By preserving a user’s selection you are in turn removing the option for a Preset View to always take a user to that view’s predefined configuration as that view is now affected by the user.
Preset Views configurations
Advanced Tables offers multiple ways to customize Preset Views. Unless specified otherwise, these options can be configured directly on theAdvancedTablesPlugin object inside your PanelProvider:
Configuring the Managed Preset View class (New)
TheManagedPresetView class is responsable for storing the visibility and sorting configurations between a User and a PresetView If you need to extend this class, you may pass your custom class to managedPresetView():
Disabling Preset View management (New)
By default, Preset Views will be sorted in the order they are added to thegetPresetViews() array. Similarly, any Preset View that has the favorite() method will be displayed by default in the end-user’s Favorites Bar, and any Preset View without favorite() will be displayed in the View Manager.
Advanced Tables v3 now allows Preset Views to be managed by the end-user. Users can sort Preset Views as well as add/remove them from the Favorites Bar.
If you need to disable Preset View management you may do so by passing false to the ->globalUserViewsManageable() method:
presetViewsManageable, Preset Views will be not be able to be sorted nor added/removed from a user’s Favorites Bar.
Configuring new Preset Views sort position (New)
When Preset View management is enabled, a user is free to reorder, add or remove them from their Favorites Bar. Now, when a new Preset View is added in code and then deployed to the user, a decision needs to be made as to whether this new Preset View should be placedbefore or after the user’s previously ordered Preset Views. By default new Preset Views are positioned before a user’s current Preset View ordering, however this can be configured using the newPresetViewSortPosition() method:
Persisting the active Preset View to Session
Persisting the active Preset View to the session allows a user to navigate away from the table and then return to the table with the same view selected. You may enable this by using the->persistActiveViewInSession() method in your Panel Provider:
Distinguishing between Preset Views and Users Views
Enabling both Preset Views and User Views has the potential of causing confusion with end-users if they create a User View on top of a Preset View. This is because a Preset View can “filter” the table using thequery() method which doesn’t correspond to any filter on the table. This means that when a user builds a User View using a Preset View as its base, there’s no way for the user to “turn off” the filter scope.
While Advanced Tables has multiple options you can use to mitigate these potential issues, the easiest way around this issue is to not use the query() method and instead use the defaultFilters() method. This way, even when a user creates a User View based on a Preset View, they are in full control of data.
However, if you still need, or prefer, to use the query() method to filter your data, you can use these options to help avoid confusion. These configurations should be applied to the AdvancedTablesPlugin in your Panel Provider.
Disable User View creation
You can disable the creation of User Views all together when a Preset View is selected. If disabled, when a user clicks the Quick Save Button, a Filament notification will be displayed explaining that this action is not possible. The text of the notification can be configured in the language file.Display a divider line
You can optionally display a divider line between Preset Views and User Views to help visually distinguish between the two.Display a lock icon
You can optionally display an icon next to a Preset View to help visually distinguish between the two.heroicon-o-lock-closed will be used, however you may pass any heroicon icon to the method:
Display a query indicator in the Save View slideOver or modal (New)
Another option to help a user know how a Preset View is modifying the query is to use the->indicator() method on the Preset View:
indicator() method, a new badge with the selected text will be displayed in the View Summary when creating a new view.
Display helper text in the Save View slideOver or modal
Finally, you can display helper text in the slideOver or modal that explains that the user has chosen a Preset View as the base for their User View and that the filtering applied in the Preset View set cannot be removed. This text can be configured in the language file.Displaying the legacy dropdown
Version 1 of Advanced Tables (Filter Sets), introduced the ability to show Preset Views in a dropdown. This has been deprecated in favor of the View Manager, however if you would still like to display it you may do so with thepresetViewLegacyDropdown() method: