简体   繁体   中英

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.

在此处输入图片说明

   <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): 在此处输入图片说明

似乎是一个对象,所以它没有.length ,为此使用Object.keys

Object.keys(proposals).length;

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; return proposals && proposals.length || 0; in the function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM