2016-09-18 76 views
0

我有這樣的代碼流星onrendered使用jQuery的時候做的工作每個

Template.drone.onRendered(function() { 
     var userid = Meteor.userId(); 

     $('.table > tbody > tr').each(function() { 
     var friendid = $(this).find("td:first").html(); 
     var userid = Meteor.userId(); 
     alert('this rendered inside each'); 
     }); 
     }); 

,這是模板

Template name="drone"> 
       <table class="table"> 
     <thead> 
      <tr> 
       <th>Id</th> 
       <th>Names</th> 
       <th>Action</th> 
      </tr> 
     </thead> 
     <tbody> 
     {{#each allusers }} 
      <tr> 
       <td class="buttonId">{{_id}}</td> 
       <td>{{profile.name}}</td> 
       <td class="buttonAction">hi</td> 
      </tr> 
      {{/each}} 
     </tbody> 
    </table> 
</template> 

上面迭代的代碼表,並做一些事情與數據非常有用,從錶行,即id在每個td。

我想知道爲什麼這個代碼alert('this rendered inside each');永遠不會觸發。

回答

0

我敢打賭$('.table > tbody > tr')與任何東西都不匹配。

您位於allusers迭代的「父」模板上下文中。在此階段,您的tr尚未呈現。

你應該嘗試做一個名爲userLine新的模板:

<template name="userLine"> 
<tr> 
    <td class="buttonId">{{_id}}</td> 
    <td>{{profile.name}}</td> 
    <td class="buttonAction">hi</td> 
</tr> 
</template> 

然後,替換您的#each由:

{{#each allusers }} 
    {{ >userLine }} 
{{/each}} 

現在,您可以使用onRendered鉤上這個新的模板,它應該隨心所欲地射擊。