|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Livewire; |
| 4 | + |
| 5 | +use App\Models\User; |
| 6 | +use Illuminate\Database\Eloquent\Builder; |
| 7 | +use Illuminate\Support\Carbon; |
| 8 | +use PowerComponents\LivewirePowerGrid\{Button, |
| 9 | + Column, |
| 10 | + Components\Rules\RuleActions, |
| 11 | + Detail, |
| 12 | + Exportable, |
| 13 | + Facades\Filter, |
| 14 | + Footer, |
| 15 | + Header, |
| 16 | + PowerGrid, |
| 17 | + PowerGridColumns, |
| 18 | + PowerGridComponent, |
| 19 | + Traits\WithExport}; |
| 20 | + |
| 21 | +final class UserTable extends PowerGridComponent |
| 22 | +{ |
| 23 | + use WithExport; |
| 24 | + |
| 25 | + public array $email = []; |
| 26 | + |
| 27 | + protected array $rules = [ |
| 28 | + 'email.*' => ['required', 'email'], |
| 29 | + ]; |
| 30 | + |
| 31 | + public function onUpdatedEditable($id, $field, $value): void |
| 32 | + { |
| 33 | + User::query()->find($id)->update([ |
| 34 | + $field => $value, |
| 35 | + ]); |
| 36 | + } |
| 37 | + |
| 38 | + /* |
| 39 | + |-------------------------------------------------------------------------- |
| 40 | + | Features Setup |
| 41 | + |-------------------------------------------------------------------------- |
| 42 | + | Setup Table's general features |
| 43 | + | |
| 44 | + */ |
| 45 | + public function setUp(): array |
| 46 | + { |
| 47 | + $this->showCheckBox(); |
| 48 | + |
| 49 | + return [ |
| 50 | + Exportable::make('export') |
| 51 | + ->striped() |
| 52 | + ->type(Exportable::TYPE_XLS, Exportable::TYPE_CSV), |
| 53 | + Header::make() |
| 54 | + ->showSearchInput(), |
| 55 | + Footer::make() |
| 56 | + ->showPerPage() |
| 57 | + ->showRecordCount(), |
| 58 | + |
| 59 | + Detail::make() |
| 60 | + ->view('components.detail') |
| 61 | + ->params(['name' => 'Luan']) |
| 62 | + ->showCollapseIcon(), |
| 63 | + ]; |
| 64 | + } |
| 65 | + |
| 66 | + /* |
| 67 | + |-------------------------------------------------------------------------- |
| 68 | + | Datasource |
| 69 | + |-------------------------------------------------------------------------- |
| 70 | + | Provides data to your Table using a Model or Collection |
| 71 | + | |
| 72 | + */ |
| 73 | + |
| 74 | + /** |
| 75 | + * PowerGrid datasource. |
| 76 | + * |
| 77 | + * @return Builder<\App\Models\User> |
| 78 | + */ |
| 79 | + public function datasource(): Builder |
| 80 | + { |
| 81 | + return User::query(); |
| 82 | + } |
| 83 | + |
| 84 | + /* |
| 85 | + |-------------------------------------------------------------------------- |
| 86 | + | Add Column |
| 87 | + |-------------------------------------------------------------------------- |
| 88 | + | Make Datasource fields available to be used as columns. |
| 89 | + | You can pass a closure to transform/modify the data. |
| 90 | + | |
| 91 | + | ❗ IMPORTANT: When using closures, you must escape any value coming from |
| 92 | + | the database using the `e()` Laravel Helper function. |
| 93 | + | |
| 94 | + */ |
| 95 | + public function addColumns(): PowerGridColumns |
| 96 | + { |
| 97 | + return PowerGrid::columns() |
| 98 | + ->addColumn('id') |
| 99 | + ->addColumn('name') |
| 100 | + |
| 101 | + /** Example of custom column using a closure **/ |
| 102 | + ->addColumn('name_lower', function (User $model) { |
| 103 | + return strtolower(e($model->name)); |
| 104 | + }) |
| 105 | + |
| 106 | + ->addColumn('email') |
| 107 | + ->addColumn('created_at_formatted', fn (User $model) => Carbon::parse($model->created_at)->format('d/m/Y H:i:s')); |
| 108 | + } |
| 109 | + |
| 110 | + /* |
| 111 | + |-------------------------------------------------------------------------- |
| 112 | + | Include Columns |
| 113 | + |-------------------------------------------------------------------------- |
| 114 | + | Include the columns added columns, making them visible on the Table. |
| 115 | + | Each column can be configured with properties, filters, actions... |
| 116 | + | |
| 117 | + */ |
| 118 | + |
| 119 | + /** |
| 120 | + * PowerGrid Columns. |
| 121 | + * |
| 122 | + * @return array<int, Column> |
| 123 | + */ |
| 124 | + public function columns(): array |
| 125 | + { |
| 126 | + return [ |
| 127 | + Column::make('Id', 'id'), |
| 128 | + Column::make('Full Name', 'name') |
| 129 | + ->sortable() |
| 130 | + ->searchable(), |
| 131 | + |
| 132 | + Column::make('Email', 'email') |
| 133 | + ->sortable() |
| 134 | + ->editOnClick(true) |
| 135 | + ->searchable(), |
| 136 | + |
| 137 | + Column::make('CREATED AT', 'created_at_formatted', 'created_at') |
| 138 | + ->searchable() |
| 139 | + ->sortable(), |
| 140 | + |
| 141 | + Column::make('UPDATED AT', 'updated_at_formatted', 'updated_at') |
| 142 | + ->searchable() |
| 143 | + ->sortable(), |
| 144 | + |
| 145 | + ]; |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * PowerGrid Filters. |
| 150 | + * |
| 151 | + * @return array<int, Filter> |
| 152 | + */ |
| 153 | + public function filters(): array |
| 154 | + { |
| 155 | + return [ |
| 156 | + Filter::inputText('name'), |
| 157 | + Filter::inputText('email'), |
| 158 | + Filter::datetimepicker('created_at', 'created_at'), |
| 159 | + ]; |
| 160 | + } |
| 161 | + |
| 162 | + /* |
| 163 | + |-------------------------------------------------------------------------- |
| 164 | + | Actions Method |
| 165 | + |-------------------------------------------------------------------------- |
| 166 | + | Enable the method below only if the Routes below are defined in your app. |
| 167 | + | |
| 168 | + */ |
| 169 | + |
| 170 | + /** |
| 171 | + * PowerGrid User Action Buttons. |
| 172 | + * |
| 173 | + * @return array<int, Button> |
| 174 | + */ |
| 175 | + |
| 176 | + /* |
| 177 | + public function actions(): array |
| 178 | + { |
| 179 | + return [ |
| 180 | + Button::make('edit', 'Edit') |
| 181 | + ->class('bg-indigo-500 cursor-pointer text-white px-3 py-2.5 m-1 rounded text-sm') |
| 182 | + ->route('user.edit', ['user' => 'id']), |
| 183 | +
|
| 184 | + Button::make('destroy', 'Delete') |
| 185 | + ->class('bg-red-500 cursor-pointer text-white px-3 py-2 m-1 rounded text-sm') |
| 186 | + ->route('user.destroy', ['user' => 'id']) |
| 187 | + ->method('delete') |
| 188 | + ]; |
| 189 | + } |
| 190 | + */ |
| 191 | + |
| 192 | + /* |
| 193 | + |-------------------------------------------------------------------------- |
| 194 | + | Actions Rules |
| 195 | + |-------------------------------------------------------------------------- |
| 196 | + | Enable the method below to configure Rules for your Table and Action Buttons. |
| 197 | + | |
| 198 | + */ |
| 199 | + |
| 200 | + /** |
| 201 | + * PowerGrid User Action Rules. |
| 202 | + * |
| 203 | + * @return array<int, RuleActions> |
| 204 | + */ |
| 205 | + |
| 206 | + /* |
| 207 | + public function actionRules(): array |
| 208 | + { |
| 209 | + return [ |
| 210 | +
|
| 211 | + //Hide button edit for ID 1 |
| 212 | + Rule::button('edit') |
| 213 | + ->when(fn($user) => $user->id === 1) |
| 214 | + ->hide(), |
| 215 | + ]; |
| 216 | + } |
| 217 | + */ |
| 218 | +} |
0 commit comments