leantony / laravel-grid

A grid view for laravel, inspired by the yii2 grid widget
https://leantony.github.io/laravel-grid/
MIT License
91 stars 39 forks source link

Bug (or Enhancement ?) : Use select filter data for rendering rows #81

Open Nuranto opened 5 years ago

Nuranto commented 5 years ago

For the column :

"status" => [
                "label" => 'Status',
                "filter" => [
                    "enabled" => true,
                            'type' => 'select',
                            'data' => ['0'=>'Pending', '1' =>'Processing']
               ] 

The display in rows is 0 or 1. It would be nice if the default presenter took filter.data into consideration, and displayed automatically "Pending" or "Processing" (or empty if null value). At the moment, we need to do this :

"status" => [
                "label" => 'Status',
                         'presenter' => function ($columnData, $columnName) {
                            $statuses = ['0'=>'Pending', '1' =>'Processing'];
                            return $statuses[$columnData->status] ?? '';
                        },
                "filter" => [
                    "enabled" => true,
                            'type' => 'select',
                            'data' => ['0'=>'Pending', '1' =>'Processing']
                ] 

which works, but is quite redundant.

leantony commented 5 years ago

hmmm...why don't you just add the data on your class and use it as such on the grid attributes?

public function getStatuses() {
   return ['0'=>'Pending', '1' =>'Processing']
}

Then on your grid,

[
                "label" => 'Status',
                         'presenter' => function ($columnData, $columnName) {
                            return $this->getStatuses()[$columnData->status] ?? '';
                        },
                "filter" => [
                    "enabled" => true,
                            'type' => 'select',
                            'data' => $this->getStatuses()
                ] 
]

After all, the grid is just a normal class

Nuranto commented 5 years ago

That's what I'm doing actually (well, I trigger the values from another model, but whatever), I tried to simplify the example. But even like this, it is redondant and non-intuitive.

I think in 99% of cases, the 'data' of a select filter will be used in the presenter. So why don't make it as default ?

I agree it is a small enhancement for lazy/perfectionist devs :)

leantony commented 5 years ago

Ok, I'll add it as an option.