Skip to content

[6.x] Add default values for filters#2054

Closed
johannesMEGABAD wants to merge 8 commits into
Power-Components:6.xfrom
johannesMEGABAD:6.x
Closed

[6.x] Add default values for filters#2054
johannesMEGABAD wants to merge 8 commits into
Power-Components:6.xfrom
johannesMEGABAD:6.x

Conversation

@johannesMEGABAD

@johannesMEGABAD johannesMEGABAD commented Jan 6, 2026

Copy link
Copy Markdown
Contributor

⚡ PowerGrid - Pull Request

  • Bug fix
  • Enhancement
  • New feature
  • Breaking change

Description

This pull-request adds the functionality of setting default values for filters.

Usage

Simply chain ->default() to any filter with the appropriate value for that filter type:

public function filters(): array
{
    return [
        // Select filter - single value
        Filter::select('status', 'status')
            ->dataSource([...])
            ->default('active'),

        // Multi-select filter - array of values
        Filter::multiSelect('category_id', 'category_id')
            ->dataSource(Category::all())
            ->optionValue('id')
            ->optionLabel('name')
            ->default([1, 2, 3]),

        // Boolean filter - bool true, false, or string 'all'
        Filter::boolean('is_active', 'is_active')
            ->default(true),

        // Input text filter - string or array with no operators
        Filter::inputText('name', 'name')
            ->operators(),
            ->default('laptop'),

        // Input text with operator
        Filter::inputText('sku', 'sku')
            ->operators(['contains', 'starts_with'])
            ->default([
                'value' => 'PRD',
                'operator' => 'starts_with',
            ]),

        // Number filter - single value or range
        Filter::number('price', 'price')
            ->default(50), // minimum price

        // Number filter - range
        Filter::number('stock', 'stock')
            ->default([
                'start' => 10,
                'end' => 1000,
            ]),

        // Date filter - just start and end dates
        Filter::datepicker('created_at', 'created_at')
            ->default([
                'start' => '2025-01-01',
                'end' => '2025-12-31',
                'formatted' => '01.01.2023 bis 01.01.2025',
            ]),
    ];
}

Related Issue(s):

Documentation

This PR requires Documentation update?

  • Yes
  • No
  • I have already submitted a Documentation pull request.

@johannesMEGABAD

johannesMEGABAD commented Jan 6, 2026

Copy link
Copy Markdown
Contributor Author

@luanfreitasdev Here is my PR for the functionality to add default values to filters.. I hope I did not miss anything.. when testing everything worked fine for me. Probably it would be good to also add this to the documentation.

Also probably need to write/rewrite some test cases.. I am looking into that.
Failing tests seem to be all from FilterDatePickerTest.php:151 .. tho clearing the filter from the UI works fine.. looking into it

Readded a chunk of code I removed.. for god know why
@johannesMEGABAD

Copy link
Copy Markdown
Contributor Author

Ok I found the issue with the failing test cases and fixed it

Comment thread src/Concerns/Filter.php Outdated
collect($this->filters())
->each(function ($filter) use (&$defaultFiltersApplied) {
// Check if filter has a default value set
if (! isset($filter->defaultValue) || is_null($filter->defaultValue)) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think is_null check is redundant here

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup you are correct. Fixing it.

@luanfreitasdev luanfreitasdev left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@johannesMEGABAD. Thanks you for this PR!

Can you add any test ?

Comment thread src/Concerns/Filter.php

public bool $showFilters = false;

protected function applyDefaultFilters(): void

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion:

$columnsByField = collect($this->columns)
  ->mapWithKeys(fn (Column $column) => [
      filled($column->field) ?? filled($column->dataField) => $column,
  ]);

collect($this->filters())
    ->filter(fn ($filter) => filled($filter->defaultValue))
    ->each(function (FilterBase $filter) use (&$defaultFiltersApplied, $columnsByField) {
        $field = $filter->field;
        $column = $filter->column;
        $key = $filter->key;
        $defaultValue = $filter->defaultValue;

        $columnData = $columnsByField->get($column);
        $label = data_get($columnData, 'title', $field);

        switch ($key) {
            case 'select':
                data_set($this->filters, "select.{$field}", $defaultValue);
                $this->addEnabledFilters($field, $label);
                $defaultFiltersApplied = true;
                break;

            case 'multi_select':
                $values = is_array($defaultValue) ? $defaultValue : [$defaultValue];
                data_set($this->filters, "multi_select.{$field}", $values);
                $this->addEnabledFilters($field, $label);
                $defaultFiltersApplied = true;
                break;

            case 'boolean':
                data_set($this->filters, "boolean.{$field}", $defaultValue);
                $this->addEnabledFilters($field, $label);
                $defaultFiltersApplied = true;
                break;

            case 'input_text':
                if (is_array($defaultValue)) {
                    // Support for both value and operator
                    data_set($this->filters, "input_text.{$field}", $defaultValue['value'] ?? '');
                    if (isset($defaultValue['operator'])) {
                        data_set($this->filters, "input_text_options.{$field}", $defaultValue['operator']);
                    }
                } else {
                    data_set($this->filters, "input_text.{$field}", $defaultValue);
                }
                $this->addEnabledFilters($field, $label);
                $defaultFiltersApplied = true;
                break;

            case 'number':
                if (is_array($defaultValue)) {
                    if (isset($defaultValue['start'])) {
                        data_set($this->filters, "number.{$field}.start", $defaultValue['start']);
                    }
                    if (isset($defaultValue['end'])) {
                        data_set($this->filters, "number.{$field}.end", $defaultValue['end']);
                    }
                } else {
                    data_set($this->filters, "number.{$field}.start", $defaultValue);
                }
                $this->addEnabledFilters($field, $label);
                $defaultFiltersApplied = true;
                break;

            case 'date':
            case 'datetime':
                if (is_array($defaultValue) && isset($defaultValue['start']) && isset($defaultValue['end'])) {
                    $this->filters[$key][$field] = [
                        'start' => $defaultValue['start'],
                        'end' => $defaultValue['end'],
                        'formatted' => $defaultValue['formatted'] ?? '',
                    ];
                    $this->addEnabledFilters($field, $label);
                    $defaultFiltersApplied = true;
                }
                break;
        }
    });

if ($defaultFiltersApplied) {
    $this->persistState('filters');
}

@luanfreitasdev luanfreitasdev marked this pull request as draft January 10, 2026 16:27
@johannesMEGABAD

Copy link
Copy Markdown
Contributor Author

@johannesMEGABAD. Thanks you for this PR!

Can you add any test ?

Yup, will do.

@johannesMEGABAD

Copy link
Copy Markdown
Contributor Author

@luanfreitasdev Sorry it takes so long! Some stuff got in the way. Will do it this week.

@fiebertrauma fiebertrauma mentioned this pull request Feb 12, 2026
7 tasks
@luanfreitasdev

Copy link
Copy Markdown
Member

#2076

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants