0

I am trying to count child nodes inside a template dom-repeat. I am pulling data with firebase-query.

Inside a dom-repeat I want to display the number of child nodes of proposals object. The image shows the data structure in firebase, the dom-repeat loops all jobs.

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>

I am passing the proposals data to the function _computeproposals(job.proposals), here I need to return the number of childnodes in proposals:

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

console.log(proposals): enter image description here

Diego P
  • 1,728
  • 13
  • 28

2 Answers2

1

It's seems like an object so it does not have .length, use the Object.keys for this:

Object.keys(proposals).length;
adaliszk
  • 604
  • 5
  • 18
0

It's just an array, right? In that case, you could use proposals.length. Either use [[job.proposals.length]] in your template, or return proposals && proposals.length || 0; in the function.

Spiny Norman
  • 8,277
  • 1
  • 30
  • 55
  • I get 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.proposals.length]] don't display any value – Diego P Nov 25 '16 at 00:01
  • Look at the answer by @Adamos42, he saw what I missed. Good job :) – Spiny Norman Nov 25 '16 at 08:36