From 256f3194f36102c62b191d4c7daddd5f6f8c06a8 Mon Sep 17 00:00:00 2001 From: sparkyx Date: Fri, 1 Aug 2025 23:35:28 +0200 Subject: [PATCH] IText : add optional datalist --- include/lib/itext.class.php | 46 +++++++++++++++++++++++++++++++------ 1 file changed, 39 insertions(+), 7 deletions(-) diff --git a/include/lib/itext.class.php b/include/lib/itext.class.php index b0fb54394..5f7ce2d0c 100644 --- a/include/lib/itext.class.php +++ b/include/lib/itext.class.php @@ -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(' + 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(' + 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=''.$r.''; - + $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('',$this->id); + foreach ($this->datalist as $item) { + $r.=sprintf('' + ,$item[0],$item[1] + ,htmlentities($item[1])); + } + $r.=''; + return $r; + } }