Table should be blank in default

Actually I need the table to blank in default. It should be filled only with the search filter result. It should be blank again while using "reset filter". Is this possible?
12 Replies
Venky
VenkyOP2y ago
$this->activeFilters not found
protected function getTableQuery(): Builder
{
return StudentTransfer::query()
->when(
count($this->activeFilters) == 0,
fn ($query) => $query->where(DB::raw("1 = 0")),
);}
protected function getTableQuery(): Builder
{
return StudentTransfer::query()
->when(
count($this->activeFilters) == 0,
fn ($query) => $query->where(DB::raw("1 = 0")),
);}
Thank you @z3d0x I am trying this in custom page. Hence, I don't have List page. So Iam getting
Property [$activeFilters] not found on component
Property [$activeFilters] not found on component
error
ZedoX
ZedoX2y ago
try $this->tableFilters instead
Venky
VenkyOP2y ago
Thanks a lot. It works. Let me try apply the logic...
ZedoX
ZedoX2y ago
If possible please do share your solution here for future reference
Venky
VenkyOP2y ago
No, the $this->tableFilters not helped me. Why because, I need to know weather the request triggered from any filters or 'reset filter'. My requirement is only to show the respective record when I am searching through filter form. Otherwise the table should be blank. Here, the "$this->tableFilters" is always there and I am getting count of it as 1. I want to execute $query->whereNull('id') in default and while using 'reset filter' other wise it should return the records based on by filter value. I need to implement this requirement in many pages... Please help me. Thanks a lot to spending your valuable time here.
toeknee
toeknee2y ago
tableFilters should always be empty unless some have been applied? Have you set some filter defaults/
Venky
VenkyOP2y ago
No defaults, this is what I have
Filter::make('Code')
->form([
TextInput::make('transfer_code')

])
->query(function (Builder $query, array $data): Builder {
return $query
->when(
$data['transfer_code'],
fn (Builder $query, $code): Builder => $query->where('transfer_code', $code),
);
}),
Filter::make('Code')
->form([
TextInput::make('transfer_code')

])
->query(function (Builder $query, array $data): Builder {
return $query
->when(
$data['transfer_code'],
fn (Builder $query, $code): Builder => $query->where('transfer_code', $code),
);
}),
array:1 [▼ // app\Filament\Pages\Ho\Student\TransferPending.php:53
"Code" => array:1 [▼
"transfer_code" => null
]
]
array:1 [▼ // app\Filament\Pages\Ho\Student\TransferPending.php:53
"Code" => array:1 [▼
"transfer_code" => null
]
]
toeknee
toeknee2y ago
So don't you want to the do a check on code->transfer_code where not null?
Venky
VenkyOP2y ago
Yes, we can do it in that way. But, if we have few more filters, then we need to check every thing right? Please correct me if I am wrong...
toeknee
toeknee2y ago
Ahh I see, I would get the filters and loop and check if they are all NULL, should be fairly quick and easy.
Venky
VenkyOP2y ago
Usually the filters are adding the conditions with the existing query. But, I want to frame the query with only the filter values. Thanks @toeknee_iom for your valuable time. Yes, I will do it in that way. Please continue this post chain, If anyone having different approach... Thanks again. Finally I did in this way
$filter_array = $this->tableFilters;
$filter_check = checkNullFilters($filter_array);
$result = StudentTransfer::query()->whereNull('id');
if( Arr::get($filter_check, 'counter', 0))
{
$result = StudentTransfer::query()->where(Arr::get($filter_check, 'filter_array'));
}
else
{
$result = StudentTransfer::query()->whereNull('id');
}
return $result;
$filter_array = $this->tableFilters;
$filter_check = checkNullFilters($filter_array);
$result = StudentTransfer::query()->whereNull('id');
if( Arr::get($filter_check, 'counter', 0))
{
$result = StudentTransfer::query()->where(Arr::get($filter_check, 'filter_array'));
}
else
{
$result = StudentTransfer::query()->whereNull('id');
}
return $result;
Helper function:
function checkNullFilters(array $filters)
{
$filters = is_array($filters) ? $filters : [];
$counter = 0;
$filter_array = [];
foreach($filters as $l => $value)
{
if($l == "custom_filters")
{
foreach($value as $m => $val)
{
if(!is_null($val))
{
$counter ++;
array_push($filter_array, [
$m, '=', $val
]);
}
}
}else{
if(!is_null(Arr::get($value, "value")))
{
$counter ++;
array_push($filter_array, [
array_key_first($value), '=', Arr::get($value, "value")
]);
}
}
}
return ["counter" => $counter, "filter_array" => $filter_array];
}
function checkNullFilters(array $filters)
{
$filters = is_array($filters) ? $filters : [];
$counter = 0;
$filter_array = [];
foreach($filters as $l => $value)
{
if($l == "custom_filters")
{
foreach($value as $m => $val)
{
if(!is_null($val))
{
$counter ++;
array_push($filter_array, [
$m, '=', $val
]);
}
}
}else{
if(!is_null(Arr::get($value, "value")))
{
$counter ++;
array_push($filter_array, [
array_key_first($value), '=', Arr::get($value, "value")
]);
}
}
}
return ["counter" => $counter, "filter_array" => $filter_array];
}
Want results from more Discord servers?
Add your server