Improve ISELECT can use OPTGROUP

This commit is contained in:
sparkyx 2025-08-03 11:09:11 +02:00
parent 6be0dd2866
commit 9584a3090a
2 changed files with 117 additions and 1 deletions

View file

@ -54,7 +54,26 @@ class ISelect extends HtmlInput
$this->value=$p_value;
}
}
/*!\brief show the html input of the widget*/
/*!
* \brief show the html input of the widget
* \note to use a OPTGROUP, the key "value" must be null , it is important
* to note that it is needed to use for opening and closing the element
* \code
$select->value=array(
array ("value"=>null,"label"=>"Groupe 1"),
array ("value"=>1,"label"=>"Element 1"),
array ("value"=>2,"label"=>"Element 2"),
array ("value"=>null,"label"=>"END Groupe 1"), // not displaid
array ("value"=>null,"label"=>"Groupe 2"),
array ("value"=>1,"label"=>"Element 1"),
array ("value"=>2,"label"=>"Element 2"),
array ("value"=>null,"label"=>"END group Groupe 1")// not displaid
);
*
* \endcode
*
* */
public function input($p_name=null,$p_value=null)
{
$this->name=($p_name==null)?$this->name:$p_name;
@ -70,9 +89,24 @@ class ISelect extends HtmlInput
$a="<SELECT id=\"$this->id\" NAME=\"$this->name\" $style $this->javascript $disabled $rowsize>";
if (empty($this->value)) return '';
// var $start_group boolean , true the element OPTGROUP starts, false, it ends
$start_group=false;
for ( $i=0;$i<sizeof($this->value);$i++)
{
// open the element optgroup
if ($this->value[$i]['value']===null && !$start_group) {
$start_group=true;
$a.=sprintf('<optgroup label="%s">', htmlspecialchars($this->value[$i]['label']));
continue;
}
// close the element optgroup
if ($this->value[$i]['value']==null && $start_group) {
$start_group=false;
$a.='</optgroup >';
continue;
}
$checked=($this->selected==$this->value[$i]['value'])?"SELECTED":"";
$a.='<OPTION VALUE="'.$this->value[$i]['value'].'" '.$checked.'>';
$a.=strip_tags($this->value[$i]['label']);
}

View file

@ -264,3 +264,85 @@ $http=new HttpInput();
$date->javascript=sprintf('onchange = "console.debug(\'change date\');%s"',"format_date(this);");
echo $date->input();
?>
<h2>ISELECT</h2>
<h3> Normal</h3>
<?php
$select=new ISelect("animal");
$select->value=array(
["value"=>1,"label"=>"Deer"],
["value"=>2,"label"=>"Dog"],
["value"=>3,"label"=>"Cat"],
["value"=>4,"label"=>"Shark"],
["value"=>5,"label"=>"Goldenfish"],
["value"=>6,"label"=>"Salmon"],
["value"=>7,"label"=>"Chicken"],
["value"=>8,"label"=>"Swallow"],
["value"=>9,"label"=>"Eagle"]
);
$select->label="Animal";
echo $select->input();
?>
<p>
8 (Swallow) is selected
</p>
input is
<?php
$select->selected=8;
echo $select->input();
?>
display is
<?php
echo $select->display();
?>
<h3> OPTGROUP</h3>
<?php
$select=new ISelect("animal");
$select->value=array(
["value"=>0,"label"=>"Aucun choix"],
["value"=>null,"label"=>"Mammal"],
["value"=>1,"label"=>"Deer"],
["value"=>2,"label"=>"Dog"],
["value"=>3,"label"=>"Cat"],
["value"=>null,"label"=>"END Mammal"],
["value"=>null,"label"=>"Fish"],
["value"=>4,"label"=>"Shark"],
["value"=>5,"label"=>"Goldenfish"],
["value"=>6,"label"=>"Salmon"],
["value"=>null,"label"=>"END Fish"],
["value"=>null,"label"=>"Bird"],
["value"=>7,"label"=>"Chicken"],
["value"=>8,"label"=>"Swallow"],
["value"=>9,"label"=>"Eagle"],
["value"=>null,"label"=>"END Bird"]
);
$select->label="Animal";
echo $select->input();
?>
<p>
5 (Goldenfish) is selected
</p>
input is
<?php
$select->selected=5;
echo $select->input();
?>
display is
<?php
echo $select->display();
?>