Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

best way to construct datalist with mojolicious

What is the best way to construct a html5 datalist with mojolicious?

I looked for a tag helper but did not find a tag helper for constructing it.

Here is an example of a datalist:

<datalist id="frameworks">
    <option value="MooTools">
    <option value="Moobile">
    <option value="Dojo Toolkit">
    <option value="jQuery">
    <option value="YUI">
</datalist>

The list is dynamic and is fetched from a db, so i cannot use a static html chunk.

There are similar tag helpers, e.g. for a <select> tag I can put into my template:

%= select_field country => [[Germany => 'de'], 'en']

which produces:

<select name="country">
  <option value="de">Germany</option>
  <option value="en">en</option>
</select>

but I couldn't find anything regarding a datalist in the default tag helpers.

like image 743
David Michael Gang Avatar asked Nov 19 '25 20:11

David Michael Gang


1 Answers

It's been a while, but maybe will find it useful.

In the controller, you need to have an array with some items. Let's create it:

my @levelsArray = ();
for (my $i=0;$i<10;$i++){
    push @levelsArray, "level00".$i;
}

After that, send it to the template:

get '/index' => sub {
    my ( $mojo ) = @_;
    $mojo -> stash ('levelsArray' => \@levelsArray);
    $mojo -> render (template => 'index' );
};

and finally, render it using:

<%= select_field 'levelSelected' => [ @{ stash('levelsArray') }] %>
like image 66
Luis Diaz Avatar answered Nov 21 '25 08:11

Luis Diaz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!