@@ -54,11 +54,27 @@ public function mount(QueryStringData $queryStringData)
5454 {
5555 $ this ->filtersViews = $ this ->filters ();
5656 $ this ->search = $ queryStringData ->getSearchValue ($ this ->search );
57+
5758 $ this ->filters = $ queryStringData ->getFilterValues ($ this ->filters );
59+
60+ $ this ->applyDefaultFilters ();
61+
5862 $ this ->sortBy = $ queryStringData ->getValue ('sortBy ' , $ this ->sortBy );
5963 $ this ->sortOrder = $ queryStringData ->getValue ('sortOrder ' , $ this ->sortOrder );
6064 }
6165
66+ /**
67+ * Check if each of the filters has a default value and it's not already set
68+ */
69+ public function applyDefaultFilters ()
70+ {
71+ foreach ($ this ->filters () as $ filter ) {
72+ if (empty ($ this ->filters [$ filter ->id ]) && $ filter ->defaultValue ) {
73+ $ this ->filters [$ filter ->id ] = $ filter ->defaultValue ;
74+ }
75+ }
76+ }
77+
6278 /**
6379 * Collects all data to be passed to the view, this includes the items searched on the database
6480 * through the filters, this data will be passed to livewire render method
@@ -80,6 +96,7 @@ public function getItems(Searchable $searchable, Filterable $filterable, Sortabl
8096 $ query = $ this ->repository ();
8197
8298 $ query = $ searchable ->searchItems ($ query , $ this ->searchBy , $ this ->search );
99+
83100 $ query = $ filterable ->applyFilters ($ query , $ this ->filters (), $ this ->filters );
84101 $ query = $ sortable ->sortItems ($ query , $ this ->sortBy , $ this ->sortOrder );
85102
0 commit comments