Custom makeInputSelect filter #602
Replies: 1 comment 1 reply
-
Hi @kolydart,
I will push a PR to the doc and another to Powergrid Demo featuring a clear example. My first idea was to suggest you to consider using an Enum for the states and combine it with the makeInputEnumSelect filter. However, if this is not possible, I would take the following approach: 📌 Add a method on // File: app/Models/User.php
<?php
use Illuminate\Support\Collection;
class User extends Authenticatable
{
//...
public static function statuses(): Collection
{
return collect(
[
['status' => 1, 'label' => 'Active'],
['status' => 0, 'label' => 'Pending'],
['status' => -1, 'label' => 'Inactive'],
]
);
} 📌 Add a column public function addColumns(): PowerGridEloquent
{
return PowerGrid::eloquent()
//...
->addColumn('status')
->addColumn('status_label', fn ($user) => User::statuses()->firstWhere('status', $user->status)['label'])
} 📌 Include the column public function columns(): array
{
return [
//...
Column::add()
->title('status')
->field('status_label', 'status')
->makeInputSelect(User::statuses(), 'label', 'status'),
];
} Resulting in: In this way, if you need to change, add or remove any of the statuses, you only have to modify the |
Beta Was this translation helpful? Give feedback.
-
Hi. I've been looking for a way to create a custom makeInputSelect filter, from an array. Could not find it in documentation or in this discussion. I found a way by creating a collection. I quote it here, in case it is usefull for others.
The model is simple;
Item
model has astate
attribute containg a values from[-1,0,1]
. I want to show / convert state attribute in my table to text values['Inactive', 'Pending', 'Active']
and filter it by these values. So, the powergrid file contains the following:Result:
Beta Was this translation helpful? Give feedback.
All reactions