【问题标题】:Count child nodes inside a dom-repeat计算 dom-repeat 中的子节点
【发布时间】:2016-11-26 01:05:16
【问题描述】:

我正在尝试计算模板 dom-repeat 中的子节点。我正在使用 firebase-query 提取数据。

在 dom-repeat 中,我想显示提案对象的子节点数。图为firebase中的数据结构,dom-repeat循环了所有的job。

   <template is="dom-repeat" indexAs="index" id="joblist" items="{{jobs}}" as="job">
    <div class="job-container" on-transitionend="_getData">
     <paper-card class="cards" heading="{{job.name}}" elevation="0">
        <paper-ripple id="ripple" recenters></paper-ripple>
        <div class="card-content">{{job.description}}</div>
      <div class="card-actions">
       <div class="horizontal justified">
        <iron-label class="g_lbl green">
            &nbsp;{{job.budget}}&nbsp;&nbsp;
        </iron-label>
        <iron-label class="g_lbl grey">
            &nbsp;[[_computeproposals(job.proposals)]] Propuestas&nbsp;
        </iron-label>
       </div>
      </div>
     </paper-card>
    </div>
   </template>

我正在将提案数据传递给函数_computeproposals(job.proposals),这里我需要返回提案中的子节点数:

    _computeproposals:function(proposals){
        //should return the number of proposals here
        console.log(proposals);
            return <<number of child nodes in proposals>>;
        }

console.log(提案):

【问题讨论】:

    标签: javascript polymer polymer-1.0 polymerfire


    【解决方案1】:

    它看起来像一个对象,所以它没有.length,为此使用Object.keys

    Object.keys(proposals).length;
    

    【讨论】:

      【解决方案2】:

      这只是一个数组,对吧?在这种情况下,您可以使用proposals.length。在模板中使用[[job.proposals.length]],或者在函数中使用return proposals &amp;&amp; proposals.length || 0;

      【讨论】:

      • 我得到 Uncaught TypeError: Cannot read property 'length' of undefined(...) when I return proposals.length in the function, not sure how to convert to an array, in the template [[job. proposal.length]] 不显示任何值
      • 看看@Adamos42 的答案,他看到了我错过的东西。干得好:)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多