Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Date Range Filter Table Splade #539

Closed
kahfieidn opened this issue Dec 1, 2023 · 1 comment
Closed

Date Range Filter Table Splade #539

kahfieidn opened this issue Dec 1, 2023 · 1 comment

Comments

@kahfieidn
Copy link

kahfieidn commented Dec 1, 2023

  • Laravel Version: 10
  • PHP Version: 8.1
  • Splade JS Version (npm): 1.4
  • Splade PHP Version (composer): 1.4

Hello, i need feature date range table filter in splade, this is important,, any reference to alternative this feature?

@kahfieidn kahfieidn changed the title Date Range Filter Splade Date Range Filter Table Splade Dec 2, 2023
@kahfieidn
Copy link
Author

Thanks to JIMMO help me to solve this.

For now my only workaround is to make custom month & year filter, using spatie query builder
ref : https://spatie.be/docs/laravel-query-builder/v5/installation-setup

Splade support Table with Spatie's Query Builder => https://splade.dev/docs/table-spatie-query-builder

$yearFilter = AllowedFilter::callback('year', function ($query, $value) {
            $query->where(function ($query) use ($value) {
                Collection::wrap($value)->each(function ($value) use ($query) {
                    $query->whereYear('users.created_at', $value);
                });
            });
});

$monthFilter = AllowedFilter::callback('month', function ($query, $value) {
            $query->where(function ($query) use ($value) {
                Collection::wrap($value)->each(function ($value) use ($query) {
                    $query->whereMonth('users.created_at', $value);
                });
            });
});

return QueryBuilder::for(User::class)
            ->defaultSort('-created_at')
            ->allowedSorts(['created_at'])
            ->allowedFilters(['username', 'name', 'email', $yearFilter, $monthFilter]);

Then u need to manually set the year & month filter options in splade table

Example:

->selectFilter(key: 'year', options: ['2024' => '2024', '2023' => '2023', '2022' => '2022', '2021' => '2021'], label: 'Tahun Izin', noFilterOptionLabel: '-')
->selectFilter(key: 'month', options: ['01' => 'January', '02' => 'February', '03' => 'March', '04' => 'April', '05' => 'May', '06' => 'June', '07' => 'July', '08' => 'August', '09' => 'September', '10' => 'October', '11' => 'November', '12' => 'December'], label: 'Month Permission', noFilterOptionLabel: '-')

It working fine for me, maybe this thread can help you to another alternative waiting splade upgrading Table filter improvements in branch #162

Thanks

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

No branches or pull requests

1 participant