IText : add optional datalist

This commit is contained in:
sparkyx 2025-08-01 23:35:28 +02:00
parent 007e0c94a2
commit 256f3194f3

View file

@ -44,6 +44,9 @@ class IText extends HtmlInput
var $pattern; /*!< $pattern HTML pattern */
var $maxlength; /*!< HTML maxlength */
var $require ; /*!< $require (bool)*/
protected $datalist; /*!< $datalist (double array)
* each row has a key (value) and a label used with make_datalist */
function __construct($name='',$value='',$p_id="")
{
parent::__construct($name,$value,$p_id);
@ -56,6 +59,7 @@ class IText extends HtmlInput
$this->css_size="";
$this->pattern="";
$this->maxlength="";
$this->datalist=null;
}
/*!
\brief show the html input of the widget
@ -87,12 +91,12 @@ class IText extends HtmlInput
if ($this->maxlength !="" ) {
$maxlength=sprintf(' maxlength="%s" ',$this->maxlength);
}
$datalist=(empty($this->datalist ))?"":sprintf('list="dl_%s"',$this->id);
if ( ! isset ($this->css_size) || empty ($this->css_size))
{
$r= sprintf('<INPUT TYPE="TEXT" %s id="%s" name="%s" value="%s" placeholder="%s" title="%s"
size="%s" %s %s %s %s %s %s %s>
size="%s" %s %s %s %s %s %s %s %s>
',$this->style,
$this->id,
$this->name,
@ -106,11 +110,12 @@ class IText extends HtmlInput
$require,
$strAttribute,
$pattern,
$maxlength
$maxlength,
$datalist
);
} else {
$r= sprintf('<INPUT TYPE="TEXT" %s id="%s" name="%s" value="%s" placeholder="%s" title="%s"
style="width:%s ;" %s %s %s %s %s>
style="width:%s ;" %s %s %s %s %s %s>
',$this->style,
$this->id,
$this->name,
@ -124,17 +129,27 @@ class IText extends HtmlInput
$require,
$strAttribute,
$pattern,
$maxlength
$maxlength,
$datalist
);
}
/* add tag for column if inside a table */
if ( $this->table == 1 ) $r='<td>'.$r.'</td>';
$r.=$this->make_datalist();
return $r;
}
/*!\brief print in html the readonly value of the widget*/
public function get_datalist() {
return $this->datalist;
}
public function set_datalist($datalist) {
$this->datalist = $datalist;
return $this;
}
/*!\brief print in html the readonly value of the widget*/
public function display()
{
$t= ((isset($this->title)))?'title="'.$this->title.'" ':' ';
@ -173,4 +188,21 @@ class IText extends HtmlInput
static public function test_me()
{
}
/**
* @brief add a datalist
* @param $array (double array) each row has 2 keys 0 => stored value 1=>label
* @return string
*/
protected function make_datalist()
{
if ( empty($this->datalist)) return "";
$r=sprintf('<datalist id="dl_%s"">',$this->id);
foreach ($this->datalist as $item) {
$r.=sprintf('<option value="%s">%s %s</option>'
,$item[0],$item[1]
,htmlentities($item[1]));
}
$r.='</datalist>';
return $r;
}
}