Skip to content

Commit

Permalink
Merge pull request #2263 from imTigger/master
Browse files Browse the repository at this point in the history
[9.0] Add the possibility to makeVisible() some attribute of a model.
  • Loading branch information
yajra authored Dec 17, 2019
2 parents c524b85 + ad69720 commit 20cc622
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
14 changes: 14 additions & 0 deletions src/DataTableAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ abstract class DataTableAbstract implements DataTable, Arrayable, Jsonable
'order' => [],
'only' => null,
'hidden' => [],
'visible' => [],
];

/**
Expand Down Expand Up @@ -261,6 +262,19 @@ public function makeHidden(array $attributes = [])
return $this;
}

/**
* Add a makeVisible() to the row object.
*
* @param array $attributes
* @return $this
*/
public function makeVisible(array $attributes = [])
{
$this->columnDef['visible'] = array_merge_recursive(Arr::get($this->columnDef, 'visible', []), $attributes);

return $this;
}

/**
* Set columns that should not be escaped.
* Optionally merge the defaults from config.
Expand Down
3 changes: 2 additions & 1 deletion src/Processors/DataProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ public function __construct($results, array $columnDef, array $templates, $start
$this->includeIndex = $columnDef['index'];
$this->rawColumns = $columnDef['raw'];
$this->makeHidden = $columnDef['hidden'];
$this->makeVisible = $columnDef['visible'];
$this->templates = $templates;
$this->start = $start;
}
Expand All @@ -99,7 +100,7 @@ public function process($object = false)
$indexColumn = config('datatables.index_column', 'DT_RowIndex');

foreach ($this->results as $row) {
$data = Helper::convertToArray($row, ['hidden' => $this->makeHidden]);
$data = Helper::convertToArray($row, ['hidden' => $this->makeHidden, 'visible' => $this->makeVisible]);
$value = $this->addColumns($data, $row);
$value = $this->editColumns($value, $row);
$value = $this->setupRowVariables($value, $row);
Expand Down
1 change: 1 addition & 0 deletions src/Utilities/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ public static function getOrMethod($method)
public static function convertToArray($row, $filters = [])
{
$row = method_exists($row, 'makeHidden') ? $row->makeHidden(Arr::get($filters, 'hidden', [])) : $row;
$row = method_exists($row, 'makeVisible') ? $row->makeVisible(Arr::get($filters, 'visible', [])) : $row;
$data = $row instanceof Arrayable ? $row->toArray() : (array) $row;

foreach ($data as &$value) {
Expand Down

0 comments on commit 20cc622

Please sign in to comment.