2016-09-18 68 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钩上这个新的模板,它应该随心所欲地射击。