livewire-datatables icon indicating copy to clipboard operation
livewire-datatables copied to clipboard

Feature Request: set default values to filters

Open paulmod opened this issue 3 years ago • 9 comments

is it possible to add a method that set a default value to a filter? Column::name('name')->filterable()->filterDefault('John') DateColumn::name('date')->filterable()->filterDefault(['start' => '1970-01-01', 'end' => '1970-12-31')

paulmod avatar Nov 09 '20 15:11 paulmod

You can do this Inside builder method:

$this->activeSelectFilters[9][0] = "Default value";

*Replace "9" with the appropriate column number

amit-viacon avatar Nov 30 '20 07:11 amit-viacon

You can do this Inside builder method:

$this->activeSelectFilters[9][0] = "Default value";

*Replace "9" with the appropriate column number

The results are then properly filtered by the new defaults but those default settings are not displayed in the header row. I tried to call $this->refreshLivewireDatatable() with no luck.

gotroch avatar Dec 03 '20 15:12 gotroch

I did it with redefining mount() function.

Something like:

    public function mount(
        $model = null,
        $include = [],
        $exclude = [],
        $hide = [],
        $dates = [],
        $times = [],
        $searchable = [],
        $sort = null,
        $hideHeader = null,
        $hidePagination = null,
        $perPage = 10,
        $exportable = false,
        $hideable = false,
        $beforeTableSlot = false,
        $afterTableSlot = false,
        $params = []
    )
    {
        parent::mount($model,
            $include,
            $exclude,
            $hide,
            $dates,
            $times,
            $searchable,
            $sort,
            $hideHeader,
            $hidePagination,
            $perPage,
            $exportable,
            $hideable,
            $beforeTableSlot,
            $afterTableSlot,
            $params);
        $this->activeBooleanFilters[4] = "1";
    }

plassani avatar Dec 14 '20 16:12 plassani

You can do this Inside builder method:

$this->activeSelectFilters[9][0] = "Default value";

*Replace "9" with the appropriate column number

The results are then properly filtered by the new defaults but those default settings are not displayed in the header row. I tried to call $this->refreshLivewireDatatable() with no luck.

I have the same issue, no filter set visible and I also get an error when selecting others and deselecting the default one.

goldjunge4 avatar Dec 31 '20 08:12 goldjunge4

You can also set the appropriate property inside your table class. For example:


public $activeBooleanFilters = [
    3 => 1,
    4 => 0
];

public activeSelectFilters = [
    9 => ["Default value"]
];

amit-viacon avatar Oct 01 '21 11:10 amit-viacon

Set you defaults on mount():

public $activeBooleanFilters = [];

public $mount() { $this->activeBooleanFilters[4] = "1"; }

Then wire:model the control to the activeFilter you had setted the defaults to:

amaurycid avatar Oct 02 '21 21:10 amaurycid

+1 This would be a really nice addition.

jjmpsp avatar Feb 23 '22 11:02 jjmpsp

+1

trippo avatar Mar 03 '22 10:03 trippo

How to use this please give a demo code on builder thanks

@amit-viacon

abbasmashaddy72 avatar Jul 23 '22 05:07 abbasmashaddy72

Hey guys. I have added this Feature. Would love to have your feedback on it.

Refs #527

Cosnavel avatar Nov 26 '22 00:11 Cosnavel