From 9b71cb53d0de2d5feb134013f25a41eadad44661 Mon Sep 17 00:00:00 2001 From: Alessandro Ferro <49845537+xfarrow@users.noreply.github.com> Date: Tue, 19 Jul 2022 00:08:56 +0200 Subject: [PATCH] It is now possible to hide a column in Table.php --- phpxpress/phpxpress/table.php | 65 +++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/phpxpress/phpxpress/table.php b/phpxpress/phpxpress/table.php index e297ea3..41ab754 100644 --- a/phpxpress/phpxpress/table.php +++ b/phpxpress/phpxpress/table.php @@ -5,6 +5,7 @@ // Structure private $dataSource; private $columnCaptions; + private $invisible_columns; // Events private $onValueDisplayingFunctionName; @@ -35,9 +36,37 @@ echo ""; + /* + ** + ** management of invisible columns. + ** + ** $this->invisible_columns is an array containing the name + ** of the key values of the array/object not to be displayed. + ** + ** $invisible_column_captions is an array holding the column + ** captions of the columns to be removed. + */ + if(!empty($this->invisible_columns)){ + if(is_array($this->dataSource[0])){ + $array_keys = array_keys($this -> dataSource[0]); + } + else{ + $array_keys = array_keys(get_object_vars($this -> dataSource[0])); + } + + $invisible_columns_captions = array(); + foreach($this->invisible_columns as $invisible_column_name){ + $column_index = array_search($invisible_column_name, $array_keys); + array_push($invisible_columns_captions, $this->columnCaptions[$column_index]); + } + } + // Print head echo ''; foreach($this->columnCaptions as $caption){ + if( isset($invisible_columns_captions) && in_array($caption, $invisible_columns_captions)) + continue; + echo ''; } echo ''; @@ -48,6 +77,10 @@ echo ''; foreach ($obj as $name => $value) { + if(!empty($this->invisible_columns) && in_array($name, $this->invisible_columns)){ + continue; + } + if(isset( $this -> onValueDisplayingFunctionName)){ call_user_func_array($this -> onValueDisplayingFunctionName , array($name, &$value, (array)$obj)); } @@ -164,6 +197,38 @@ } } + /* + ** Expected $column_name: the key of the column to remove. + ** + ** Making a column invisible, rather than removing it from the datasource, might + ** be useful in those circumstances where a value is needed in the datasource + ** or in the onValueDisplaying, without actually removing it. + ** + */ + function invisible_column($column_name){ + + if(is_array($this->dataSource[0])){ + $array_keys = array_keys($this -> dataSource[0]); + } + else{ + $array_keys = array_keys(get_object_vars($this -> dataSource[0])); + } + + if(!in_array($column_name, $array_keys)){ + throw new InvalidArgumentException("The provided column $column_name is not present in the datasource's column keys"); + } + + if(isset($this->invisible_columns)){ + if(in_array($column_name, $this->invisible_columns)) + throw new InvalidArgumentException("The provided column $column_name is already invisible"); + + array_push($this->invisible_columns, $column_name); + } + else{ + $this->invisible_columns = array($column_name); + } + } + /* ** The Event "onValueDisplaying" gets fired just before the displaying of a value within ** a table body, so you can display your own value.
' . $caption . '