DiegoP DiegoP - 1 year ago 75
Javascript Question

Count child nodes inside a dom-repeat

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">
<iron-label class="g_lbl grey">
&nbsp;[[_computeproposals(job.proposals)]] Propuestas&nbsp;

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

//should return the number of proposals here
return <<number of child nodes in proposals>>;

enter image description here

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download