Skip to content

Commit fd4e9e8

Browse files
committed
Improve ColumnDefs
1 parent d805449 commit fd4e9e8

File tree

5 files changed

+107
-7
lines changed

5 files changed

+107
-7
lines changed

src/Html/Column.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ public function orderable(bool $flag = true): static
140140
* @param string $name
141141
* @return static
142142
*/
143-
public static function make(array|string $data, string $name = ''): static
143+
public static function make(array|string $data = [], string $name = ''): static
144144
{
145145
$attr = $data;
146146
if (is_string($data)) {
@@ -404,7 +404,7 @@ public function orderSequence(array $value): static
404404
* @return $this
405405
* @see https://datatables.net/reference/option/columns.cellType
406406
*/
407-
public function cellType(string $value): static
407+
public function cellType(string $value = 'th'): static
408408
{
409409
$this->attributes['cellType'] = $value;
410410

src/Html/ColumnDefinition.php

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,54 @@
22

33
namespace Yajra\DataTables\Html;
44

5-
use Illuminate\Support\Fluent;
6-
7-
// TODO: Review class implementation
8-
class ColumnDefinition extends Fluent
5+
/**
6+
* @see https://datatables.net/reference/option/columnDefs
7+
*/
8+
class ColumnDefinition extends Column
99
{
10-
public function targets(array $value): static
10+
/**
11+
* @param array $attributes
12+
*/
13+
public function __construct($attributes = [])
14+
{
15+
parent::__construct($attributes);
16+
17+
$this->attributes = $attributes;
18+
}
19+
20+
/**
21+
* @param array|string|int $value
22+
* @return $this
23+
* @see https://datatables.net/reference/option/columnDefs.targets
24+
*/
25+
public function targets(array|string|int $value): static
1126
{
1227
$this->attributes['targets'] = $value;
1328

1429
return $this;
1530
}
31+
32+
/**
33+
* @param array $value
34+
* @return $this
35+
* @see https://datatables.net/reference/option/columns
36+
*/
37+
public function columns(array $value): static
38+
{
39+
$this->attributes['columns'] = $value;
40+
41+
return $this;
42+
}
43+
44+
/**
45+
* @return array
46+
*/
47+
public function toArray(): array
48+
{
49+
$array = parent::toArray();
50+
51+
unset($array['attributes']);
52+
53+
return $array;
54+
}
1655
}

src/Html/Options/HasColumns.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,14 @@ public function columnDefs(array|Arrayable|callable $value): static
3030
$value = $value->toArray();
3131
}
3232

33+
if (is_array($value)) {
34+
foreach ($value as $key => $def) {
35+
if ($def instanceof Arrayable) {
36+
$value[$key] = $def->toArray();
37+
}
38+
}
39+
}
40+
3341
$this->attributes['columnDefs'] = $value;
3442

3543
return $this;

tests/BuilderTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
use Yajra\DataTables\Html\Builder;
66
use Yajra\DataTables\Html\Column;
7+
use Yajra\DataTables\Html\ColumnDefinition;
8+
use Yajra\DataTables\Html\ColumnDefinitions;
79

810
class BuilderTest extends TestCase
911
{
@@ -184,4 +186,27 @@ public function it_can_add_action_column()
184186
$this->assertEquals(false, $column->exportable);
185187
$this->assertEquals(true, $column->printable);
186188
}
189+
190+
/** @test */
191+
public function it_has_column_defs()
192+
{
193+
$builder = $this->getHtmlBuilder();
194+
$builder->columnDefs([['targets' => '_all']]);
195+
196+
$this->assertEquals([['targets' => '_all']], $builder->getAttribute('columnDefs'));
197+
$this->assertCount(1, $builder->getAttribute('columnDefs'));
198+
199+
$builder->columnDefs([ColumnDefinition::make()->targets('_all')->visible()]);
200+
201+
$this->assertEquals([['targets' => '_all', 'visible' => true]], $builder->getAttribute('columnDefs'));
202+
$this->assertCount(1, $builder->getAttribute('columnDefs'));
203+
204+
$builder->addColumnDef(ColumnDefinition::make()->targets([1]));
205+
$this->assertEquals(['targets' => [1]], $builder->getAttribute('columnDefs')[1]);
206+
$this->assertCount(2, $builder->getAttribute('columnDefs'));
207+
208+
$builder->columnDefs(ColumnDefinitions::make()->push(ColumnDefinition::make()->targets(1)));
209+
$this->assertEquals([['targets' => 1]], $builder->getAttribute('columnDefs'));
210+
$this->assertCount(1, $builder->getAttribute('columnDefs'));
211+
}
187212
}

tests/ColumnDefinitionTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Yajra\DataTables\Html\Tests;
4+
5+
use Yajra\DataTables\Html\ColumnDefinition;
6+
7+
class ColumnDefinitionTest extends TestCase
8+
{
9+
/** @test */
10+
public function it_has_property_setters()
11+
{
12+
$def = ColumnDefinition::make()
13+
->targets([1])
14+
->columns([])
15+
->cellType()
16+
->className('my-class')
17+
->contentPadding('mmm')
18+
->createdCell('fn');
19+
20+
$this->assertEquals([1], $def->targets);
21+
$this->assertEquals([], $def->columns);
22+
$this->assertEquals('th', $def->cellType);
23+
$this->assertEquals('my-class', $def->className);
24+
$this->assertEquals('mmm', $def->contentPadding);
25+
$this->assertEquals('fn', $def->createdCell);
26+
}
27+
28+
}

0 commit comments

Comments
 (0)