翡翠选择字段填充数据

Jade select field populating data

本文关键字:填充 数据 字段 选择      更新时间:2023-09-26

有没有更好的方法来填充基于 Jade 的选择字段,我目前正在使用此示例。有没有更好的方法可以不破坏模板代码?

项目值为"天"示例。

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

另外,当项目是 ['天', '周'] 数组时,显示多个选择怎么样?

编辑多个元素的小可能解决方案

      enginges = [ 'google', 'bing', 'yahoo', 'duckduckgo']
      for engine in enginges
        option(selected=job.sources.indexOf(engine) != -1) #{engine}

你应该能够执行以下操作:

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

相同的概念应该能够应用于多项目选择下拉列表。

几件事要添加到答案中(https://stackoverflow.com/a/10368381/870274):

    "
  1. 每个"现在更常用,而不是"for"

  2. 不要忘记该行的"-":重复 = [ '不重复', '天',"周","月"],否则会出现编译错误。 所以最后结果将是(与您的相同):

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