Skip to content

Commit b0888fa

Browse files
committed
Update readme
1 parent a997238 commit b0888fa

File tree

1 file changed

+139
-0
lines changed

1 file changed

+139
-0
lines changed

README.md

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,145 @@ public function setTableDataId($attribute, $value) : ?string;
268268
public function setTableDataAttributes($attribute, $value) : array;
269269
```
270270

271+
### Components
272+
273+
Along with being able to provide a view to a column, you can use pre-defined components that are built into the package. These are good for when you want to add actions to a column.
274+
275+
**Note:** By design using the components() method on a column will disable all other functionality (i.e. searching/sorting etc.).
276+
277+
#### Defining Components for a Column
278+
279+
```php
280+
Column::make('Actions')
281+
->components([
282+
Link::make('Edit'),
283+
Link::make('Delete'),
284+
])
285+
````
286+
287+
or
288+
289+
```php
290+
Column::make('Actions')
291+
->addComponent(Link::make('Edit'))
292+
->addComponent(Link::make('Delete'))
293+
````
294+
295+
If you would like to all the components for a given row, you may pass a callback as the second parameter of the `components()` method:
296+
297+
```php
298+
Column::make('Actions')
299+
->components([
300+
Link::make('Edit'),
301+
Link::make('Delete'),
302+
], function($model) {
303+
// Hide the actions for model id 1
304+
return $model->id === 1;
305+
})
306+
````
307+
308+
Building on that, if you would like to pass a custom message to that column when hiding the components for this row, you may pass another callback as the third parameter:
309+
310+
```php
311+
Column::make('Actions')
312+
->components([
313+
Link::make('Edit'),
314+
Link::make('Delete'),
315+
], function($model) {
316+
// Hide the actions for model id 1
317+
return $model->id === 1;
318+
}, function($model) {
319+
return __('You can not alter role ' . $model->name . '.');
320+
})
321+
````
322+
323+
#### Methods
324+
325+
Of course two links that don't do anything would be useless, here are a list of methods to be used for the built in components.
326+
327+
#### Inherited by all components
328+
| Method | Usage |
329+
| -------- | ----- |
330+
| setAttribute($attribute, $value) | Set an attribute on the component |
331+
| setAttributes(array $attributes = []) | Set multiple attributes at once |
332+
| getAttributes() | Get the array of available attributes |
333+
| setOption($option, $value) | Set an option on the component |
334+
| setOptions(array $options = []) | Set multiple options at once |
335+
| getOptions() | Get the array of available options |
336+
| hideIf($condition) | Hide this component if true |
337+
| hide() | Hide this component forever |
338+
| isHidden() | This component is currently hidden |
339+
340+
By default all components have access to the `$attributes` and `$options` arrays.
341+
342+
#### Link Component
343+
344+
| Method | Usage | Type |
345+
| -------- | ----- | ---- |
346+
| text($text) | Set the text of the link | string/false |
347+
| class($class) | Set the html class on the link | string |
348+
| id($id) | Set the id of the link | string |
349+
| icon($icon) | Set the icon of the link (font awesome) | string |
350+
| href(function($model){}) | Set the href of the link | string/callback |
351+
| view($view) | The view to render for the component | string |
352+
353+
#### Button Component
354+
355+
| Method | Usage | Type |
356+
| -------- | ----- | ---- |
357+
| text($text) | Set the text of the button | string/false |
358+
| class($class) | Set the html class on the button | string |
359+
| id($id) | Set the id of the button | string |
360+
| icon($icon) | Set the icon of the button (font awesome) | string |
361+
| view($view) | The view to render for the component | string |
362+
363+
#### Example
364+
365+
This example comes from the upcoming release of my popular [Laravel Boilerplate Project](http://laravel-boilerplate.com). Here we render the roles table in the admin panel.
366+
367+
This example uses searching, sorting, relationships, custom attributes, counted relationships, and components:
368+
369+
```php
370+
public function columns() : array {
371+
return [
372+
Column::make('Name')
373+
->searchable()
374+
->sortable(),
375+
Column::make('Permissions', 'permissions_label')
376+
->customAttribute()
377+
->html()
378+
->searchable(function($builder, $term) {
379+
return $builder->orWhereHas('permissions', function($query) use($term) {
380+
return $query->where('name', 'like', '%'.$term.'%');
381+
});
382+
}),
383+
Column::make('Number of Users', 'users_count')
384+
->sortable(),
385+
Column::make('Actions')
386+
->components([
387+
Link::make(false)
388+
->icon('fas fa-pencil-alt')
389+
->class('btn btn-primary btn-sm')
390+
->href(function($model) {
391+
return route('admin.auth.role.edit', $model->id);
392+
})
393+
->hideIf(auth()->user()->cannot('access.roles.edit')),
394+
Link::make(false)
395+
->icon('fas fa-trash')
396+
->class('btn btn-danger btn-sm')
397+
->setAttribute('data-method', 'delete')
398+
->href(function($model) {
399+
return route('admin.auth.role.destroy', $model->id);
400+
})
401+
->hideIf(auth()->user()->cannot('access.roles.delete')),
402+
], function($model) {
403+
// Hide components for this row if..
404+
return $model->id === config('access.roles.admin');
405+
}),
406+
];
407+
}
408+
```
409+
271410
## Inspiration From:
272411

273412
- [https://github.com/kdion4891/laravel-livewire-tables](https://github.com/kdion4891/laravel-livewire-tables)

0 commit comments

Comments
 (0)