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

[9.0] Add the possibility to makeVisible() some attribute of a model. #2263

Merged
merged 1 commit into from
Dec 17, 2019
Merged
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
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