Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Imam Ali Mustofa
Imam Ali Mustofa

Posted on • Originally published atdarkterminal.prose.sh

     

Reusable Input Datalist

Reusable Input Datalist

Hello Punk! In my daily boring...

I found something interesting that I will share with you, this is not too fancy or glam but still "fun-cy" for me, cz In my stupid knowledge this is really helpful and enough for me and myself.

HTML5 Input Datalist

At least, this is whatW3Schools said in their example:

W3Schools Example

What if I wrap inside PHP Function?

When I work withHTMX I need isolated component that can be reusable a form. So I create a PHP Function that generate the Input Datalist.

// Filename: helpers/Utils.phppublicstaticfunctionselectSearch(string$inputId,array$options,int|string$selected=''):string{$inputHTML='<div>';$inputHTML.='<input type="text"mf">.$inputId.'" list="" placeholder="Search..." autocomplete="off" />';$inputHTML.='<datalistmf">.$inputId.'_list">';$selectedText='';$selectedValue='';foreach($optionsas$option){$text=\explode('#',$option)[1];$value=\explode('#',$option)[0];if($selected==$value){$selectedText=$text;$selectedValue=$value;}$inputHTML.='<option value="'.$option.'">'.$text.'</option>';}$inputHTML.='</datalist>';$inputHTML.='<input type="hidden" name="'.$inputId.'"mf">.$inputId.'" value="'.$selectedValue.'" />';$inputHTML.='</div>';$inputHTML.='<script type="text/javascript">';$inputHTML.='fancyDropdown("'.$inputId.'");';$inputHTML.='setTimeout(() => document.getElementById("'.$inputId.'").value = "'.$selectedText.'", 350);';$inputHTML.='</script>';return$inputHTML;}
Enter fullscreen modeExit fullscreen mode

I create a static function insideUtils class so I can use that function everywhere in my project.

Is that using TailwindCSS? Yes, it's too dirty. But fck it! It's work! (Don't blame on me, if you really programmer so read it)

You can see thisscript:

$inputHTML.='<script type="text/javascript">';$inputHTML.='fancyDropdown("'.$inputId.'");';$inputHTML.='setTimeout(() => document.getElementById("'.$inputId.'").value = "'.$selectedText.'", 350);';$inputHTML.='</script>';
Enter fullscreen modeExit fullscreen mode

Yes! Again I wrote JavaScript as a string in PHP, why not! And where is the fckinfancyDropdown come from?!

Here is, I found a Question in StackOverflow that asked about

Question in StackOverflow

The Question:https://stackoverflow.com/q/13693482

Yes! Around 11 years ago, and the solution that match with my need is come around

the solution that match with my need

The Solution:https://stackoverflow.com/a/76178626

The updated version of

The updated version

Original Answer:https://stackoverflow.com/a/74786719

Here is this code:

functionfancyDropdown(inputId){constinput=document.getElementById(inputId);consttarget=document.getElementById('target_'+inputId);constdatalist=input.nextElementSibling;letminWidth=datalist.offsetWidth;functionoutputsize(){if(input.offsetWidth<minWidth){datalist.style.minWidth=input.offsetWidth+'px';}else{datalist.style.width=input.offsetWidth+'px';}}newResizeObserver(outputsize).observe(input);input.addEventListener("input",function(e){datalist.style.display="block";consttext=input.value.toUpperCase();lethide=1;for(letoptionofdatalist.options){if(option.value.toUpperCase().indexOf(text)>-1){option.style.display="block";hide=0;}else{option.style.display="none";}}if(hide){datalist.style.display="none";}});input.addEventListener("click",function(e){lethide=1;for(letoptionofdatalist.options){if(window.getComputedStyle(option,null).display=="block")hide=0;}if(datalist.style.display=="block"||hide==1){datalist.style.display="none";}else{datalist.style.display="block";}});document.addEventListener("click",function(e){if(e.target.tagName=="OPTION"){letinputValue=e.target.value.split('#');input.value=inputValue[1];target.value=inputValue[0];}if(e.target.tagName!=="DATALIST"&&e.target.tagName!=="INPUT"){datalist.style.display="none";}});datalist.style.display="none";}
Enter fullscreen modeExit fullscreen mode

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Software Freestyle Engineer (SFE) and Metaphor Storyteller, I like to create useless tools and apps.
  • Location
    Tegal City, Indonesia
  • Education
    Street Community Programmer (SCP)
  • Pronouns
    l
  • Work
    Software Freestyle Engineer (Freestyler)
  • Joined

More fromImam Ali Mustofa

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp