Skip to content

Commit 9ef5ae6

Browse files
committed
update
1 parent 9bb809a commit 9ef5ae6

File tree

8 files changed

+1468
-1546
lines changed

8 files changed

+1468
-1546
lines changed

app/Livewire/UserTable.php

+60-136
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,20 @@
33
namespace App\Livewire;
44

55
use App\Models\User;
6-
use Illuminate\Database\Eloquent\Builder;
76
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};
7+
use Illuminate\Database\Eloquent\Builder;
8+
use PowerComponents\LivewirePowerGrid\Button;
9+
use PowerComponents\LivewirePowerGrid\Column;
10+
use PowerComponents\LivewirePowerGrid\Detail;
11+
use PowerComponents\LivewirePowerGrid\Footer;
12+
use PowerComponents\LivewirePowerGrid\Header;
13+
use PowerComponents\LivewirePowerGrid\PowerGrid;
14+
use PowerComponents\LivewirePowerGrid\Exportable;
15+
use PowerComponents\LivewirePowerGrid\Facades\Rule;
16+
use PowerComponents\LivewirePowerGrid\Facades\Filter;
17+
use PowerComponents\LivewirePowerGrid\PowerGridFields;
18+
use PowerComponents\LivewirePowerGrid\Traits\WithExport;
19+
use PowerComponents\LivewirePowerGrid\PowerGridComponent;
2020

2121
final class UserTable extends PowerGridComponent
2222
{
@@ -28,20 +28,6 @@ final class UserTable extends PowerGridComponent
2828
'email.*' => ['required', 'email'],
2929
];
3030

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-
*/
4531
public function setUp(): array
4632
{
4733
$this->showCheckBox();
@@ -63,68 +49,32 @@ public function setUp(): array
6349
];
6450
}
6551

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-
*/
52+
protected function queryString()
53+
{
54+
return $this->powerGridQueryString('user');
55+
}
56+
7957
public function datasource(): Builder
8058
{
8159
return User::query();
8260
}
8361

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
62+
public function fields(): PowerGridFields
9663
{
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'));
64+
return PowerGrid::fields()
65+
->add('id')
66+
->add('name')
67+
->add('email')
68+
->add('created_at')
69+
->add('created_at_formatted', fn (User $model) => Carbon::parse($model->created_at)->format('d/m/Y H:i:s'));
10870
}
10971

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-
*/
12472
public function columns(): array
12573
{
12674
return [
127-
Column::make('Id', 'id'),
75+
Column::make('ID', 'id')
76+
->sortable(),
77+
12878
Column::make('Full Name', 'name')
12979
->sortable()
13080
->searchable(),
@@ -139,17 +89,12 @@ public function columns(): array
13989
->sortable(),
14090

14191
Column::make('UPDATED AT', 'updated_at_formatted', 'updated_at')
142-
->searchable()
143-
->sortable(),
92+
->searchable(),
14493

94+
Column::action('Action')
14595
];
14696
}
14797

148-
/**
149-
* PowerGrid Filters.
150-
*
151-
* @return array<int, Filter>
152-
*/
15398
public function filters(): array
15499
{
155100
return [
@@ -159,60 +104,39 @@ public function filters(): array
159104
];
160105
}
161106

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
107+
#[\Livewire\Attributes\On('edit')]
108+
public function edit($rowId): void
178109
{
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-
];
110+
$this->js('alert('.$rowId.')');
189111
}
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
112+
113+
public function actions(User $row): array
208114
{
209-
return [
115+
return [
116+
Button::make('edit', 'Edit')
117+
->class('btn btn-primary')
118+
->route('user.edit', ['user' => 'id']),
119+
120+
Button::make('destroy', 'Delete')
121+
->class('btn btn-danger')
122+
->route('user.destroy', ['user' => 'id'])
123+
->method('delete')
124+
];
125+
}
210126

211-
//Hide button edit for ID 1
212-
Rule::button('edit')
213-
->when(fn($user) => $user->id === 1)
214-
->hide(),
215-
];
127+
public function actionRules(User $row): array
128+
{
129+
return [
130+
Rule::button('edit')
131+
->when(fn ($row) => $row->id === 1)
132+
->hide(),
133+
];
134+
}
135+
136+
public function onUpdatedEditable($id, $field, $value): void
137+
{
138+
User::query()->find($id)->update([
139+
$field => $value,
140+
]);
216141
}
217-
*/
218142
}

composer.json

+7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@
44
"description": "The Laravel Framework.",
55
"keywords": ["framework", "laravel"],
66
"license": "MIT",
7+
"repositories": [
8+
{
9+
"type": "path",
10+
"url": "/Volumes/Secure/_Work/Personal Projects/Power Components/PowerGrid/livewire-powergrid"
11+
}
12+
],
13+
714
"require": {
815
"php": "^8.1",
916
"guzzlehttp/guzzle": "^7.7",

0 commit comments

Comments
 (0)