Skip to content

Commit 5e2a42b

Browse files
authored
Increment Column (rappasoft#2096)
* Add IncrementColumn with RowIndex, ColumnIndex --------- Co-authored-by: lrljoe <[email protected]>
1 parent e6e7f29 commit 5e2a42b

File tree

13 files changed

+156
-7
lines changed

13 files changed

+156
-7
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
title: Increment Column (beta)
3+
weight: 12
4+
---
5+
6+
The IncrementColumn provides an easy way to display the row's index in the loop.
7+
8+
Please note - this is not linked to the row's primary key!
9+
10+
```php
11+
IncrementColumn::make('#'),
12+
```

docs/column-types/link_columns.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: Link Columns
3-
weight: 12
3+
weight: 13
44
---
55

66
Link columns provide a way to display HTML links in your table without having to use `format()` or partial views:

docs/column-types/livewire_component_column.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: Livewire Component (beta)
3-
weight: 13
3+
weight: 14
44
---
55

66
Livewire Component Columns allow for the use of a Livewire Component as a Column.

docs/column-types/sum_column.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: Sum Columns (beta)
3-
weight: 14
3+
weight: 15
44
---
55

66
Sum columns provide an easy way to display the "Sum" of a field on a relation.

docs/column-types/view_component_column.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: View Component Columns
3-
weight: 15
3+
weight: 16
44
---
55

66
View Component columns let you specify a component name and attributes and provide attributes to the View Component. This will render the View Component in it's entirety.

docs/column-types/wire_link_column.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: Wire Link Column (beta)
3-
weight: 16
3+
weight: 17
44
---
55

66
WireLink columns provide a way to display Wired Links in your table without having to use `format()` or partial views, with or without a Confirmation Message

resources/views/datatable.blade.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,9 @@
9898
@foreach($this->selectedVisibleColumns as $colIndex => $column)
9999
<x-livewire-tables::table.td wire:key="{{ $tableName . '-' . $row->{$primaryKey} . '-datatable-td-' . $column->getSlug() }}" :column="$column" :colIndex="$colIndex">
100100
@if($column->isHtml())
101-
{!! $column->renderContents($row) !!}
101+
{!! $column->setIndexes($rowIndex, $colIndex)->renderContents($row) !!}
102102
@else
103-
{{ $column->renderContents($row) }}
103+
{{ $column->setIndexes($rowIndex, $colIndex)->renderContents($row) }}
104104
@endif
105105
</x-livewire-tables::table.td>
106106
@endforeach
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
@aware(['rowIndex'])
2+
<div {{ $attributeBag }}>{{ $rowIndex+1 }}</div>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Rappasoft\LaravelLivewireTables\Views\Columns;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
use Rappasoft\LaravelLivewireTables\Exceptions\DataTableConfigurationException;
7+
use Rappasoft\LaravelLivewireTables\Views\Column;
8+
9+
class IncrementColumn extends Column
10+
{
11+
protected string $view = 'livewire-tables::includes.columns.increment';
12+
13+
public function __construct(string $title, ?string $from = null)
14+
{
15+
parent::__construct($title, $from);
16+
$this->label(fn () => null);
17+
18+
}
19+
20+
public function getContents(Model $row): null|string|\Illuminate\Support\HtmlString|DataTableConfigurationException|\Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
21+
{
22+
return view($this->getView())
23+
->withColumn($this)
24+
->withAttributeBag($this->getAttributeBag($row));
25+
}
26+
}

src/Views/Traits/Configuration/ColumnConfiguration.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,26 @@ public function setIsReorderColumn(bool $isReorderColumn): self
9797

9898
return $this;
9999
}
100+
101+
public function setIndexes(int $rowIndex, int $columnIndex): self
102+
{
103+
$this->setRowIndex($rowIndex);
104+
$this->setColumnIndex($columnIndex);
105+
106+
return $this;
107+
}
108+
109+
public function setColumnIndex(int $columnIndex): self
110+
{
111+
$this->columnIndex = $columnIndex;
112+
113+
return $this;
114+
}
115+
116+
public function setRowIndex(int $rowIndex): self
117+
{
118+
$this->rowIndex = $rowIndex;
119+
120+
return $this;
121+
}
100122
}

0 commit comments

Comments
 (0)