Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

[FEATURE REQUEST] Custom filters in SpladeTable Component #359

Closed
luisvbz opened this issue Mar 17, 2023 · 12 comments
Closed

[FEATURE REQUEST] Custom filters in SpladeTable Component #359

luisvbz opened this issue Mar 17, 2023 · 12 comments

Comments

@luisvbz
Copy link

luisvbz commented Mar 17, 2023

  • Laravel Version: 10.3.3
  • PHP Version: 8.1.17
  • Splade JS Version (npm): 1.4.1
  • Splade PHP Version (composer): 1.4.1

Hello, it would be great to have the possibility of adding custom filters in SpladeTable tables with a slot and thus adding these filters with the Splade form components.
For example:

<x-splade-table :for="$users">
    <x-slot name="filters">
           <!-- The key attribute would be the column by which it would filter -->
           <x-splade-input label="Date of Creation" name="created_at"  key="created_at" date range />
           <x-splade-select label="Country" name="country" :options="$countires" key="country_id" choices />
    </x-slot>
</x-splade-table>

So we could use these filters automatically by the key attributte or using the When function within the SpladeTable class
For exmaple:

class Users extends AbstractTable
{

    public function for()
    {
        return User::query()->when($this->filters['created_at'], function($q) {
                         $q->whereBetween('created_at', $this->filters['created_at'])
                   })->when($this->filters['country'], fn($q) => $q->where('country_id', $this->filters['country']);
    }
}

By the way, excellent work!

@zulfikar-ditya
Copy link
Contributor

Did you mean splade data table filter ?
Did you look this doc. I don't now this help or not but here
Try using splade table in Table Class section

php artisan make:table Users

will look like this

$table
// ...other column's 
->selectFilter(
    key: 'id', 
    label: 'Country',
    options: \App\Models\Country::all(['id', 'name'])->toArray()
)
// ...other column's 

@luisvbz CMIIW

@luisvbz
Copy link
Author

luisvbz commented Mar 21, 2023

Yes, I know it has those filters. Actually, I use them quite often.
My idea or proposal is to enable the "filters" slot to allow you to add any filter you need, such as an input with a date range, filters with checkboxes, a multiple select, etc. This would give you greater control over what you want to search for, instead of just relying on the filters that the SpladeTable class currently has.

@zulfikar-ditya thanks for reply me

Did you mean splade data table filter ? Did you look this doc. I don't now this help or not but here Try using splade table in Table Class section

php artisan make:table Users

will look like this

$table
// ...other column's 
->selectFilter(
    key: 'id', 
    label: 'Country',
    options: \App\Models\Country::all(['id', 'name'])->toArray()
)
// ...other column's 

@luisvbz CMIIW

Yes, I know it has those filters. Actually, I use them quite often.
My idea or proposal is to enable the "filters" slot to allow you to add any filter you need, such as an input with a date range, filters with checkboxes, a multiple select, etc. This would give you greater control over what you want to search for, instead of just relying on the filters that the SpladeTable class currently has.

@zulfikar-ditya
Copy link
Contributor

Ah my bad,
I don't recognize that, but date range picker is in progress #162 by @pascalbaljet . Maybe we would like to filter with multiple select value to or filter using checkbox like you said @luisvbz

@luisvbz
Copy link
Author

luisvbz commented May 2, 2023

Any updates?

@zulfikar-ditya
Copy link
Contributor

#162 stil in draft

@e-smily
Copy link

e-smily commented Nov 4, 2023

Did you mean splade data table filter ? Did you look this doc. I don't now this help or not but here Try using splade table in Table Class section

php artisan make:table Users

will look like this

$table
// ...other column's 
->selectFilter(
    key: 'id', 
    label: 'Country',
    options: \App\Models\Country::all(['id', 'name'])->toArray()
)
// ...other column's 

@luisvbz CMIIW

Always get:

htmlspecialchars(): Argument #1 ($string) must be of type string, array given

when i try to fill the options of the select-filter:

options: ObjektGruppe::all(['id', 'gruppe_name_de'])->toArray() ,

it only works with:

options: [8 => 'Gemälde', 22 => 'Porzellan',],

how can i dynamicly fill the select-filter?

@dusp2k
Copy link

dusp2k commented Nov 6, 2023

Did you mean splade data table filter ? Did you look this doc. I don't now this help or not but here Try using splade table in Table Class section

php artisan make:table Users

will look like this

$table
// ...other column's 
->selectFilter(
    key: 'id', 
    label: 'Country',
    options: \App\Models\Country::all(['id', 'name'])->toArray()
)
// ...other column's 

@luisvbz CMIIW

Always get:

htmlspecialchars(): Argument #1 ($string) must be of type string, array given

when i try to fill the options of the select-filter:

options: ObjektGruppe::all(['id', 'gruppe_name_de'])->toArray() ,

it only works with:

options: [8 => 'Gemälde', 22 => 'Porzellan',],

how can i dynamicly fill the select-filter?

Use mapWithKeys() to flatten the array to key/value pairs:

options: ObjektGruppe::all()->mapWithKeys(fn($objektGruppe) => [$objektGruppe->id => $objektGruppe->gruppe_name_de])

@e-smily
Copy link

e-smily commented Nov 6, 2023

Thank you Daniel, that helped.
For the sake of completeness, here is what it looks like now:

            $table->selectFilter(
                key: 'objekt_gruppe_id',
                options: ObjektGruppe::all()->where('aktiv', '=', 1)->sortBy('gruppe_name_de')
                ->mapWithKeys(fn($objektGruppe) =>[$objektGruppe->id => $objektGruppe->gruppe_name_de])
                ->toArray(),
                label: 'Gruppe',
                noFilterOption: true,
                noFilterOptionLabel: 'All Gruppen'
            );

@e-smily
Copy link

e-smily commented Nov 6, 2023

I just found another problem, but maybe I'm just making a mistake?

The SQL string that is created:

select count(*) as aggregate from objectleft joinobject_grouponobject.object_group_id=object_group.idandobject_group.deleted_at is null where LOWER(object. object_group_id) LIKE '%2%' and object.deleted_at is null

The problem is that the key is filtered as a string with LIKE:

where LOWER(object.object_group_id) LIKE '%2%'

So I don't get a unique group, it would have to be filtered for an integer:

where object.object_group_id = 2

how can I influence that?

@dusp2k
Copy link

dusp2k commented Nov 6, 2023

Don't know about the LIKE, but this:

ObjektGruppe::all()->where('aktiv', '=', 1)->sortBy('gruppe_name_de')

This will first load all records from the DB and than do the where() and sortBy() on the collection.

You might want to do it the following way, which will do the filtering and ordering on the DB side:

ObjektGruppe::where('aktiv', '=', 1)->orderBy('gruppe_name_de', 'asc')->get()

@e-smily
Copy link

e-smily commented Nov 6, 2023

Unfortunately my English isn't the best :)

the select component is filled and displayed correctly, but when I try to filter and select an element, the SQL string described above is generated internally by Splade.. I have no influence on that..
For example, I have the following data in the "object groups" table (id is integer, group_name_de is text)

1 painting
2 glass
...
15 porcelain

If you now filter by object_group_id LIKE '%1%', paintings and porcelain will be included in the selection
if object_group_id = 1 is filtered, only paintings will be selected

@kahfieidn
Copy link

See this #539 => #539

working fine for me, For now my only workaround is to make custom month & year filter, using spatie query builder

@protonemedia protonemedia locked and limited conversation to collaborators Mar 20, 2024
@pascalbaljet pascalbaljet converted this issue into discussion #590 Mar 20, 2024

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants