Skip to content

Commit 99badcc

Browse files
committed
Merge branch 'develop'
2 parents 9286470 + be167d5 commit 99badcc

25 files changed

+664
-33
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
All notable changes to `laravel-livewire-tables` will be documented in this file
44

5-
## 1.0.0 - 2020-XX-XX
5+
## [Unreleased]
6+
7+
## 0.1.0 - 2020-04-03
68

79
- Initial release
10+
11+
[Unreleased]: https://github.com/rappasoft/laravel-livewire-tables/compare/v0.1.0...development

README.md

Lines changed: 144 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
[![StyleCI](https://styleci.io/repos/250246992/shield?style=plastic)](https://github.styleci.io/repos/250246992)
55
[![Total Downloads](https://img.shields.io/packagist/dt/rappasoft/laravel-livewire-tables.svg?style=flat-square)](https://packagist.org/packages/rappasoft/laravel-livewire-tables)
66

7-
**This package is currently in development and the source is constantly changing, use at your own risk.**
7+
**This package is still in development and does not have a test suite.**
88

99
A dynamic Laravel Livewire component for data tables.
1010

@@ -31,8 +31,8 @@ namespace App\Http\Livewire;
3131

3232
use App\User;
3333
use Illuminate\Database\Eloquent\Builder;
34-
use Rappasoft\LivewireTables\Http\Livewire\Column;
35-
use Rappasoft\LivewireTables\Http\Livewire\TableComponent;
34+
use Rappasoft\LaravelLivewireTables\Views\Column;
35+
use Rappasoft\LaravelLivewireTables\TableComponent;
3636

3737
class UsersTable extends TableComponent
3838
{
@@ -268,6 +268,147 @@ 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 hide 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+
**Note:** You should still assert on the backend that these functions can not be performed on this entity.
309+
310+
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:
311+
312+
```php
313+
Column::make('Actions')
314+
->components([
315+
Link::make('Edit'),
316+
Link::make('Delete'),
317+
], function($model) {
318+
// Hide the actions for model id 1
319+
return $model->id === 1;
320+
}, function($model) {
321+
return __('You can not alter role ' . $model->name . '.');
322+
})
323+
````
324+
325+
#### Methods
326+
327+
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.
328+
329+
#### Inherited by all components
330+
| Method | Usage |
331+
| -------- | ----- |
332+
| setAttribute($attribute, $value) | Set an attribute on the component |
333+
| setAttributes(array $attributes = []) | Set multiple attributes at once |
334+
| getAttributes() | Get the array of available attributes |
335+
| setOption($option, $value) | Set an option on the component |
336+
| setOptions(array $options = []) | Set multiple options at once |
337+
| getOptions() | Get the array of available options |
338+
| hideIf($condition) | Hide this component if true |
339+
| hide() | Hide this component forever |
340+
| isHidden() | This component is currently hidden |
341+
342+
By default all components have access to the `$attributes` and `$options` arrays.
343+
344+
#### Link Component
345+
346+
| Method | Usage | Type |
347+
| -------- | ----- | ---- |
348+
| text($text) | Set the text of the link | string/false |
349+
| class($class) | Set the html class on the link | string |
350+
| id($id) | Set the id of the link | string |
351+
| icon($icon) | Set the icon of the link (font awesome) | string |
352+
| href(function($model){}) | Set the href of the link | string/callback |
353+
| view($view) | The view to render for the component | string |
354+
355+
#### Button Component
356+
357+
| Method | Usage | Type |
358+
| -------- | ----- | ---- |
359+
| text($text) | Set the text of the button | string/false |
360+
| class($class) | Set the html class on the button | string |
361+
| id($id) | Set the id of the button | string |
362+
| icon($icon) | Set the icon of the button (font awesome) | string |
363+
| view($view) | The view to render for the component | string |
364+
365+
#### Example
366+
367+
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.
368+
369+
This example uses searching, sorting, relationships, custom attributes, counted relationships, and components:
370+
371+
```php
372+
public function columns() : array {
373+
return [
374+
Column::make('Name')
375+
->searchable()
376+
->sortable(),
377+
Column::make('Permissions', 'permissions_label')
378+
->customAttribute()
379+
->html()
380+
->searchable(function($builder, $term) {
381+
return $builder->orWhereHas('permissions', function($query) use($term) {
382+
return $query->where('name', 'like', '%'.$term.'%');
383+
});
384+
}),
385+
Column::make('Number of Users', 'users_count')
386+
->sortable(),
387+
Column::make('Actions')
388+
->components([
389+
Link::make('Edit') // Optionally pass false to hide the text
390+
->icon('fas fa-pencil-alt')
391+
->class('btn btn-primary btn-sm')
392+
->href(function($model) {
393+
return route('admin.auth.role.edit', $model->id);
394+
})
395+
->hideIf(auth()->user()->cannot('access.roles.edit')),
396+
Link::make('Delete')
397+
->icon('fas fa-trash')
398+
->class('btn btn-danger btn-sm')
399+
->setAttribute('data-method', 'delete') // Javascript takes over and injects a hidden form
400+
->href(function($model) {
401+
return route('admin.auth.role.destroy', $model->id);
402+
})
403+
->hideIf(auth()->user()->cannot('access.roles.delete')),
404+
], function($model) {
405+
// Hide components for this row if..
406+
return $model->id === config('access.roles.admin');
407+
}),
408+
];
409+
}
410+
```
411+
271412
## Inspiration From:
272413

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

composer.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,26 +27,25 @@
2727
},
2828
"autoload": {
2929
"psr-4": {
30-
"Rappasoft\\LivewireTables\\": "src"
30+
"Rappasoft\\LaravelLivewireTables\\": "src"
3131
}
3232
},
3333
"autoload-dev": {
3434
"psr-4": {
35-
"Rappasoft\\LivewireTables\\Tests\\": "tests"
35+
"Rappasoft\\LaravelLivewireTables\\Tests\\": "tests"
3636
}
3737
},
3838
"scripts": {
3939
"test": "vendor/bin/phpunit",
4040
"test-coverage": "vendor/bin/phpunit --coverage-html coverage"
41-
4241
},
4342
"config": {
4443
"sort-packages": true
4544
},
4645
"extra": {
4746
"laravel": {
4847
"providers": [
49-
"Rappasoft\\LivewireTables\\LivewireTablesServiceProvider"
48+
"Rappasoft\\LaravelLivewireTables\\LivewireTablesServiceProvider"
5049
]
5150
}
5251
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<button type="button"
2+
@foreach ($attributes as $key => $value)
3+
{{ $key }}="{{ $value }}"
4+
@endforeach
5+
>
6+
@if (array_key_exists('icon', $options))
7+
<i class="{{ $options['icon'] }}"></i>
8+
@endif
9+
10+
{{ $options['text'] ?? '' }}
11+
</button>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<a
2+
@foreach ($attributes as $key => $value)
3+
@if ($key === 'href' && is_callable($value))
4+
{{ $key }}="{{ app()->call($value, ['model' => $model]) }}"
5+
@else
6+
{{ $key }}="{{ $value }}"
7+
@endif
8+
@endforeach
9+
>
10+
@if (array_key_exists('icon', $options))
11+
<i class="{{ $options['icon'] }}"></i>
12+
@endif
13+
14+
{{ $options['text'] ?? '' }}
15+
</a>

resources/views/includes/_body.blade.php

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
<tr
77
class="{{ $this->setTableRowClass($model) }}"
88
id="{{ $this->setTableRowId($model) }}"
9-
@foreach ($this->setTableRowAttributes($model) as $key => $value) {{ $key . '="'.$value.'"' }} @endforeach
9+
@foreach ($this->setTableRowAttributes($model) as $key => $value)
10+
{{ $key }}="{{ $value }}"
11+
@endforeach
1012
>
1113
@if($checkbox && $checkboxLocation === 'left')
1214
@include('laravel-livewire-tables::includes._checkbox-row')
@@ -16,9 +18,25 @@ class="{{ $this->setTableRowClass($model) }}"
1618
<td
1719
class="{{ $this->setTableDataClass($column->attribute, Arr::get($model->toArray(), $column->attribute)) }}"
1820
id="{{ $this->setTableDataId($column->attribute, Arr::get($model->toArray(), $column->attribute)) }}"
19-
@foreach ($this->setTableDataAttributes($column->attribute, Arr::get($model->toArray(), $column->attribute)) as $key => $value) {{ $key . '="'.$value.'"' }} @endforeach
21+
@foreach ($this->setTableDataAttributes($column->attribute, Arr::get($model->toArray(), $column->attribute)) as $key => $value)
22+
{{ $key }}="{{ $value }}"
23+
@endforeach
2024
>
21-
@if ($column->isView())
25+
@if ($column->hasComponents())
26+
@if ($column->componentsAreHiddenForModel($model))
27+
@if ($message = $column->componentsHiddenMessageForModel($model))
28+
{{ $message }}
29+
@else
30+
&nbsp;
31+
@endif
32+
@else
33+
@foreach($column->getComponents() as $component)
34+
@if (! $component->isHidden())
35+
@include($component->view(), ['model' => $model, 'attributes' => $component->getAttributes(), 'options' => $component->getOptions()])
36+
@endif
37+
@endforeach
38+
@endif
39+
@elseif ($column->isView())
2240
@include($column->view)
2341
@else
2442
@if ($column->isHtml())

resources/views/includes/_columns.blade.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
<th
77
class="{{ $this->setTableHeadClass($column->attribute) }}"
88
id="{{ $this->setTableHeadId($column->attribute) }}"
9-
@foreach ($this->setTableHeadAttributes($column->attribute) as $key => $value) {{ $key . '="'.$value.'"' }} @endforeach
9+
@foreach ($this->setTableHeadAttributes($column->attribute) as $key => $value)
10+
{{ $key }}="{{ $value }}"
11+
@endforeach
1012
>
1113
@if($column->sortable)
1214
<span style="cursor: pointer;" wire:click="sort('{{ $column->attribute }}')">

src/LivewireTablesServiceProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Rappasoft\LivewireTables;
3+
namespace Rappasoft\LaravelLivewireTables;
44

55
use Illuminate\Support\ServiceProvider;
66

src/Http/Livewire/TableComponent.php renamed to src/TableComponent.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
<?php
22

3-
namespace Rappasoft\LivewireTables\Http\Livewire;
3+
namespace Rappasoft\LaravelLivewireTables;
44

55
use Illuminate\Database\Eloquent\Builder;
66
use Illuminate\Support\Str;
77
use Illuminate\View\View;
88
use Livewire\Component;
99
use Livewire\WithPagination;
10-
use Rappasoft\LivewireTables\Http\Livewire\Traits\Checkboxes;
11-
use Rappasoft\LivewireTables\Http\Livewire\Traits\Loading;
12-
use Rappasoft\LivewireTables\Http\Livewire\Traits\Offline;
13-
use Rappasoft\LivewireTables\Http\Livewire\Traits\Pagination;
14-
use Rappasoft\LivewireTables\Http\Livewire\Traits\Search;
15-
use Rappasoft\LivewireTables\Http\Livewire\Traits\Sorting;
16-
use Rappasoft\LivewireTables\Http\Livewire\Traits\Table;
17-
use Rappasoft\LivewireTables\Http\Livewire\Traits\Yajra;
10+
use Rappasoft\LaravelLivewireTables\Traits\Checkboxes;
11+
use Rappasoft\LaravelLivewireTables\Traits\Loading;
12+
use Rappasoft\LaravelLivewireTables\Traits\Offline;
13+
use Rappasoft\LaravelLivewireTables\Traits\Pagination;
14+
use Rappasoft\LaravelLivewireTables\Traits\Search;
15+
use Rappasoft\LaravelLivewireTables\Traits\Sorting;
16+
use Rappasoft\LaravelLivewireTables\Traits\Table;
17+
use Rappasoft\LaravelLivewireTables\Traits\Yajra;
1818

1919
/**
2020
* Class TableComponent.

src/Traits/CanBeHidden.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace Rappasoft\LaravelLivewireTables\Traits;
4+
5+
/**
6+
* Trait CanBeHidden.
7+
*/
8+
trait CanBeHidden
9+
{
10+
/**
11+
* @var bool
12+
*/
13+
protected $hidden = false;
14+
15+
/**
16+
* @param $condition
17+
*
18+
* @return $this
19+
*/
20+
public function hideIf($condition): self
21+
{
22+
$this->hidden = $condition === true;
23+
24+
return $this;
25+
}
26+
27+
/**
28+
* @param bool $hidden
29+
*
30+
* @return $this
31+
*/
32+
public function hide($hidden = true): self
33+
{
34+
$this->hidden = $hidden;
35+
36+
return $this;
37+
}
38+
39+
/**
40+
* @return bool
41+
*/
42+
public function isHidden(): bool
43+
{
44+
return $this->hidden;
45+
}
46+
}

0 commit comments

Comments
 (0)