|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace PowerComponents\LivewirePowerGrid\Tests\Concerns\Components; |
| 4 | + |
| 5 | +use Illuminate\Support\Collection; |
| 6 | +use PowerComponents\LivewirePowerGrid\{Column, Facades\PowerGrid, PowerGridComponent, PowerGridFields}; |
| 7 | + |
| 8 | +class DishesCustomSortCollectionTable extends PowerGridComponent |
| 9 | +{ |
| 10 | + public string $tableName = 'dishes-custom-sort-collection-table'; |
| 11 | + |
| 12 | + public function datasource(): Collection |
| 13 | + { |
| 14 | + return collect([ |
| 15 | + ['id' => 1, 'name' => 'Zebra Dish', 'price' => 100.00, 'calories' => 300], |
| 16 | + ['id' => 2, 'name' => 'Apple Pie', 'price' => 50.00, 'calories' => 200], |
| 17 | + ['id' => 3, 'name' => 'Banana Split', 'price' => 75.00, 'calories' => 400], |
| 18 | + ['id' => 4, 'name' => 'Cherry Tart', 'price' => 25.00, 'calories' => 100], |
| 19 | + ['id' => 5, 'name' => 'Donut', 'price' => 10.00, 'calories' => 500], |
| 20 | + ]); |
| 21 | + } |
| 22 | + |
| 23 | + public function setUp(): array |
| 24 | + { |
| 25 | + return [ |
| 26 | + PowerGrid::header() |
| 27 | + ->showSearchInput(), |
| 28 | + |
| 29 | + PowerGrid::footer() |
| 30 | + ->showPerPage() |
| 31 | + ->showRecordCount(), |
| 32 | + ]; |
| 33 | + } |
| 34 | + |
| 35 | + public function fields(): PowerGridFields |
| 36 | + { |
| 37 | + return PowerGrid::fields() |
| 38 | + ->add('id') |
| 39 | + ->add('name') |
| 40 | + ->add('price') |
| 41 | + ->add('calories'); |
| 42 | + } |
| 43 | + |
| 44 | + public function columns(): array |
| 45 | + { |
| 46 | + return [ |
| 47 | + Column::make('Id', 'id') |
| 48 | + ->sortable(), |
| 49 | + |
| 50 | + Column::make('Name', 'name') |
| 51 | + ->sortable(), |
| 52 | + |
| 53 | + Column::make('Price', 'price') |
| 54 | + ->sortUsing(function (Collection $collection, string $direction) { |
| 55 | + // Custom sort: sort by price with rounding to nearest 50 |
| 56 | + return $collection->sortBy(function ($item) { |
| 57 | + return round(data_get($item, 'price') / 50) * 50; |
| 58 | + }, SORT_REGULAR, $direction === 'desc'); |
| 59 | + }), |
| 60 | + |
| 61 | + Column::make('Calories', 'calories') |
| 62 | + ->sortUsing(function (Collection $collection, string $direction) { |
| 63 | + // Custom sort: sort by calories in groups (low/medium/high) |
| 64 | + return $collection->sortBy(function ($item) { |
| 65 | + $calories = data_get($item, 'calories'); |
| 66 | + if ($calories <= 200) { |
| 67 | + return 1; // Low |
| 68 | + } |
| 69 | + if ($calories <= 400) { |
| 70 | + return 2; // Medium |
| 71 | + } |
| 72 | + |
| 73 | + return 3; // High |
| 74 | + }, SORT_REGULAR, $direction === 'desc'); |
| 75 | + }), |
| 76 | + |
| 77 | + Column::action('Action'), |
| 78 | + ]; |
| 79 | + } |
| 80 | + |
| 81 | + public function setTestThemeClass(string $themeClass): void |
| 82 | + { |
| 83 | + config(['livewire-powergrid.theme' => $themeClass]); |
| 84 | + } |
| 85 | +} |
0 commit comments