Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ somewhere in your CSS
```html
...

<livewire:datatable model="App\User" />
<livewire:datatable model="App\User" name="all-my-users" />

...
```
Expand All @@ -62,11 +62,14 @@ somewhere in your CSS
```html
<livewire:datatable
model="App\User"
name="users"
include="id, name, dob, created_at"
dates="dob"
/>
```

- *Attention*: Please note that having multiple datatables on the same page _or_ more than one datatable of the same type on different pages needs to have a unique `name` attribute assigned to each one so they do not conflict with each other as in the example above.

### Props
| Property | Arguments | Result | Example |
|----|----|----|----|
Expand Down Expand Up @@ -100,7 +103,6 @@ somewhere in your CSS

### Declare a public method ```columns``` that returns an array containing one or more ```Mediconesystems\LivewireDatatables\Column```


## Columns
Columns can be built using any of the static methods below, and then their attributes assigned using fluent method chains.
There are additional specific types of Column; ```NumberColumn```, ```DateColumn```, ```TimeColumn```, using the correct one for your datatype will enable type-specific formatting and filtering:
Expand Down
82 changes: 67 additions & 15 deletions src/Http/Livewire/LivewireDatatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ class LivewireDatatable extends Component
public $name;
public $columnGroups = [];
public $userFilter;
public $persistComplexQuery;
public $persistSearch = true;
public $persistComplexQuery = true;
public $persistHiddenColumns = true;
public $persistSort = true;
public $persistPerPage = true;
Expand Down Expand Up @@ -127,7 +128,18 @@ public function applyToTable($options)
}
}

foreach (['perPage', 'search', 'activeSelectFilters', 'activeDateFilters', 'activeTimeFilters', 'activeBooleanFilters', 'activeTextFilters', 'activeNumberFilters', 'hide', 'selected'] as $property) {
foreach ([
'perPage',
'search',
'activeSelectFilters',
'activeDateFilters',
'activeTimeFilters',
'activeBooleanFilters',
'activeTextFilters',
'activeNumberFilters',
'hide',
'selected',
] as $property) {
if (isset($options[$property])) {
$this->$property = $options[$property];
}
Expand Down Expand Up @@ -175,21 +187,47 @@ public function mount(
$afterTableSlot = false,
$params = []
) {
foreach (['model', 'include', 'exclude', 'hide', 'dates', 'times', 'searchable', 'sort', 'hideHeader', 'hidePagination', 'exportable', 'hideable', 'beforeTableSlot', 'afterTableSlot'] as $property) {
foreach ([
'model',
'include',
'exclude',
'hide',
'dates',
'times',
'searchable',
'sort',
'hideHeader',
'hidePagination',
'exportable',
'hideable',
'beforeTableSlot',
'afterTableSlot',
] as $property) {
$this->$property = $this->$property ?? $$property;
}

$this->params = $params;

$this->columns = $this->getViewColumns();

$this->initialiseSearch();
$this->initialiseSort();
$this->initialiseHiddenColumns();
$this->initialiseFilters();
$this->initialisePerPage();
$this->initialiseColumnGroups();
}

// save settings
public function dehydrate()
{
if ($this->persistSearch) {
session()->put($this->sessionStorageKey() . '_search', $this->search);
}

return parent::dehydrate(); // @phpstan-ignore-line
}

public function columns()
{
return $this->modelInstance;
Expand Down Expand Up @@ -465,7 +503,7 @@ public function getFreshColumnsProperty()

public function sessionStorageKey()
{
return Str::snake(Str::afterLast(get_called_class(), '\\'));
return Str::snake(Str::afterLast(get_called_class(), '\\')) . $this->name;
}

public function getSessionStoredSort()
Expand All @@ -474,8 +512,8 @@ public function getSessionStoredSort()
return;
}

$this->sort = session()->get($this->sessionStorageKey() . $this->name . '_sort', $this->sort);
$this->direction = session()->get($this->sessionStorageKey() . $this->name . '_direction', $this->direction);
$this->sort = session()->get($this->sessionStorageKey() . '_sort', $this->sort);
$this->direction = session()->get($this->sessionStorageKey() . '_direction', $this->direction);
}

public function getSessionStoredPerPage()
Expand All @@ -493,7 +531,10 @@ public function setSessionStoredSort()
return;
}

session()->put([$this->sessionStorageKey() . $this->name . '_sort' => $this->sort, $this->sessionStorageKey() . $this->name . '_direction' => $this->direction]);
session()->put([
$this->sessionStorageKey() . '_sort' => $this->sort,
$this->sessionStorageKey() . '_direction' => $this->direction,
]);
}

public function setSessionStoredFilters()
Expand All @@ -503,7 +544,7 @@ public function setSessionStoredFilters()
}

session()->put([
$this->sessionStorageKey() . $this->name . '_filter' => [
$this->sessionStorageKey() . '_filter' => [
'text' => $this->activeTextFilters,
'boolean' => $this->activeBooleanFilters,
'select' => $this->activeSelectFilters,
Expand All @@ -514,6 +555,15 @@ public function setSessionStoredFilters()
]);
}

public function initialiseSearch()
{
if (! $this->persistSearch) {
return;
}

$this->search = session()->get($this->sessionStorageKey() . '_search', $this->search);
}

public function initialiseSort()
{
$this->sort = $this->defaultSort()
Expand All @@ -532,9 +582,9 @@ public function initialiseHiddenColumns()
return;
}

if (session()->has($this->sessionStorageKey() . $this->name . '_hidden_columns')) {
if (session()->has($this->sessionStorageKey() . '_hidden_columns')) {
$this->columns = collect($this->columns)->map(function ($column, $index) {
$column['hidden'] = in_array($index, session()->get($this->sessionStorageKey() . $this->name . '_hidden_columns'));
$column['hidden'] = in_array($index, session()->get($this->sessionStorageKey() . '_hidden_columns'));

return $column;
})->toArray();
Expand Down Expand Up @@ -565,7 +615,7 @@ public function initialiseFilters()
return;
}

$filters = session()->get($this->sessionStorageKey() . $this->name . '_filter');
$filters = session()->get($this->sessionStorageKey() . '_filter');

$this->activeBooleanFilters = $filters['boolean'] ?? [];
$this->activeSelectFilters = $filters['select'] ?? [];
Expand Down Expand Up @@ -651,8 +701,10 @@ public function sort($index, $direction = null)
}
$this->page = 1;

$key = Str::snake(Str::afterLast(get_called_class(), '\\'));
session()->put([$key . $this->name . '_sort' => $this->sort, $key . $this->name . '_direction' => $this->direction]);
session()->put([
$this->sessionStorageKey() . '_sort' => $this->sort,
$this->sessionStorageKey() . '_direction' => $this->direction,
]);
}

public function toggle($index)
Expand All @@ -670,7 +722,7 @@ public function toggle($index)
if ($this->persistHiddenColumns) {
$hidden = collect($this->columns)->filter->hidden->keys()->toArray();

session()->put([$this->sessionStorageKey() . $this->name . '_hidden_columns' => $hidden]);
session()->put([$this->sessionStorageKey() . '_hidden_columns' => $hidden]);
}
}

Expand Down Expand Up @@ -1472,7 +1524,7 @@ public function render()
$this->emit('refreshDynamic');

if ($this->persistPerPage) {
session()->put([$this->sessionStorageKey() . $this->name . '_perpage' => $this->perPage]);
session()->put([$this->sessionStorageKey() . '_perpage' => $this->perPage]);
}

return view('datatables::datatable')->layoutData(['title' => $this->title]);
Expand Down