Skip to content

Commit

Permalink
Merge pull request #84 from djpa3k/data_as_object
Browse files Browse the repository at this point in the history
  • Loading branch information
n1crack authored Feb 19, 2022
2 parents 3aca3a2 + 63b0254 commit f5b5c32
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
5 changes: 3 additions & 2 deletions src/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,12 @@ public function isOrderable(): bool
}

/**
* @param string $property data as object, fallback data as string
* @return string
*/
public function data(): string
public function data($property = '_'): string
{
return $this->attr['data'] ?? '';
return $this->attr['data'][$property] ?? $this->attr['data']['_'] ?? $this->attr['data'] ?? '';
}

/**
Expand Down
10 changes: 5 additions & 5 deletions src/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,8 @@ public function setColumnAttributes(): void
{
$columns = $this->options->columns();
if ($columns) {
$attributes = array_column($columns, null, 'data');

foreach ($attributes as $index => $attr) {
foreach ($columns as $attr) {
$index = $attr['data']['_'] ?? $attr['data'];
if ($this->columns->visible()->isExists($index)) {
$this->columns->visible()->get($index)->attr = $attr;
}
Expand Down Expand Up @@ -278,7 +277,7 @@ protected function limit(): string
protected function orderBy(): string
{
$orders = $this->options->order();

$orders = array_filter($orders, function ($order) {
return \in_array($order['dir'], ['asc', 'desc'],
true) && $this->columns->visible()->offsetGet($order['column'])->isOrderable();
Expand All @@ -287,7 +286,8 @@ protected function orderBy(): string
$o = [];

foreach ($orders as $order) {
$id = $this->options->columns()[$order['column']]['data'];
$data = $this->options->columns()[$order['column']]['data'];
$id = $data['sort'] ?? $data['_'] ?? $data;

if ($this->columns->visible()->isExists($id)) {
$o[] = $this->columns->visible()->get($id)->name.' '.$order['dir'];
Expand Down

0 comments on commit f5b5c32

Please sign in to comment.