计算 dom-repeat 中的子节点

Count child nodes inside a dom-repeat

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

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

   <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),这里我需要return提案中子节点的数量:

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

console.log(提案):

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

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

Object.keys(proposals).length;