Skip to content

Commit 4a83f08

Browse files
author
Herbert Maschke
committed
Merge branch 'upstream/master' into introduce-summary-row
2 parents a2954e9 + c8fe8c8 commit 4a83f08

File tree

8 files changed

+241
-54
lines changed

8 files changed

+241
-54
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ somewhere in your CSS
5252
```html
5353
...
5454

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

5757
...
5858
```
@@ -62,11 +62,14 @@ somewhere in your CSS
6262
```html
6363
<livewire:datatable
6464
model="App\User"
65+
name="users"
6566
include="id, name, dob, created_at"
6667
dates="dob"
6768
/>
6869
```
6970

71+
- *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.
72+
7073
### Props
7174
| Property | Arguments | Result | Example |
7275
|----|----|----|----|
@@ -100,7 +103,6 @@ somewhere in your CSS
100103
101104
### Declare a public method ```columns``` that returns an array containing one or more ```Mediconesystems\LivewireDatatables\Column```
102105

103-
104106
## Columns
105107
Columns can be built using any of the static methods below, and then their attributes assigned using fluent method chains.
106108
There are additional specific types of Column; ```NumberColumn```, ```DateColumn```, ```TimeColumn```, using the correct one for your datatype will enable type-specific formatting and filtering:

resources/views/livewire/datatables/header-inline-hide.blade.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ class="w-32 hidden group-hover:inline-block absolute z-10 top-0 left-0 ml-3 bg-b
1313
<path stroke-miterlimit="10" d="M313.66 206.75H.5V1.49l157.65 204.9L313.66 1.49v205.26z" />
1414
</svg>
1515
</div>
16-
<div
17-
class="@if($column['hidden']) hidden @else relative h-12 overflow-hidden align-top flex table-cell @endif" @if (isset($column['width']))style="width:{{ $column['width'] }}"@endif>
16+
<div class="@if($column['hidden']) hidden @else relative h-12 overflow-hidden align-top flex table-cell @endif" @include('datatables::style-width')>
17+
1818
@if($column['unsortable'])
1919
<div class="w-full h-full px-6 py-3 border-b border-gray-200 bg-gray-50 text-xs leading-4 font-medium text-gray-500 uppercase tracking-wider flex justify-between items-center focus:outline-none">
2020
<span class="inline flex-grow @if($column['align'] === 'right') text-right @elseif($column['align'] === 'center') text-center @endif"">{{ str_replace('_', ' ', $column['label']) }}</span>

resources/views/livewire/datatables/header-no-hide.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
@unless($column['hidden'])
2-
<div class="relative table-cell h-12 overflow-hidden align-top" @if (isset($column['width']))style="width:{{ $column['width'] }}"@endif>
2+
<div class="relative table-cell h-12 overflow-hidden align-top" @include('datatables::style-width')>
33
@if($column['unsortable'])
44
<div class="w-full h-full px-6 py-3 border-b border-gray-200 bg-gray-50 text-left text-xs leading-4 font-medium text-gray-500 uppercase tracking-wider flex items-center focus:outline-none @if($column['align'] === 'right') justify-end @elseif($column['align'] === 'center') justify-center @endif">
55
<span class="inline ">{{ str_replace('_', ' ', $column['label']) }}</span>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
@if (isset($column['width']))style="width:{{ $column['width'] }};"@endif
2+
@if (isset($column['minWidth']))style="min-width:{{ $column['minWidth'] }};"@endif
3+
@if (isset($column['maxWidth']))style="max-width:{{ $column['maxWidth'] }};"@endif

src/Column.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44

55
use Illuminate\Support\Facades\DB;
66
use Illuminate\Support\Str;
7+
use Mediconesystems\LivewireDatatables\Http\Livewire\LivewireDatatable;
78

89
class Column
910
{
1011
public $type = 'string';
12+
public $index = 0;
1113
public $label;
1214
public $name;
1315
public $select;
@@ -31,6 +33,8 @@ class Column
3133
public $align = 'left';
3234
public $preventExport;
3335
public $width;
36+
public $minWidth;
37+
public $maxWidth;
3438
public $exportCallback;
3539

3640
/**
@@ -63,6 +67,18 @@ public static function name($name)
6367
return $column;
6468
}
6569

70+
public static function index(LivewireDatatable $datatable, $attribute = 'id')
71+
{
72+
$column = new static;
73+
$column->name = $attribute;
74+
$column->label = '#';
75+
$column->callback = function () use ($datatable) {
76+
return $datatable->page * $datatable->perPage - $datatable->perPage + $datatable->row++;
77+
};
78+
79+
return $column;
80+
}
81+
6682
public static function raw($raw)
6783
{
6884
$column = new static;
@@ -133,6 +149,13 @@ public function disableSummary()
133149
return $this;
134150
}
135151

152+
public function setIndex($index)
153+
{
154+
$this->index = $index;
155+
156+
return $this;
157+
}
158+
136159
public function sortBy($column)
137160
{
138161
$this->sort = $column;
@@ -408,4 +431,34 @@ public function width($width)
408431

409432
return $this;
410433
}
434+
435+
public function minWidth($minWidth)
436+
{
437+
if (preg_match('/^\\d*\\.?\\d+$/i', $minWidth) === 1) {
438+
$minWidth .= 'px';
439+
}
440+
441+
if (preg_match('/^(\\d*\\.?\\d+)\\s?(cm|mm|in|px|pt|pc|em|ex|ch|rem|vw|vmin|vmax|%+)$/i', $minWidth) === 0) {
442+
return $this;
443+
}
444+
445+
$this->minWidth = $minWidth;
446+
447+
return $this;
448+
}
449+
450+
public function maxWidth($maxWidth)
451+
{
452+
if (preg_match('/^\\d*\\.?\\d+$/i', $maxWidth) === 1) {
453+
$maxWidth .= 'px';
454+
}
455+
456+
if (preg_match('/^(\\d*\\.?\\d+)\\s?(cm|mm|in|px|pt|pc|em|ex|ch|rem|vw|vmin|vmax|%+)$/i', $maxWidth) === 0) {
457+
return $this;
458+
}
459+
460+
$this->maxWidth = $maxWidth;
461+
462+
return $this;
463+
}
411464
}

src/ColumnSet.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,27 @@ public static function fromModelInstance($model)
2626
return new static(
2727
collect($model->getAttributes())->keys()->reject(function ($name) use ($model) {
2828
return in_array($name, $model->getHidden());
29-
})->map(function ($attribute) {
30-
return Column::name($attribute);
29+
})->map(function ($attribute, $index) {
30+
return Column::name($attribute)->setIndex($index);
3131
})
3232
);
3333
}
3434

3535
public static function fromArray($columns)
3636
{
37-
return new static(collect($columns));
37+
return new static(collect(static::squeezeIndex($columns)));
38+
}
39+
40+
/**
41+
* Takes an array of columns and squeezes the consecutive index inside each element.
42+
*/
43+
public static function squeezeIndex($columns)
44+
{
45+
foreach ($columns as $index => $column) {
46+
$column->setIndex($index);
47+
}
48+
49+
return $columns;
3850
}
3951

4052
public function include($include)

0 commit comments

Comments
 (0)