20

Jadeベースの選択フィールドにデータを入力するためのより良い方法はありますか?私は現在この例を使用しています。テンプレートコードを台無しにしないためのより良い方法はありますか?

アイテムの値は「日」の例です。

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

また、アイテムが['day'、'week']の配列である場合、複数の選択を表示するのはどうですか?

//複数の要素の可能な小さなソリューションを編集します

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

2 に答える 2

38

次のようなことができるはずです。

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

同じ概念を複数のアイテムの選択ドロップダウンに適用できる必要があります。

于 2012-04-28T23:13:29.170 に答える
2

答えに追加するいくつかのこと(https://stackoverflow.com/a/10368381/870274):

  1. 「for」の代わりに「each」が現在より一般的に使用されています

  2. 次の行の「-」を忘れないでください:repeation = ['no-repeat'、'day'、'week'、'month']、さもないとコンパイルエラーが発生します。したがって、最終結果は(あなたと同じ)になります:

    select
      - repeation = [ 'no-repeat', 'day', 'week', 'month']
      each item in repeation
        option(selected=job.repeat == item) #{item}
    
于 2016-05-16T22:20:54.310 に答える