【问题标题】:Get current index of for tag in jsrender获取 jsrender 中 for 标签的当前索引
【发布时间】:2023-03-15 11:21:01
【问题描述】:

有什么办法可以得到标签中for标签的当前索引。

需要解决方案

{{for ~ID=#index}}
     {{:~ID}}
{{/for}}

它不起作用,因为 #index 只能在 for 循环中访问。

工作代码:

{{for}}
     {{:#index}}
{{/for}}

有没有什么方法可以访问for标签而不是prop标签中的Jsonobject键和值。

{{for arrayOfObj}}
   {{:#data.key}} //In here data is a jsonobject. 
   //I need an key and value of this object.
{{/for}}

提前致谢。

【问题讨论】:

    标签: jquery-templates jsrender


    【解决方案1】:

    你的问题不是很清楚。你有一个对象数组 - 好的,你想做什么?

    如果你想遍历数组,你可以写{{for arrayOfObj}}...{{/for}}

    现在,在该块中,您可以获取索引和对象。如果您想获取特定的已知密钥(例如,如果您知道该对象具有 name 属性),您可以编写 {{:name}}

    但是如果你想遍历每个对象的所有属性,你可以使用{{props}}(对于每个对象,在{{for}}块内):

    {{for arrayOfObj}} - iterate over array
      {{:#index}} - this is the index in the array
      {{:name}}
      {{props}} - iterate over props of this object
        {{:key}}
        {{:prop}}
        {{:#getIndex()}} - this is the index in the array
      {{/props}}
    {{/for}}
    

    【讨论】:

    • 我尝试访问标签内的索引。 {{for arrayOfObj ~ObjectinIndex=arrayOfObj[#index]}}{{/for}}在这里我需要一个标签内的for标签索引
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-23
    • 1970-01-01
    相关资源
    最近更新 更多