Skip to content

Commit 027449b

Browse files
Merge pull request #139 from Power-Components/default-value
Support for default filter values
2 parents c8cd876 + d2e1b83 commit 027449b

1 file changed

Lines changed: 128 additions & 1 deletion

File tree

docs/table-features/filters.md

Lines changed: 128 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -722,11 +722,138 @@ class DishTable extends PowerGridComponent
722722
}
723723
```
724724

725+
## Default Filter Values
726+
727+
PowerGrid allows you to set default values for filters. These values will be applied automatically when the table loads, filtering the data before displaying it to the user.
728+
729+
### Usage
730+
731+
Use the `default()` method to set a default value for any filter.
732+
733+
```php
734+
use PowerComponents\LivewirePowerGrid\Facades\Filter;
735+
736+
public function filters(): array
737+
{
738+
return [
739+
Filter::select('category_name', 'category_id')
740+
->dataSource(Category::all())
741+
->optionLabel('name')
742+
->optionValue('id')
743+
->default(1), // [!code ++]
744+
745+
Filter::boolean('in_stock')
746+
->label('In Stock', 'Out of Stock')
747+
->default(true), // [!code ++]
748+
];
749+
}
750+
```
751+
752+
---
753+
754+
### Supported Filter Types
755+
756+
The following filter types support default values:
757+
758+
#### Select Filter
759+
760+
```php
761+
Filter::select('category_name', 'category_id')
762+
->dataSource(Category::all())
763+
->optionLabel('name')
764+
->optionValue('id')
765+
->default(1), // Single value matching the optionValue
766+
```
767+
768+
#### Multi-Select Filter
769+
770+
```php
771+
Filter::multiSelect('category_name', 'category_id')
772+
->dataSource(Category::all())
773+
->optionLabel('name')
774+
->optionValue('id')
775+
->default([1, 2, 3]), // Array of values
776+
```
777+
778+
#### Boolean Filter
779+
780+
```php
781+
Filter::boolean('in_stock')
782+
->label('In Stock', 'Out of Stock')
783+
->default(true), // true or false
784+
```
785+
786+
#### Text Filter
787+
788+
```php
789+
// Simple text value
790+
Filter::inputText('name')
791+
->default('Pizza'),
792+
793+
// With operator
794+
Filter::inputText('name')
795+
->default([
796+
'value' => 'Pizza',
797+
'operator' => 'contains',
798+
]),
799+
```
800+
801+
#### Number Filter
802+
803+
```php
804+
// Single value (start)
805+
Filter::number('price', 'price')
806+
->default(100),
807+
808+
// Range (start and end)
809+
Filter::number('price', 'price')
810+
->default([
811+
'start' => 100,
812+
'end' => 500,
813+
]),
814+
```
815+
816+
#### Datepicker / Datetime Picker Filter
817+
818+
```php
819+
Filter::datetimepicker('created_at_formatted', 'created_at')
820+
->default([
821+
'start' => '2024-01-01 00:00:00',
822+
'end' => '2024-12-31 23:59:59',
823+
]),
824+
825+
Filter::datepicker('production_date', 'production_date')
826+
->default([
827+
'start' => '2024-01-01',
828+
'end' => '2024-12-31',
829+
]),
830+
```
831+
832+
---
833+
834+
### Default Filter Methods
835+
836+
**default(mixed $value)**
837+
838+
Set the default value for a filter. The value format depends on the filter type:
839+
840+
| Filter Type | Default Value Format |
841+
|------------------|--------------------------------------------------|
842+
| `select` | Single value (matching optionValue) |
843+
| `multiSelect` | Array of values |
844+
| `boolean` | `true` or `false` |
845+
| `inputText` | String or array with `value` and `operator` keys |
846+
| `number` | Number or array with `start` and `end` keys |
847+
| `datepicker` | Array with `start` and `end` keys |
848+
| `datetimepicker` | Array with `start` and `end` keys |
849+
850+
---
851+
725852
## Custom Components
726853

727854
If you need to further customize your filters, you may render Blade Components with the Filter. This is useful when working with external packages like [WireUI](https://livewire-wireui.com/).
728855

729-
To render a component, chain the `component()` method to your Filter passing the Blade View and the attributes you may want to use in your component(E.g, wire:model or classes).
856+
To render a component, chain the `component()` method to your Filter passing the Blade View and the attributes you may want to use in your component (E.g., wire:model or classes).
730857

731858
To use default PowerGrid attributes, just call `$attributes->getAttributes()` within your component.
732859

0 commit comments

Comments
 (0)