2016-11-24 62 views
0

我正在嘗試計算dom-repeat模板中的子節點。我正在使用Firebase查詢來提取數據。在dom-repeat中計算子節點

在dom-repeat裏面我想要顯示提議對象的子節點的數量。該圖顯示了firebase中的數據結構,dom-repeat循環了所有作業。

enter image description here

<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),在這裏我需要返回提案中的childNodes數量:

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

console.log(投標): enter image description here

回答

1

這似乎是一個對象,因此它不具有.length,使用Object.keys此:

Object.keys(proposals).length; 
0

這只是一個數組,對吧?在這種情況下,您可以使用proposals.length。在模板中使用[[job.proposals.length]],或者在函數中使用return proposals && proposals.length || 0;

+0

我得到遺漏的類型錯誤:無法讀取的未定義的屬性「長度」(...),當我返回proposals.length在函數中,不知道如何轉換爲數組,在模板[[job.proposals.length]]中不顯示任何值 –

+0

看@ Adamos42的答案,他看到我錯過了。做得好 :) –