From d0a106f6a7abe22d7cd0b835de0e7923e43b3cfc Mon Sep 17 00:00:00 2001 From: Alessandro Ferro <49845537+xfarrow@users.noreply.github.com> Date: Wed, 9 Mar 2022 00:00:56 +0100 Subject: [PATCH] added event management --- phpxpress/examples/table.php | 9 +++++++++ phpxpress/phpxpress/table.php | 29 +++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/phpxpress/examples/table.php b/phpxpress/examples/table.php index 0940e1b..9b32c0c 100644 --- a/phpxpress/examples/table.php +++ b/phpxpress/examples/table.php @@ -40,8 +40,17 @@ $table->setStripedRows(true); $table->setBordered(true); $table->setHoverAnimation(true); + + $table->onValueDisplaying("onValueDisplaying"); // event + $table->draw(); + function onValueDisplaying($caption, &$value){ + if($caption == "ssn"){ + $value = "SSN not shown for privacy reasons"; + } + } + ?> \ No newline at end of file diff --git a/phpxpress/phpxpress/table.php b/phpxpress/phpxpress/table.php index 252d0a3..91fb0d9 100644 --- a/phpxpress/phpxpress/table.php +++ b/phpxpress/phpxpress/table.php @@ -2,9 +2,14 @@ include "include.php"; class Table{ + // Structure private $dataSource; private $columnCaptions; + // Events + private $onValueDisplayingFunctionName; + + // Layout private $darkTheme = false; private $stripedRows = false; private $bordered = false; @@ -39,7 +44,12 @@ foreach($this->dataSource as $obj){ echo ''; foreach ($obj as $name => $value) { + + if(isset( $this -> onValueDisplayingFunctionName)){ + call_user_func_array($this -> onValueDisplayingFunctionName , array($name, &$value)); + } echo '' . $value . ''; + } echo ''; } @@ -72,6 +82,25 @@ throw new LengthException('Number of provided captions not matching the datasource ones.'); } + /* + ** The Event "onValueDisplaying" gets fired just before the displaying of a value within + ** a table body, so you can display your own value if you want to. + ** The function you have to provide must have this signature: + ** + ** function myFunction($caption, &$value){...} + ** + ** You can change "caption" and "value" to whichever name you want. + ** + ** caption: the ACTUAL fieldname of the dataSource whose value belong + ** value: the value you want to display. + */ + function onValueDisplaying($functionName){ + if(!is_callable($functionName)) + throw new InvalidArgumentException("Couldn't call $functionName. You must provide the name of a function."); + + $this -> onValueDisplayingFunctionName = $functionName; + } + function setDarkTheme($bool){ if(!is_bool($bool)){ throw new InvalidArgumentException('Parameter must be a boolean.');