Skip to content

Commit 16a254c

Browse files
committed
Merge branch 'develop'
2 parents 10ae01b + 4b7bb32 commit 16a254c

16 files changed

+738
-398
lines changed

CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Changelog
22

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

5-
## 1.0.0 - 2020-03-23
5+
## 1.0.0 - 2020-XX-XX
66

77
- Initial release

README.md

Lines changed: 237 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,263 @@
1-
# A Table Component for Laravel Livewire
1+
# A dynamic table component for Laravel Livewire
22

3-
[![Latest Version on Packagist](https://img.shields.io/packagist/v/rappasoft/livewire-tables.svg?style=flat-square)](https://packagist.org/packages/rappasoft/livewire-tables)
4-
![Run Tests](https://github.com/rappasoft/livewire-tables/workflows/Run%20Tests/badge.svg?branch=master)
3+
[![Latest Version on Packagist](https://img.shields.io/packagist/v/rappasoft/laravel-livewire-tables.svg?style=flat-square)](https://packagist.org/packages/rappasoft/laravel-livewire-tables)
54
[![StyleCI](https://styleci.io/repos/242222088/shield?style=plastic)](https://github.styleci.io/repos/242222088)
6-
[![Code Coverage](https://scrutinizer-ci.com/g/rappasoft/livewire-tables/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/rappasoft/livewire-tables/?branch=master)
7-
[![Quality Score](https://img.shields.io/scrutinizer/g/rappasoft/livewire-tables.svg?style=flat-square)](https://scrutinizer-ci.com/g/rappasoft/livewire-tables)
8-
[![Total Downloads](https://img.shields.io/packagist/dt/rappasoft/livewire-tables.svg?style=flat-square)](https://packagist.org/packages/rappasoft/livewire-tables)
5+
[![Total Downloads](https://img.shields.io/packagist/dt/rappasoft/laravel-livewire-tables.svg?style=flat-square)](https://packagist.org/packages/rappasoft/laravel-livewire-tables)
96

10-
A Laravel Livewire extension for data tables.
7+
A Laravel Livewire component for data tables.
8+
9+
**This package is currently in development and the source is constantly changing, use at your own risk.**
1110

1211
## Installation
1312

1413
You can install the package via composer:
1514

1615
``` bash
17-
composer require rappasoft/livewire-tables
16+
composer require rappasoft/laravel-livewire-tables
1817
```
1918

20-
## Version Compatibility
19+
## Usage
2120

22-
Laravel | Livewire Tables
23-
:---------|:----------
24-
7.x | 1.x
21+
### Creating Tables
2522

26-
## Publish
23+
To create a table component you can start with the below stub:
2724

28-
You can publish the configuration file to override the defaults:
25+
```
26+
<?php
27+
28+
namespace App\Http\Livewire;
29+
30+
use App\User;
31+
use Illuminate\Database\Eloquent\Builder;
32+
use Rappasoft\LivewireTables\Http\Livewire\Column;
33+
use Rappasoft\LivewireTables\Http\Livewire\TableComponent;
34+
35+
class UsersTable extends TableComponent
36+
{
37+
38+
public function query() : Builder
39+
{
40+
return User::with('role')
41+
->withCount('permissions');
42+
}
43+
44+
public function columns() : array
45+
{
46+
return [
47+
Column::make('ID')
48+
->searchable()
49+
->sortable(),
50+
Column::make('Name')
51+
->searchable()
52+
->sortable(),
53+
Column::make('E-mail', 'email')
54+
->searchable()
55+
->sortable(),
56+
Column::make('Role', 'role.name')
57+
->searchable()
58+
->sortable(),
59+
Column::make('Permissions', 'permissions_count')
60+
->sortable(),
61+
Column::make('Actions')
62+
->view('backend.auth.user.includes.actions'),
63+
];
64+
}
65+
}
2966
30-
``` bash
31-
php artisan vendor:publish --provider="Rappasoft\LivewireTables\LivewireTablesServiceProvider"
3267
```
3368

34-
## Usage
69+
Your component must implement two methods:
3570

71+
```
72+
/**
73+
* This defines the start of the query, usually Model::query() but can also eagar load relationships and counts.
74+
*/
75+
public function query() : Builder;
3676
77+
/**
78+
* This defines the columns of the table, they don't necessarily have to map to columns on the table.
79+
*/
80+
public function columns() : array;
81+
```
3782

38-
### Testing
83+
### Defining Columns
3984

40-
``` bash
41-
composer test
85+
You can define the columns of your table with the column class:
86+
87+
```
88+
Column::make('Name', 'column_name')
4289
```
4390

44-
### Changelog
91+
The first parameter is the name of the table header. The second parameter is the name of the table column. You can leave blank and the lowercase snake_case version will be used by default.
92+
93+
Here are a list of the column method you can chain to build your columns:
94+
95+
```
96+
/**
97+
* This column is searchable, with no callback it will search the column by name or by the supplied relationship, using a callback overrides the default searching functionality.
98+
*/
99+
public function searchable(callable $callable = null) : self;
100+
101+
/**
102+
* This column is sortable, with no callback it will sort the column by name and sort order defined on the components $sortDirection variable
103+
*/
104+
public function sortable(callable $callable = null) : self;
105+
106+
/**
107+
* The columns output will be put through {!! !!} instead of {{ }}.
108+
*/
109+
public function unescaped() : self;
110+
111+
/**
112+
* The columns output will be put through the Laravel HtmlString class.
113+
*/
114+
public function html() : self;
115+
116+
/**
117+
* This column will not look on the table for the column name, it will look on the model for the given attribute. Useful for custom attributes like getFullNameAttribute: Column::make('Full Name', 'full_name')->customAttribute()
118+
*/
119+
public function customAttribute() : self;
120+
121+
/**
122+
* This view will be used for the column, can still be used with sortable and searchable.
123+
*/
124+
public function view($view) : self;
125+
```
126+
127+
### Properties
128+
129+
You can override any of these in your table component:
130+
131+
#### Table
132+
133+
| Property | Default | Usage |
134+
| -------- | ------- | ----- |
135+
| $tableHeaderEnabled | true | Whether or not to display the table header |
136+
| $tableFooterEnabled | false | Whether or not to display the table footer |
137+
| $tableClass | table table-striped | The class to set on the table |
138+
| $tableHeaderClass | *none* | The class to set on the thead of the table |
139+
| $tableFooterClass | *none* | The class to set on the tfoot of the table |
140+
| $responsive | table-responsive | Tables wrapping div class |
141+
142+
#### Searching
143+
144+
| Property | Default | Usage |
145+
| -------- | ------- | ----- |
146+
| $searchEnabled | true | Whether or not searching is enabled |
147+
| $searchDebounce | 350 | Amount of time in ms to wait to send the search query and refresh the table |
148+
| $disableSearchOnLoading | true | Whether or not to disable the search bar when it is searching/loading new data |
149+
| $search | *none* | The initial search string |
150+
| $searchLabel | Search... | The placeholder for the search box |
151+
| $noResultsMessage | There are no results to display for this query. | The message to display when there are no results |
152+
153+
#### Sorting
154+
155+
| Property | Default | Usage |
156+
| -------- | ------- | ----- |
157+
| $sortField | id | The initial field to be sorting by |
158+
| $sortDirection | asc | The initial direction to sort |
159+
160+
#### Pagination
161+
162+
| Property | Default | Usage |
163+
| -------- | ------- | ----- |
164+
| $paginationEnabled | true | Displays per page and pagination links |
165+
| $perPageOptions | [10, 25, 50] | The options to limit the amount of results per page |
166+
| $perPage | 25 | Amount of items to show per page |
167+
| $perPageLabel | Per Page | The label for the per page filter |
168+
169+
#### Loading
170+
171+
| Property | Default | Usage |
172+
| -------- | ------- | ----- |
173+
| $loadingIndicator | false | Whether or not to show a loading indicator when searching |
174+
| $loadingMessage | Loading... | The loading message that gets displayed |
175+
176+
#### Offline
177+
178+
| Property | Default | Usage |
179+
| -------- | ------- | ----- |
180+
| $offlineIndicator | true | Whether or not to display an offline message when there is no connection |
181+
| $offlineMessage | You are not currently connected to the internet. | The message to display when offline |
182+
183+
#### Checkboxes
184+
185+
| Property | Default | Usage |
186+
| -------- | ------- | ----- |
187+
| $checkbox | false | Whether or not checkboxes are enabled |
188+
| $checkboxLocation | left | The side to put the checkboxes on |
189+
| $checkboxAttribute | id | The model attribute to bind to the checkbox array |
190+
| $checkboxAll | false | Whether or not all checkboxes are currently selected |
191+
| $checkboxValues | [] | The currently selected values of the checkboxes |
192+
193+
#### Other
194+
195+
| Property | Default | Usage |
196+
| -------- | ------- | ----- |
197+
| $wrapperClass | *none* | The classes applied to the wrapper div |
198+
| $refresh | false | Whether or not to refresh the table at a certain interval. false = off, If it's an integer it will be treated as milliseconds (2000 = refresh every 2 seconds), If it's a string it will call that function every 5 seconds.
199+
200+
### Table Methods
201+
202+
```
203+
/**
204+
* Used to set a class on a table header based on the column attribute
205+
*/
206+
public function setTableHeadClass($attribute) : ?string;
207+
208+
/**
209+
* Used to set a ID on a table header based on the column attribute
210+
*/
211+
public function setTableHeadId($attribute) : ?string;
212+
213+
/**
214+
* Used to set any attributes on a table header based on the column attribute
215+
* ['name' => 'my-custom-name', 'data-key' => 'my-custom-key']
216+
*/
217+
public function setTableHeadAttributes($attribute) : array;
218+
219+
/**
220+
* Used to set a class on a table row
221+
* You have the entre model of the row to work with
222+
*/
223+
public function setTableRowClass($model) : ?string;
224+
225+
/**
226+
* Used to set a ID on a table row
227+
* You have the entre model of the row to work with
228+
*/
229+
public function setTableRowId($model) : ?string;
230+
231+
/**
232+
* Used to set any attribute on a table row
233+
* You have the entre model of the row to work with
234+
* ['name' => 'my-custom-name', 'data-key' => 'my-custom-key']
235+
*/
236+
public function setTableRowAttributes($model) : array;
237+
238+
/**
239+
* Used to set the class of a table cell based on the column and the value of the cell
240+
*/
241+
public function setTableDataClass($attribute, $value) : ?string;
242+
243+
/**
244+
* Used to set the ID of a table cell based on the column and the value of the cell
245+
*/
246+
public function setTableDataId($attribute, $value) : ?string;
247+
248+
/**
249+
* Used to set any attributes of a table cell based on the column and the value of the cell
250+
* ['name' => 'my-custom-name', 'data-key' => 'my-custom-key']
251+
*/
252+
public function setTableDataAttributes($attribute, $value) : array;
253+
```
254+
255+
## Inspiration From:
256+
257+
- [https://github.com/kdion4891/laravel-livewire-tables](https://github.com/kdion4891/laravel-livewire-tables)
258+
- [https://github.com/yajra/laravel-datatables](https://github.com/yajra/laravel-datatables)
259+
260+
## Changelog
45261

46262
Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.
47263

composer.json

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
{
2-
"name": "rappasoft/livewire-tables",
3-
"description": "Datatables for Laravel Livewire",
2+
"name": "rappasoft/laravel-livewire-tables",
3+
"description": "A dynamic table component for Laravel Livewire",
44
"keywords": [
55
"rappasoft",
66
"livewire",
77
"datatables",
88
"tables",
99
"laravel"
1010
],
11-
"homepage": "https://github.com/rappasoft/livewire-tables",
11+
"homepage": "https://github.com/rappasoft/laravel-livewire-tables",
1212
"license": "MIT",
1313
"type": "library",
1414
"authors": [
@@ -19,12 +19,10 @@
1919
}
2020
],
2121
"require": {
22-
"php": "^7.2.5",
23-
"illuminate/support": "^6.0|^7.0"
22+
"php": "^7.2.5"
2423
},
2524
"require-dev": {
2625
"livewire/livewire": "^1.0",
27-
"orchestra/testbench": "^4.0|^5.0",
2826
"phpunit/phpunit": "^8.0|^9.0"
2927
},
3028
"autoload": {

config/livewire-tables.php

Lines changed: 0 additions & 6 deletions
This file was deleted.

resources/views/includes/_body.blade.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,21 @@
33
<tr><td colspan="{{ collect($columns)->count() }}">{{ $noResultsMessage }}</td></tr>
44
@else
55
@foreach($models as $model)
6-
<tr class="{{ $this->trClass($model) }}">
6+
<tr
7+
class="{{ $this->setTableRowClass($model) }}"
8+
id="{{ $this->setTableRowId($model) }}"
9+
@foreach ($this->setTableRowAttributes($model) as $key => $value) {{ $key . '="'.$value.'"' }} @endforeach
10+
>
711
@if($checkbox && $checkboxLocation === 'left')
812
@include('laravel-livewire-tables::includes._checkbox-row')
913
@endif
1014

1115
@foreach($columns as $column)
12-
<td class="{{ $this->tdClass($column->attribute, Arr::get($model->toArray(), $column->attribute)) }}">
16+
<td
17+
class="{{ $this->setTableDataClass($column->attribute, Arr::get($model->toArray(), $column->attribute)) }}"
18+
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
20+
>
1321
@if ($column->isView())
1422
@include($column->view)
1523
@else

resources/views/includes/_columns.blade.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33
@endif
44

55
@foreach($columns as $column)
6-
<th class="{{ $this->thClass($column->attribute) }}">
6+
<th
7+
class="{{ $this->setTableHeadClass($column->attribute) }}"
8+
id="{{ $this->setTableHeadId($column->attribute) }}"
9+
@foreach ($this->setTableHeadAttributes($column->attribute) as $key => $value) {{ $key . '="'.$value.'"' }} @endforeach
10+
>
711
@if($column->sortable)
812
<span style="cursor: pointer;" wire:click="sort('{{ $column->attribute }}')">
913
{{ $column->text }}

0 commit comments

Comments
 (0)