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}
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.
A couple of things to add to the answer (https://stackoverflow.com/a/10368381/870274):
"each" is more commonly used now instead of "for"
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}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With