From 5e2a42b2a032a7d3b1b54763bf2bdd598fadcd28 Mon Sep 17 00:00:00 2001 From: Joe <104938042+lrljoe@users.noreply.github.com> Date: Sat, 30 Nov 2024 04:03:18 +0000 Subject: [PATCH] Increment Column (#2096) * Add IncrementColumn with RowIndex, ColumnIndex --------- Co-authored-by: lrljoe --- docs/column-types/increment_column.md | 12 +++ docs/column-types/link_columns.md | 2 +- .../column-types/livewire_component_column.md | 2 +- docs/column-types/sum_column.md | 2 +- docs/column-types/view_component_column.md | 2 +- docs/column-types/wire_link_column.md | 2 +- resources/views/datatable.blade.php | 4 +- .../includes/columns/increment.blade.php | 2 + src/Views/Columns/IncrementColumn.php | 26 +++++++ .../Configuration/ColumnConfiguration.php | 22 ++++++ src/Views/Traits/Helpers/ColumnHelpers.php | 10 +++ src/Views/Traits/IsColumn.php | 4 + .../Views/Columns/IncrementColumnTest.php | 73 +++++++++++++++++++ 13 files changed, 156 insertions(+), 7 deletions(-) create mode 100644 docs/column-types/increment_column.md create mode 100644 resources/views/includes/columns/increment.blade.php create mode 100644 src/Views/Columns/IncrementColumn.php create mode 100644 tests/Unit/Views/Columns/IncrementColumnTest.php diff --git a/docs/column-types/increment_column.md b/docs/column-types/increment_column.md new file mode 100644 index 000000000..76ea7496c --- /dev/null +++ b/docs/column-types/increment_column.md @@ -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('#'), +``` \ No newline at end of file diff --git a/docs/column-types/link_columns.md b/docs/column-types/link_columns.md index 1e4aa7fa1..7c36ee077 100644 --- a/docs/column-types/link_columns.md +++ b/docs/column-types/link_columns.md @@ -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: diff --git a/docs/column-types/livewire_component_column.md b/docs/column-types/livewire_component_column.md index 6a6af2eea..ebeb72724 100644 --- a/docs/column-types/livewire_component_column.md +++ b/docs/column-types/livewire_component_column.md @@ -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. diff --git a/docs/column-types/sum_column.md b/docs/column-types/sum_column.md index b15288ef2..638818b23 100644 --- a/docs/column-types/sum_column.md +++ b/docs/column-types/sum_column.md @@ -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. diff --git a/docs/column-types/view_component_column.md b/docs/column-types/view_component_column.md index f3dd04276..5fdb4b46d 100644 --- a/docs/column-types/view_component_column.md +++ b/docs/column-types/view_component_column.md @@ -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. diff --git a/docs/column-types/wire_link_column.md b/docs/column-types/wire_link_column.md index 253544cd0..63c23e8a2 100644 --- a/docs/column-types/wire_link_column.md +++ b/docs/column-types/wire_link_column.md @@ -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 diff --git a/resources/views/datatable.blade.php b/resources/views/datatable.blade.php index 27ef1185f..7b90b7734 100644 --- a/resources/views/datatable.blade.php +++ b/resources/views/datatable.blade.php @@ -98,9 +98,9 @@ @foreach($this->selectedVisibleColumns as $colIndex => $column) @if($column->isHtml()) - {!! $column->renderContents($row) !!} + {!! $column->setIndexes($rowIndex, $colIndex)->renderContents($row) !!} @else - {{ $column->renderContents($row) }} + {{ $column->setIndexes($rowIndex, $colIndex)->renderContents($row) }} @endif @endforeach diff --git a/resources/views/includes/columns/increment.blade.php b/resources/views/includes/columns/increment.blade.php new file mode 100644 index 000000000..5ede69f09 --- /dev/null +++ b/resources/views/includes/columns/increment.blade.php @@ -0,0 +1,2 @@ +@aware(['rowIndex']) +
{{ $rowIndex+1 }}
diff --git a/src/Views/Columns/IncrementColumn.php b/src/Views/Columns/IncrementColumn.php new file mode 100644 index 000000000..4271fe721 --- /dev/null +++ b/src/Views/Columns/IncrementColumn.php @@ -0,0 +1,26 @@ +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)); + } +} diff --git a/src/Views/Traits/Configuration/ColumnConfiguration.php b/src/Views/Traits/Configuration/ColumnConfiguration.php index c46b01663..0115e74fe 100644 --- a/src/Views/Traits/Configuration/ColumnConfiguration.php +++ b/src/Views/Traits/Configuration/ColumnConfiguration.php @@ -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; + } } diff --git a/src/Views/Traits/Helpers/ColumnHelpers.php b/src/Views/Traits/Helpers/ColumnHelpers.php index da220287e..164de98da 100644 --- a/src/Views/Traits/Helpers/ColumnHelpers.php +++ b/src/Views/Traits/Helpers/ColumnHelpers.php @@ -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; + } } diff --git a/src/Views/Traits/IsColumn.php b/src/Views/Traits/IsColumn.php index b90a8b2e4..d36b3e06d 100644 --- a/src/Views/Traits/IsColumn.php +++ b/src/Views/Traits/IsColumn.php @@ -62,4 +62,8 @@ trait IsColumn protected bool $hasTableRowUrl = false; protected bool $isReorderColumn = false; + + protected ?int $columnIndex; + + protected ?int $rowIndex; } diff --git a/tests/Unit/Views/Columns/IncrementColumnTest.php b/tests/Unit/Views/Columns/IncrementColumnTest.php new file mode 100644 index 000000000..88302d164 --- /dev/null +++ b/tests/Unit/Views/Columns/IncrementColumnTest.php @@ -0,0 +1,73 @@ +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()); + + } +}