Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add increment column (BETA) #2028

Closed
Closed
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
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 {!! count($attributes) ? $column->arrayToAttributes($attributes) : '' !!}>{{ $rowIndex+1 }}</div>
28 changes: 28 additions & 0 deletions src/Views/Columns/IncrementColumn.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?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)
->withIsTailwind($this->isTailwind())
->withIsBootstrap($this->isBootstrap())
->withAttributes($this->hasAttributesCallback() ? app()->call($this->getAttributesCallback(), ['row' => $row]) : []);
}
}
46 changes: 46 additions & 0 deletions tests/Traits/Visuals/Columns/IncrementColumnVisualsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace Rappasoft\LaravelLivewireTables\Tests\Traits\Visuals\Columns;

use Exception;
use Illuminate\View\ViewException;
use Livewire\Livewire;
use Rappasoft\LaravelLivewireTables\Exceptions\DataTableConfigurationException;
use Rappasoft\LaravelLivewireTables\Tests\Http\Livewire\FailingTables\{BrokenSecondaryHeaderTable, NoBuildMethodTable, NoPrimaryKeyTable};
use Rappasoft\LaravelLivewireTables\Tests\Http\Livewire\{PetsTable,PetsTableAttributes};
use Rappasoft\LaravelLivewireTables\Tests\TestCase;

final class IncrementColumnVisualsTest extends TestCase
{
private $testErrors;

/*
public function test_increment_column_renders_correctly(): void
{
Livewire::test(new class extends PetsTable
{
public function configure(): void
{
$this->setPrimaryKey('id');
}

public function columns(): array
{
return [
\Rappasoft\LaravelLivewireTables\Views\Columns\IncrementColumn::make('#'),
\Rappasoft\LaravelLivewireTables\Views\Column::make('Name')->searchable(),
];
}

public function filters(): array
{
return [];
}
})
->assertSeeHtmlInOrder([
'<tr rowpk="1"',
'<td',
'<div>1</div>',
]);
}*/
}
39 changes: 39 additions & 0 deletions tests/Views/Columns/IncrementColumnTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Rappasoft\LaravelLivewireTables\Tests\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_renders_correctly(): void
{
$rows = $this->basicTable->getRows();
$row1 = $rows->first();

}
}