Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
rappasoft committed Apr 4, 2020
2 parents a0c1d65 + 5c9e9e1 commit 90d3dcd
Show file tree
Hide file tree
Showing 25 changed files with 664 additions and 33 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

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

## 1.0.0 - 2020-XX-XX
## [Unreleased]

## 0.1.0 - 2020-04-03

- Initial release

[Unreleased]: https://github.com/rappasoft/laravel-livewire-tables/compare/v0.1.0...development
147 changes: 144 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
[![StyleCI](https://styleci.io/repos/250246992/shield?style=plastic)](https://github.styleci.io/repos/250246992)
[![Total Downloads](https://img.shields.io/packagist/dt/rappasoft/laravel-livewire-tables.svg?style=flat-square)](https://packagist.org/packages/rappasoft/laravel-livewire-tables)

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

A dynamic Laravel Livewire component for data tables.

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

use App\User;
use Illuminate\Database\Eloquent\Builder;
use Rappasoft\LivewireTables\Http\Livewire\Column;
use Rappasoft\LivewireTables\Http\Livewire\TableComponent;
use Rappasoft\LaravelLivewireTables\Views\Column;
use Rappasoft\LaravelLivewireTables\TableComponent;

class UsersTable extends TableComponent
{
Expand Down Expand Up @@ -268,6 +268,147 @@ public function setTableDataId($attribute, $value) : ?string;
public function setTableDataAttributes($attribute, $value) : array;
```

### Components

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.

**Note:** By design using the `components()` method on a column will disable all other functionality (i.e. searching/sorting etc.).

#### Defining Components for a Column

```php
Column::make('Actions')
->components([
Link::make('Edit'),
Link::make('Delete'),
])
````

or

```php
Column::make('Actions')
->addComponent(Link::make('Edit'))
->addComponent(Link::make('Delete'))
````

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:

```php
Column::make('Actions')
->components([
Link::make('Edit'),
Link::make('Delete'),
], function($model) {
// Hide the actions for model id 1
return $model->id === 1;
})
````

**Note:** You should still assert on the backend that these functions can not be performed on this entity.

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:

```php
Column::make('Actions')
->components([
Link::make('Edit'),
Link::make('Delete'),
], function($model) {
// Hide the actions for model id 1
return $model->id === 1;
}, function($model) {
return __('You can not alter role ' . $model->name . '.');
})
````

#### Methods

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.

#### Inherited by all components
| Method | Usage |
| -------- | ----- |
| setAttribute($attribute, $value) | Set an attribute on the component |
| setAttributes(array $attributes = []) | Set multiple attributes at once |
| getAttributes() | Get the array of available attributes |
| setOption($option, $value) | Set an option on the component |
| setOptions(array $options = []) | Set multiple options at once |
| getOptions() | Get the array of available options |
| hideIf($condition) | Hide this component if true |
| hide() | Hide this component forever |
| isHidden() | This component is currently hidden |

By default all components have access to the `$attributes` and `$options` arrays.

#### Link Component

| Method | Usage | Type |
| -------- | ----- | ---- |
| text($text) | Set the text of the link | string/false |
| class($class) | Set the html class on the link | string |
| id($id) | Set the id of the link | string |
| icon($icon) | Set the icon of the link (font awesome) | string |
| href(function($model){}) | Set the href of the link | string/callback |
| view($view) | The view to render for the component | string |

#### Button Component

| Method | Usage | Type |
| -------- | ----- | ---- |
| text($text) | Set the text of the button | string/false |
| class($class) | Set the html class on the button | string |
| id($id) | Set the id of the button | string |
| icon($icon) | Set the icon of the button (font awesome) | string |
| view($view) | The view to render for the component | string |

#### Example

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.

This example uses searching, sorting, relationships, custom attributes, counted relationships, and components:

```php
public function columns() : array {
return [
Column::make('Name')
->searchable()
->sortable(),
Column::make('Permissions', 'permissions_label')
->customAttribute()
->html()
->searchable(function($builder, $term) {
return $builder->orWhereHas('permissions', function($query) use($term) {
return $query->where('name', 'like', '%'.$term.'%');
});
}),
Column::make('Number of Users', 'users_count')
->sortable(),
Column::make('Actions')
->components([
Link::make('Edit') // Optionally pass false to hide the text
->icon('fas fa-pencil-alt')
->class('btn btn-primary btn-sm')
->href(function($model) {
return route('admin.auth.role.edit', $model->id);
})
->hideIf(auth()->user()->cannot('access.roles.edit')),
Link::make('Delete')
->icon('fas fa-trash')
->class('btn btn-danger btn-sm')
->setAttribute('data-method', 'delete') // Javascript takes over and injects a hidden form
->href(function($model) {
return route('admin.auth.role.destroy', $model->id);
})
->hideIf(auth()->user()->cannot('access.roles.delete')),
], function($model) {
// Hide components for this row if..
return $model->id === config('access.roles.admin');
}),
];
}
```

## Inspiration From:

- [https://github.com/kdion4891/laravel-livewire-tables](https://github.com/kdion4891/laravel-livewire-tables)
Expand Down
7 changes: 3 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,25 @@
},
"autoload": {
"psr-4": {
"Rappasoft\\LivewireTables\\": "src"
"Rappasoft\\LaravelLivewireTables\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"Rappasoft\\LivewireTables\\Tests\\": "tests"
"Rappasoft\\LaravelLivewireTables\\Tests\\": "tests"
}
},
"scripts": {
"test": "vendor/bin/phpunit",
"test-coverage": "vendor/bin/phpunit --coverage-html coverage"

},
"config": {
"sort-packages": true
},
"extra": {
"laravel": {
"providers": [
"Rappasoft\\LivewireTables\\LivewireTablesServiceProvider"
"Rappasoft\\LaravelLivewireTables\\LivewireTablesServiceProvider"
]
}
}
Expand Down
11 changes: 11 additions & 0 deletions resources/views/components/button.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<button type="button"
@foreach ($attributes as $key => $value)
{{ $key }}="{{ $value }}"
@endforeach
>
@if (array_key_exists('icon', $options))
<i class="{{ $options['icon'] }}"></i>
@endif

{{ $options['text'] ?? '' }}
</button>
15 changes: 15 additions & 0 deletions resources/views/components/link.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<a
@foreach ($attributes as $key => $value)
@if ($key === 'href' && is_callable($value))
{{ $key }}="{{ app()->call($value, ['model' => $model]) }}"
@else
{{ $key }}="{{ $value }}"
@endif
@endforeach
>
@if (array_key_exists('icon', $options))
<i class="{{ $options['icon'] }}"></i>
@endif

{{ $options['text'] ?? '' }}
</a>
24 changes: 21 additions & 3 deletions resources/views/includes/_body.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
<tr
class="{{ $this->setTableRowClass($model) }}"
id="{{ $this->setTableRowId($model) }}"
@foreach ($this->setTableRowAttributes($model) as $key => $value) {{ $key . '="'.$value.'"' }} @endforeach
@foreach ($this->setTableRowAttributes($model) as $key => $value)
{{ $key }}="{{ $value }}"
@endforeach
>
@if($checkbox && $checkboxLocation === 'left')
@include('laravel-livewire-tables::includes._checkbox-row')
Expand All @@ -16,9 +18,25 @@ class="{{ $this->setTableRowClass($model) }}"
<td
class="{{ $this->setTableDataClass($column->attribute, Arr::get($model->toArray(), $column->attribute)) }}"
id="{{ $this->setTableDataId($column->attribute, Arr::get($model->toArray(), $column->attribute)) }}"
@foreach ($this->setTableDataAttributes($column->attribute, Arr::get($model->toArray(), $column->attribute)) as $key => $value) {{ $key . '="'.$value.'"' }} @endforeach
@foreach ($this->setTableDataAttributes($column->attribute, Arr::get($model->toArray(), $column->attribute)) as $key => $value)
{{ $key }}="{{ $value }}"
@endforeach
>
@if ($column->isView())
@if ($column->hasComponents())
@if ($column->componentsAreHiddenForModel($model))
@if ($message = $column->componentsHiddenMessageForModel($model))
{{ $message }}
@else
&nbsp;
@endif
@else
@foreach($column->getComponents() as $component)
@if (! $component->isHidden())
@include($component->view(), ['model' => $model, 'attributes' => $component->getAttributes(), 'options' => $component->getOptions()])
@endif
@endforeach
@endif
@elseif ($column->isView())
@include($column->view)
@else
@if ($column->isHtml())
Expand Down
4 changes: 3 additions & 1 deletion resources/views/includes/_columns.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
<th
class="{{ $this->setTableHeadClass($column->attribute) }}"
id="{{ $this->setTableHeadId($column->attribute) }}"
@foreach ($this->setTableHeadAttributes($column->attribute) as $key => $value) {{ $key . '="'.$value.'"' }} @endforeach
@foreach ($this->setTableHeadAttributes($column->attribute) as $key => $value)
{{ $key }}="{{ $value }}"
@endforeach
>
@if($column->sortable)
<span style="cursor: pointer;" wire:click="sort('{{ $column->attribute }}')">
Expand Down
2 changes: 1 addition & 1 deletion src/LivewireTablesServiceProvider.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Rappasoft\LivewireTables;
namespace Rappasoft\LaravelLivewireTables;

use Illuminate\Support\ServiceProvider;

Expand Down
18 changes: 9 additions & 9 deletions src/Http/Livewire/TableComponent.php → src/TableComponent.php
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
<?php

namespace Rappasoft\LivewireTables\Http\Livewire;
namespace Rappasoft\LaravelLivewireTables;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Str;
use Illuminate\View\View;
use Livewire\Component;
use Livewire\WithPagination;
use Rappasoft\LivewireTables\Http\Livewire\Traits\Checkboxes;
use Rappasoft\LivewireTables\Http\Livewire\Traits\Loading;
use Rappasoft\LivewireTables\Http\Livewire\Traits\Offline;
use Rappasoft\LivewireTables\Http\Livewire\Traits\Pagination;
use Rappasoft\LivewireTables\Http\Livewire\Traits\Search;
use Rappasoft\LivewireTables\Http\Livewire\Traits\Sorting;
use Rappasoft\LivewireTables\Http\Livewire\Traits\Table;
use Rappasoft\LivewireTables\Http\Livewire\Traits\Yajra;
use Rappasoft\LaravelLivewireTables\Traits\Checkboxes;
use Rappasoft\LaravelLivewireTables\Traits\Loading;
use Rappasoft\LaravelLivewireTables\Traits\Offline;
use Rappasoft\LaravelLivewireTables\Traits\Pagination;
use Rappasoft\LaravelLivewireTables\Traits\Search;
use Rappasoft\LaravelLivewireTables\Traits\Sorting;
use Rappasoft\LaravelLivewireTables\Traits\Table;
use Rappasoft\LaravelLivewireTables\Traits\Yajra;

/**
* Class TableComponent.
Expand Down
46 changes: 46 additions & 0 deletions src/Traits/CanBeHidden.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace Rappasoft\LaravelLivewireTables\Traits;

/**
* Trait CanBeHidden.
*/
trait CanBeHidden
{
/**
* @var bool
*/
protected $hidden = false;

/**
* @param $condition
*
* @return $this
*/
public function hideIf($condition): self
{
$this->hidden = $condition === true;

return $this;
}

/**
* @param bool $hidden
*
* @return $this
*/
public function hide($hidden = true): self
{
$this->hidden = $hidden;

return $this;
}

/**
* @return bool
*/
public function isHidden(): bool
{
return $this->hidden;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Rappasoft\LivewireTables\Http\Livewire\Traits;
namespace Rappasoft\LaravelLivewireTables\Traits;

/**
* Trait Checkboxes.
Expand Down
Loading

0 comments on commit 90d3dcd

Please sign in to comment.