Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jade select field populating data

Tags:

Is there any better ways to populate Jade based select fields, I am currently using this example. Is there any better ways to not ruin the template code?

the item value is 'day' example.

    select
      repeation = [ 'no-repeat', 'day', 'week', 'month']
      for item in repeation
        if job.repeat == item
          option(selected="true") #{item}
        else
          option #{item}

Also what about displaying multiple selections, when the item is array of ['day', 'week']?

// Edit small possible solution for multiple element

      enginges = [ 'google', 'bing', 'yahoo', 'duckduckgo']
      for engine in enginges
        option(selected=job.sources.indexOf(engine) != -1) #{engine}
like image 335
Risto Novik Avatar asked Apr 28 '12 11:04

Risto Novik


2 Answers

You should be able to do something like:

for item in repeation
  option(selected=job.repeat == item) #{item}

The same concept should be able to be applied to a multiple item select drop down.

like image 137
AntelopeSalad Avatar answered Oct 05 '22 00:10

AntelopeSalad


A couple of things to add to the answer (https://stackoverflow.com/a/10368381/870274):

  1. "each" is more commonly used now instead of "for"

  2. don't forget the "-" for the line: repeation = [ 'no-repeat', 'day', 'week', 'month'] ,or you will get a compilation error. So the final result would be (same as yours):

    select
      - repeation = [ 'no-repeat', 'day', 'week', 'month']
      each item in repeation
        option(selected=job.repeat == item) #{item}
    
like image 27
Rico Rodriquez Collins Avatar answered Oct 04 '22 22:10

Rico Rodriquez Collins