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

Ability to use static data (no server-side) #221

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
18 changes: 18 additions & 0 deletions src/Html/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@
*/
public Collection $collection;

/**
* @var Collection<int, Row>
*/
public Collection $rows;

/**
* @var array<string, string|null>
*/
Expand All @@ -68,6 +73,7 @@
$defaults = $this->config->get('datatables-html.table', []);

$this->collection = new Collection;
$this->rows = new Collection;
$this->tableAttributes = $defaults;
}

Expand Down Expand Up @@ -179,6 +185,18 @@
$tableHtml .= '<thead'.($this->theadClass ?? '').'>';
$tableHtml .= '<tr>'.implode('', $th).'</tr>'.$searchHtml.'</thead>';

if ($this->attributes['serverSide'] === false) {
$tableHtml .= '<tbody>';
foreach ($this->getRows() as $row) {
$tableHtml .= '<tr>';
foreach ($this->getColumns() as $column) {
$tableHtml .= '<td>'.$row->getCellContentForColumn($column).'</td>';

Check failure on line 193 in src/Html/Builder.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (8.2, prefer-stable)

Cannot call method getCellContentForColumn() on array.

Check failure on line 193 in src/Html/Builder.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (8.3, prefer-stable)

Cannot call method getCellContentForColumn() on array.
}
$tableHtml .= '</tr>';
}
$tableHtml .= '</tbody>';
}

if ($drawFooter) {
$tf = $this->compileTableFooter();
$tableHtml .= '<tfoot><tr>'.implode('', $tf).'</tr></tfoot>';
Expand Down
59 changes: 59 additions & 0 deletions src/Html/Cell.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace Yajra\DataTables\Html;

use Illuminate\Support\Fluent;

/**
* @property string $column
* @property string $content
*/
class Cell extends Fluent
{
/**
* @param array $attributes
*/
public function __construct(array $attributes = [])
{
$attributes['attributes'] ??= [];

parent::__construct($attributes);
}

/**
* Make a new column instance.
*/
public static function make(array|string $data = [], string $content = ''): static
{
$attributes = $data;

if (is_string($data)) {
$attributes = [
'column' => $data,
'content' => $content,
];
}

return new static($attributes);
}

/**
* @return $this
*/
public function column(string $value): static
{
$this->attributes['column'] = $value;

return $this;
}

/**
* @return $this
*/
public function content(string $value): static
{
$this->attributes['content'] = $value;

return $this;
}
}
1 change: 1 addition & 0 deletions src/Html/HasOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ trait HasOptions
use Options\HasAjax;
use Options\HasCallbacks;
use Options\HasColumns;
use Options\HasRows;
use Options\HasFeatures;
use Options\HasInternationalisation;
use Options\Plugins\AutoFill;
Expand Down
60 changes: 60 additions & 0 deletions src/Html/Options/HasRows.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace Yajra\DataTables\Html\Options;

use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Support\Collection;
use Yajra\DataTables\Html\Row;

/**
* DataTables - Row option builder.
*/
trait HasRows
{
/**
* Set columns option value.
*
* @return $this
*
* @see https://datatables.net/reference/option/columns
*/
public function rows(array $rows): static
{
$this->rows = new Collection;

foreach ($rows as $key => $value) {
if (! is_a($value, Row::class)) {
if (array_key_exists('cells', $value)) {
$cells = $value['cells'];
$attributes = $value['attributes'] ?? [];
} else {
$cells = $value;
$attributes = [];
}

$this->rows->push(new Row($attributes, $cells));
} else {
$this->rows->push($value);
}
}

return $this;
}

public function setRows(Collection|array $rows): static
{
$this->rows = collect($rows);

return $this;
}

/**
* Get collection of rows.
*
* @return \Illuminate\Support\Collection<array-key, array>
*/
public function getRows(): Collection
{
return $this->rows;
}
}
75 changes: 75 additions & 0 deletions src/Html/Row.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace Yajra\DataTables\Html;

use Illuminate\Support\Fluent;
use Illuminate\Support\Collection;

/**
* @property string $content
* @method content($content)
*/
class Row extends Fluent
{
public function __construct(array $attributes = [], array $cells = [])
{
$attributes['attributes'] ??= [];

$this->buildCells($cells);

parent::__construct($attributes);
}

/**
* Make a new column instance.
*/
public static function make(array $attributes = [], array $cells = []): static
{
return new static($attributes, $cells);
}

public function cells($cells): static

Check failure on line 31 in src/Html/Row.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (8.2, prefer-stable)

Method Yajra\DataTables\Html\Row::cells() has parameter $cells with no type specified.

Check failure on line 31 in src/Html/Row.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (8.3, prefer-stable)

Method Yajra\DataTables\Html\Row::cells() has parameter $cells with no type specified.
{
$this->cells = collect($cells);

Check failure on line 33 in src/Html/Row.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (8.2, prefer-stable)

Access to an undefined property Yajra\DataTables\Html\Row::$cells.

Check failure on line 33 in src/Html/Row.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (8.2, prefer-stable)

Unable to resolve the template type TKey in call to function collect

Check failure on line 33 in src/Html/Row.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (8.2, prefer-stable)

Unable to resolve the template type TValue in call to function collect

Check failure on line 33 in src/Html/Row.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (8.3, prefer-stable)

Access to an undefined property Yajra\DataTables\Html\Row::$cells.

Check failure on line 33 in src/Html/Row.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (8.3, prefer-stable)

Unable to resolve the template type TKey in call to function collect

Check failure on line 33 in src/Html/Row.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (8.3, prefer-stable)

Unable to resolve the template type TValue in call to function collect

return $this;
}

protected function buildCells(array $cells): static
{
$this->cells = new Collection;

Check failure on line 40 in src/Html/Row.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (8.2, prefer-stable)

Access to an undefined property Yajra\DataTables\Html\Row::$cells.

Check failure on line 40 in src/Html/Row.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (8.3, prefer-stable)

Access to an undefined property Yajra\DataTables\Html\Row::$cells.

foreach ($cells as $key => $value) {
if (! is_a($value, Cell::class)) {
if (is_array($value)) {
$cellAttributes = array_merge($value, [
'column' => $value['column'] ?? $key,
'content' => $value['content'] ?? null,
]);
} else {
$cellAttributes = [
'column' => $key,
'content' => $value,
];
}

$this->cells->push(new Cell($cellAttributes));
} else {
$this->cells->push($value);
}
}

$this->cells = $this->cells->keyBy('column');

return $this;
}

public function getCellContentForColumn(Column $column): mixed
{
if ($this->cells->has($column->data)) {

Check failure on line 69 in src/Html/Row.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (8.2, prefer-stable)

Access to an undefined property Yajra\DataTables\Html\Row::$cells.

Check failure on line 69 in src/Html/Row.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (8.3, prefer-stable)

Access to an undefined property Yajra\DataTables\Html\Row::$cells.
return $this->cells->get($column->data)->content;

Check failure on line 70 in src/Html/Row.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (8.2, prefer-stable)

Access to an undefined property Yajra\DataTables\Html\Row::$cells.

Check failure on line 70 in src/Html/Row.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (8.3, prefer-stable)

Access to an undefined property Yajra\DataTables\Html\Row::$cells.
}

return null;
}
}
Loading