如何在使用JSONArray遍历车把时使用索引? [英] How can I use index while iterating over JSONArray in handlebar?

查看:270
本文介绍了如何在使用JSONArray遍历车把时使用索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图以不同的方式显示adultJsonArr的细节,并且我希望能够保存它,而不必写入冗长的代码。



我不是了解如何在迭代时使用索引,我试着在第5行中提到,但没有奏效(@index也无法为我工作)。



关于如何保存对adultJsonArr中的输入所做的更改的任何建议

  1.< form> 
2. {{#each adultJsonArr as | adultTravellerDetails index |}}
3.< div id =adult_form {{index}}>
4.成人{{index}}:
5.< input type =textvalue ={{adultJsonArr。[index] .traveller_name}}required>
6.< input type =dobvalue ={{adultTravellerDetails.traveller_dob}}>
7.< input type =textvalue ={{adultTravellerDetails.traveller_gender}}required>
8.< / div>
9. {{/ each}}
10.< div {{actionconfirmTravellerUpdateadultJsonArr}}>
11. {{mdl-button text ='submit'}}
12.< / div>
13.< / form>


解决方案

索引应该只能用于显示目的。您无需在 adultJsonArr 的当前索引内查找内容,因为您已经可以访问 adultTravellerDetails



话虽如此,如果您真的有一些用例需要查看当前索引处的数组值(或者您可能还有其他一些有序数组元数据,你想通过索引查找?)你可以做这样的事情:

{{get adultJsonArr(concat index。 traveller_name)}}



我们正在为 concat c $ c> get 查找 adultJsonArr 。这感觉有点冒失,但它的工作。


I am trying to show the details of the adultJsonArr in different and I want to get it saved on action without going into writing lengthy code.

I'm not getting idea of how to use the index while iterating, I tried as mentioned in line no 5 but didn't work( @index also couldn't do job for me).

Any suggestion on how to save the changes made to the input in the adultJsonArr

1.<form> 
2.   {{#each adultJsonArr as |adultTravellerDetails index|}}
3.    <div id="adult_form{{index}}">
4.      Adult{{index}}: 
5.      <input type="text" value="{{adultJsonArr.[index].traveller_name}}" required> 
6.      <input type="dob" value="{{adultTravellerDetails.traveller_dob}}">
7.      <input type="text" value="{{adultTravellerDetails.traveller_gender}}" required>
8.    </div>
9.   {{/each}}
10.  <div {{action "confirmTravellerUpdate" adultJsonArr }}>
11.     {{mdl-button text='submit'}}
12.  </div>
13.</form>

解决方案

The index should probably only be used for display purposes. There's no need to look up things within the current index of adultJsonArr because you already have access to adultTravellerDetails.

Having said that, if you really have some use-case where you need to look up the value of the array at the current index, (or maybe you have some other sorted array with meta-data that you want to look up by index?) you can do something like this:

{{get adultJsonArr (concat index ".traveller_name")}}

We are building a path with concat for get to look up on adultJsonArr. This feels a bit hacky, but it works.

这篇关于如何在使用JSONArray遍历车把时使用索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆