Skip to content

Commit

Permalink
Increment Column (#2096)
Browse files Browse the repository at this point in the history
* Add IncrementColumn with RowIndex, ColumnIndex

---------

Co-authored-by: lrljoe <[email protected]>
  • Loading branch information
lrljoe and lrljoe authored Nov 30, 2024
1 parent e6e7f29 commit 5e2a42b
Show file tree
Hide file tree
Showing 13 changed files with 156 additions and 7 deletions.
12 changes: 12 additions & 0 deletions docs/column-types/increment_column.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
title: Increment Column (beta)
weight: 12
---

The IncrementColumn provides an easy way to display the row's index in the loop.

Please note - this is not linked to the row's primary key!

```php
IncrementColumn::make('#'),
```
2 changes: 1 addition & 1 deletion docs/column-types/link_columns.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: Link Columns
weight: 12
weight: 13
---

Link columns provide a way to display HTML links in your table without having to use `format()` or partial views:
Expand Down
2 changes: 1 addition & 1 deletion docs/column-types/livewire_component_column.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: Livewire Component (beta)
weight: 13
weight: 14
---

Livewire Component Columns allow for the use of a Livewire Component as a Column.
Expand Down
2 changes: 1 addition & 1 deletion docs/column-types/sum_column.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: Sum Columns (beta)
weight: 14
weight: 15
---

Sum columns provide an easy way to display the "Sum" of a field on a relation.
Expand Down
2 changes: 1 addition & 1 deletion docs/column-types/view_component_column.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: View Component Columns
weight: 15
weight: 16
---

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.
Expand Down
2 changes: 1 addition & 1 deletion docs/column-types/wire_link_column.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: Wire Link Column (beta)
weight: 16
weight: 17
---

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
Expand Down
4 changes: 2 additions & 2 deletions resources/views/datatable.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,9 @@
@foreach($this->selectedVisibleColumns as $colIndex => $column)
<x-livewire-tables::table.td wire:key="{{ $tableName . '-' . $row->{$primaryKey} . '-datatable-td-' . $column->getSlug() }}" :column="$column" :colIndex="$colIndex">
@if($column->isHtml())
{!! $column->renderContents($row) !!}
{!! $column->setIndexes($rowIndex, $colIndex)->renderContents($row) !!}
@else
{{ $column->renderContents($row) }}
{{ $column->setIndexes($rowIndex, $colIndex)->renderContents($row) }}
@endif
</x-livewire-tables::table.td>
@endforeach
Expand Down
2 changes: 2 additions & 0 deletions resources/views/includes/columns/increment.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@aware(['rowIndex'])
<div {{ $attributeBag }}>{{ $rowIndex+1 }}</div>
26 changes: 26 additions & 0 deletions src/Views/Columns/IncrementColumn.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Rappasoft\LaravelLivewireTables\Views\Columns;

use Illuminate\Database\Eloquent\Model;
use Rappasoft\LaravelLivewireTables\Exceptions\DataTableConfigurationException;
use Rappasoft\LaravelLivewireTables\Views\Column;

class IncrementColumn extends Column
{
protected string $view = 'livewire-tables::includes.columns.increment';

public function __construct(string $title, ?string $from = null)
{
parent::__construct($title, $from);
$this->label(fn () => null);

}

public function getContents(Model $row): null|string|\Illuminate\Support\HtmlString|DataTableConfigurationException|\Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
{
return view($this->getView())
->withColumn($this)
->withAttributeBag($this->getAttributeBag($row));
}
}
22 changes: 22 additions & 0 deletions src/Views/Traits/Configuration/ColumnConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,26 @@ public function setIsReorderColumn(bool $isReorderColumn): self

return $this;
}

public function setIndexes(int $rowIndex, int $columnIndex): self
{
$this->setRowIndex($rowIndex);
$this->setColumnIndex($columnIndex);

return $this;
}

public function setColumnIndex(int $columnIndex): self
{
$this->columnIndex = $columnIndex;

return $this;
}

public function setRowIndex(int $rowIndex): self
{
$this->rowIndex = $rowIndex;

return $this;
}
}
10 changes: 10 additions & 0 deletions src/Views/Traits/Helpers/ColumnHelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,4 +215,14 @@ public function getIsReorderColumn(): bool
{
return $this->isReorderColumn;
}

public function getColumnIndex(): int
{
return $this->columnIndex;
}

public function getRowIndex(): int
{
return $this->rowIndex;
}
}
4 changes: 4 additions & 0 deletions src/Views/Traits/IsColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,8 @@ trait IsColumn
protected bool $hasTableRowUrl = false;

protected bool $isReorderColumn = false;

protected ?int $columnIndex;

protected ?int $rowIndex;
}
73 changes: 73 additions & 0 deletions tests/Unit/Views/Columns/IncrementColumnTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace Rappasoft\LaravelLivewireTables\Tests\Unit\Views\Columns;

use Rappasoft\LaravelLivewireTables\Exceptions\DataTableConfigurationException;
use Rappasoft\LaravelLivewireTables\Tests\Models\Pet;
use Rappasoft\LaravelLivewireTables\Tests\TestCase;
use Rappasoft\LaravelLivewireTables\Views\Columns\IncrementColumn;

final class IncrementColumnTest extends TestCase
{
public function test_can_set_the_column_title(): void
{
$column = IncrementColumn::make('Name', 'name');

$this->assertSame('Name', $column->getTitle());
}

public function test_can_not_infer_field_name_from_title_if_no_from(): void
{
$column = IncrementColumn::make('My Title');

$this->assertNull($column->getField());
}

public function test_can_not_render_field_if_no_title(): void
{
$this->expectException(\ArgumentCountError::class);

IncrementColumn::make()->getContents(Pet::find(1));
}

public function test_handles_row_index_correctly(): void
{
$rows = $this->basicTable->getRows();
$row1 = $rows->first();
$col = IncrementColumn::make('#')->setRowIndex(1);
$contents = $col->getContents($row1);
$this->assertSame(1, $col->getRowIndex());

}

public function test_handles_col_index_correctly(): void
{
$rows = $this->basicTable->getRows();
$row1 = $rows->first();
$col = IncrementColumn::make('#')->setColumnIndex(2);
$contents = $col->getContents($row1);
$this->assertSame(2, $col->getColumnIndex());

}

public function test_handles_indexes_correctly(): void
{
$rows = $this->basicTable->getRows();
$row1 = $rows->first();
$col = IncrementColumn::make('#')->setIndexes(5, 3);
$contents = $col->getContents($row1);
$this->assertSame(5, $col->getRowIndex());
$this->assertSame(3, $col->getColumnIndex());

}

public function test_renders_correctly(): void
{
$rows = $this->basicTable->getRows();
$row1 = $rows->first();
$col = IncrementColumn::make('#')->setRowIndex(1);
$contents = $col->getContents($row1);
$this->assertSame(1, $col->getRowIndex());

}
}

0 comments on commit 5e2a42b

Please sign in to comment.