From ad6972045c2edfd966f61e56a7f3437350d50b50 Mon Sep 17 00:00:00 2001 From: Tiger Fok Date: Mon, 16 Dec 2019 19:00:08 +0800 Subject: [PATCH] Add a makeVisible() to the row object. --- src/DataTableAbstract.php | 14 ++++++++++++++ src/Processors/DataProcessor.php | 3 ++- src/Utilities/Helper.php | 1 + 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/DataTableAbstract.php b/src/DataTableAbstract.php index 38e63c6c..e66ef97f 100644 --- a/src/DataTableAbstract.php +++ b/src/DataTableAbstract.php @@ -57,6 +57,7 @@ abstract class DataTableAbstract implements DataTable, Arrayable, Jsonable 'order' => [], 'only' => null, 'hidden' => [], + 'visible' => [], ]; /** @@ -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. diff --git a/src/Processors/DataProcessor.php b/src/Processors/DataProcessor.php index f0297ce1..98a12389 100644 --- a/src/Processors/DataProcessor.php +++ b/src/Processors/DataProcessor.php @@ -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; } @@ -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); diff --git a/src/Utilities/Helper.php b/src/Utilities/Helper.php index a464f160..3f68b48c 100644 --- a/src/Utilities/Helper.php +++ b/src/Utilities/Helper.php @@ -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) {