From a4c867d6b340938bdbdd9e2e9e2512cfbdd80c19 Mon Sep 17 00:00:00 2001 From: Roland Gruber Date: Wed, 29 Aug 2018 18:59:45 +0200 Subject: [PATCH] show/hide for radio --- lam/lib/html.inc | 129 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 125 insertions(+), 4 deletions(-) diff --git a/lam/lib/html.inc b/lam/lib/html.inc index 5b6c8b99..317ce4d5 100644 --- a/lam/lib/html.inc +++ b/lam/lib/html.inc @@ -1623,6 +1623,10 @@ class htmlRadio extends htmlElement { private $isEnabled = true; /** on change code */ private $onchangeEvent = null; + /** list of enclosing table rows to hide when checked */ + protected $tableRowsToHide = array(); + /** list of enclosing table rows to show when checked */ + protected $tableRowsToShow = array(); /** * Constructor. @@ -1663,13 +1667,23 @@ class htmlRadio extends htmlElement { if (!$this->isEnabled) { $disabled = ' disabled'; } - $onchange = ''; - if ($this->onchangeEvent != null) { - $onchange = ' onchange="' . $this->onchangeEvent . '"'; + if (($this->tableRowsToHide != null) || ($this->tableRowsToShow != null)) { + $this->printInitialState(); } // print radio list $counter = 0; foreach ($this->elements as $label => $value) { + $showHideOnchange = ''; + if (($this->tableRowsToHide != null) || ($this->tableRowsToShow != null)) { + $showHideOnchange = $this->getOnchangeCodeForShowHideTableRows($counter); + } + $onchange = ''; + if ($this->onchangeEvent != null) { + $onchange = ' onchange="' . $this->onchangeEvent . '"'; + } + elseif (!empty($showHideOnchange)) { + $onchange = ' onchange="' . $showHideOnchange . '"'; + } $onClick = 'onClick=" jQuery(\'input[name=' . $this->name . ']\').prop(\'checked\', false); jQuery(\'#' . $this->name . $counter . '\').prop(\'checked\', true); @@ -1709,6 +1723,104 @@ class htmlRadio extends htmlElement { $this->onchangeEvent = htmlspecialchars($onchangeEvent); } + /** + * Returns the selector to use to find the show/hide elements. + * + * @return string selector + */ + protected function getShowHideSelector() { + return 'tr'; + } + + /** + * Creates the JavaScript code to hide/show table rows based on the select value. + * + * @param int $counter index + * @return String onChange code + */ + private function getOnchangeCodeForShowHideTableRows($counter) { + $onChange = ''; + if ((sizeof($this->tableRowsToHide) == 0) && (sizeof($this->tableRowsToShow) == 0)) { + return; + } + $values = array(); + if (!empty($this->tableRowsToHide)) { + $values = array_merge($values, array_keys($this->tableRowsToHide)); + } + if (!empty($this->tableRowsToShow)) { + $values = array_merge($values, array_keys($this->tableRowsToShow)); + } + $values = array_unique($values); + $selector = $this->getShowHideSelector(); + // build Java script to show/hide depending fields + foreach ($values as $val) { + // build onChange listener + $onChange .= 'if (jQuery(\'#' . $this->name . $counter . '\').val() == \'' . $val . '\') {'; + if (isset($this->tableRowsToShow[$val])) { + for ($i = 0; $i < sizeof($this->tableRowsToShow[$val]); $i++) { + $onChange .= 'jQuery(\'#' . $this->tableRowsToShow[$val][$i] . '\').closest(\'' . $selector . '\').removeClass(\'hidden\');'; + } + } + if (isset($this->tableRowsToHide[$val])) { + for ($i = 0; $i < sizeof($this->tableRowsToHide[$val]); $i++) { + $onChange .= 'jQuery(\'#' . $this->tableRowsToHide[$val][$i] . '\').closest(\'' . $selector . '\').addClass(\'hidden\');'; + } + } + $onChange .= '};'; + } + return $onChange; + } + + private function printInitialState() { + $selector = $this->getShowHideSelector(); + // build script to set initial state + $script = ''; + echo $script; + } + + /** + * This will hide the given table rows when the radio is changed to the specified value. + * The given IDs can be of any e.g. input element. Starting from this element + * the first parent "" element will be used to show/hide. + *
+ *
+ *
Example for $tableRowsToHide: + *
array('val1' => array('option1', 'option2'), 'val2' => array('option3')) + * + * @param array $tableRowsToHide array of select value => array of IDs of child elements to hide + */ + public function setTableRowsToHide($tableRowsToHide) { + $this->tableRowsToHide = $tableRowsToHide; + } + + /** + * This will show the given table rows when the radio is changed to the specified value. + * The given IDs can be of any e.g. input element. Starting from this element + * the first parent "" element will be used to show/hide. + *
+ *
+ *
Example for $tableRowsToShow: + *
array('val1' => array('option1', 'option2'), 'val2' => array('option3')) + * + * @param array $tableRowsToShow array of select value => array of IDs of child elements to show + */ + public function setTableRowsToShow($tableRowsToShow) { + $this->tableRowsToShow = $tableRowsToShow; + } + } /** @@ -2292,7 +2404,7 @@ class htmlInputFileUpload extends htmlElement { if (!$this->isEnabled) { $disabled = ' disabled'; } - echo ''; + echo ''; return array($this->name => 'file'); } @@ -4288,6 +4400,15 @@ class htmlResponsiveRadio extends htmlRadio { return $row->generateHTML($module, $input, $values, $restricted, $tabindex, $scope); } + /** + * Returns the selector to use to find the show/hide elements. + * + * @return string selector + */ + protected function getShowHideSelector() { + return '.row'; + } + } /**