3
3
namespace App \Livewire ;
4
4
5
5
use App \Models \User ;
6
- use Illuminate \Database \Eloquent \Builder ;
7
6
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 ;
20
20
21
21
final class UserTable extends PowerGridComponent
22
22
{
@@ -28,20 +28,6 @@ final class UserTable extends PowerGridComponent
28
28
'email.* ' => ['required ' , 'email ' ],
29
29
];
30
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
31
public function setUp (): array
46
32
{
47
33
$ this ->showCheckBox ();
@@ -63,68 +49,32 @@ public function setUp(): array
63
49
];
64
50
}
65
51
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
+
79
57
public function datasource (): Builder
80
58
{
81
59
return User::query ();
82
60
}
83
61
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
96
63
{
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 ' ));
108
70
}
109
71
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
72
public function columns (): array
125
73
{
126
74
return [
127
- Column::make ('Id ' , 'id ' ),
75
+ Column::make ('ID ' , 'id ' )
76
+ ->sortable (),
77
+
128
78
Column::make ('Full Name ' , 'name ' )
129
79
->sortable ()
130
80
->searchable (),
@@ -139,17 +89,12 @@ public function columns(): array
139
89
->sortable (),
140
90
141
91
Column::make ('UPDATED AT ' , 'updated_at_formatted ' , 'updated_at ' )
142
- ->searchable ()
143
- ->sortable (),
92
+ ->searchable (),
144
93
94
+ Column::action ('Action ' )
145
95
];
146
96
}
147
97
148
- /**
149
- * PowerGrid Filters.
150
- *
151
- * @return array<int, Filter>
152
- */
153
98
public function filters (): array
154
99
{
155
100
return [
@@ -159,60 +104,39 @@ public function filters(): array
159
104
];
160
105
}
161
106
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
178
109
{
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 .') ' );
189
111
}
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
208
114
{
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
+ }
210
126
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
+ ]);
216
141
}
217
- */
218
142
}
0 commit comments