Skip to content

Commit 77ddf2e

Browse files
committed
Ability to use static data (no server-side)
1 parent 3b3731b commit 77ddf2e

File tree

5 files changed

+213
-0
lines changed

5 files changed

+213
-0
lines changed

src/Html/Builder.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ class Builder
4646
*/
4747
public Collection $collection;
4848

49+
/**
50+
* @var Collection<int, array>
51+
*/
52+
public Collection $rows;
53+
4954
/**
5055
* @var array<string, string|null>
5156
*/
@@ -68,6 +73,7 @@ public function __construct(public Repository $config, public Factory $view, pub
6873
$defaults = $this->config->get('datatables-html.table', []);
6974

7075
$this->collection = new Collection;
76+
$this->rows = new Collection;
7177
$this->tableAttributes = $defaults;
7278
}
7379

@@ -179,6 +185,18 @@ public function table(array $attributes = [], bool $drawFooter = false, bool $dr
179185
$tableHtml .= '<thead'.($this->theadClass ?? '').'>';
180186
$tableHtml .= '<tr>'.implode('', $th).'</tr>'.$searchHtml.'</thead>';
181187

188+
if ($this->attributes['serverSide'] === false) {
189+
$tableHtml .= '<tbody>';
190+
foreach ($this->getRows() as $row) {
191+
$tableHtml .= '<tr>';
192+
foreach ($this->getColumns() as $column) {
193+
$tableHtml .= '<td>'.$row->getCellContentForColumn($column).'</td>';
194+
}
195+
$tableHtml .= '</tr>';
196+
}
197+
$tableHtml .= '</tbody>';
198+
}
199+
182200
if ($drawFooter) {
183201
$tf = $this->compileTableFooter();
184202
$tableHtml .= '<tfoot><tr>'.implode('', $tf).'</tr></tfoot>';

src/Html/Cell.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace Yajra\DataTables\Html;
4+
5+
use Illuminate\Support\Fluent;
6+
7+
/**
8+
* @property string $column
9+
* @property string $content
10+
*/
11+
class Cell extends Fluent
12+
{
13+
/**
14+
* @param array $attributes
15+
*/
16+
public function __construct(array $attributes = [])
17+
{
18+
$attributes['attributes'] ??= [];
19+
20+
parent::__construct($attributes);
21+
}
22+
23+
/**
24+
* Make a new column instance.
25+
*/
26+
public static function make(array|string $data = [], string $content = ''): static
27+
{
28+
$attributes = $data;
29+
30+
if (is_string($data)) {
31+
$attributes = [
32+
'column' => $data,
33+
'content' => $content,
34+
];
35+
}
36+
37+
return new static($attributes);
38+
}
39+
40+
/**
41+
* @return $this
42+
*/
43+
public function column(string $value): static
44+
{
45+
$this->attributes['column'] = $value;
46+
47+
return $this;
48+
}
49+
50+
/**
51+
* @return $this
52+
*/
53+
public function content(string $value): static
54+
{
55+
$this->attributes['content'] = $value;
56+
57+
return $this;
58+
}
59+
}

src/Html/HasOptions.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ trait HasOptions
1212
use Options\HasAjax;
1313
use Options\HasCallbacks;
1414
use Options\HasColumns;
15+
use Options\HasRows;
1516
use Options\HasFeatures;
1617
use Options\HasInternationalisation;
1718
use Options\Plugins\AutoFill;

src/Html/Options/HasRows.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
namespace Yajra\DataTables\Html\Options;
4+
5+
use Illuminate\Contracts\Support\Arrayable;
6+
use Illuminate\Support\Collection;
7+
use Yajra\DataTables\Html\Row;
8+
9+
/**
10+
* DataTables - Row option builder.
11+
*/
12+
trait HasRows
13+
{
14+
/**
15+
* Set columns option value.
16+
*
17+
* @return $this
18+
*
19+
* @see https://datatables.net/reference/option/columns
20+
*/
21+
public function rows(array $rows): static
22+
{
23+
$this->rows = new Collection;
24+
25+
foreach ($rows as $key => $value) {
26+
if (! is_a($value, Row::class)) {
27+
if (array_key_exists('cells', $value)) {
28+
$cells = $value['cells'];
29+
$attributes = $value['attributes'] ?? [];
30+
} else {
31+
$cells = $value;
32+
$attributes = [];
33+
}
34+
35+
$this->rows->push(new Row($attributes, $cells));
36+
} else {
37+
$this->rows->push($value);
38+
}
39+
}
40+
41+
return $this;
42+
}
43+
44+
public function setRows(Collection|array $rows): static
45+
{
46+
$this->rows = collect($rows);
47+
48+
return $this;
49+
}
50+
51+
/**
52+
* Get collection of rows.
53+
*
54+
* @return \Illuminate\Support\Collection<array-key, array>
55+
*/
56+
public function getRows(): Collection
57+
{
58+
return $this->rows;
59+
}
60+
}

src/Html/Row.php

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
namespace Yajra\DataTables\Html;
4+
5+
use Illuminate\Support\Fluent;
6+
use Illuminate\Support\Collection;
7+
8+
/**
9+
* @property string $content
10+
* @method content($content)
11+
*/
12+
class Row extends Fluent
13+
{
14+
public function __construct(array $attributes = [], array $cells = [])
15+
{
16+
$attributes['attributes'] ??= [];
17+
18+
$this->buildCells($cells);
19+
20+
parent::__construct($attributes);
21+
}
22+
23+
/**
24+
* Make a new column instance.
25+
*/
26+
public static function make(array $attributes = [], array $cells = []): static
27+
{
28+
return new static($attributes, $cells);
29+
}
30+
31+
public function cells($cells): static
32+
{
33+
$this->cells = collect($cells);
34+
35+
return $this;
36+
}
37+
38+
protected function buildCells(array $cells): static
39+
{
40+
$this->cells = new Collection;
41+
42+
foreach ($cells as $key => $value) {
43+
if (! is_a($value, Cell::class)) {
44+
if (is_array($value)) {
45+
$cellAttributes = array_merge($value, [
46+
'column' => $value['column'] ?? $key,
47+
'content' => $value['content'] ?? null,
48+
]);
49+
} else {
50+
$cellAttributes = [
51+
'column' => $key,
52+
'content' => $value,
53+
];
54+
}
55+
56+
$this->cells->push(new Cell($cellAttributes));
57+
} else {
58+
$this->cells->push($value);
59+
}
60+
}
61+
62+
$this->cells = $this->cells->keyBy('column');
63+
64+
return $this;
65+
}
66+
67+
public function getCellContentForColumn(Column $column): mixed
68+
{
69+
if ($this->cells->has($column->data)) {
70+
return $this->cells->get($column->data)->content;
71+
}
72+
73+
return null;
74+
}
75+
}

0 commit comments

Comments
 (0)