Jade select field populating data

Risto Novik picture Risto Novik · Apr 28, 2012 · Viewed 27.9k times · Source

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}

Answer

AntelopeSalad picture AntelopeSalad · Apr 29, 2012

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.