1

我有一些带有 Handlebars 模板的 HTML/表格数据:

<div class="users">
    <script id="userlist" type="text/x-handlebars-template"> 
        <table class="table table-striped table-hover table-condensed">
            <thead>
                <th>Username</th> 
                <th>Real Name</th> 
                <th>Email</th> 
            </thead>
            <tbody>
                {{#each this}}     
                <tr>
                    <td>{{name}}</td>
                    <td>{{email}} {{lastName}}</td> 
                    <td>{{time}}</td> 
                </tr> 
                {{/each}} 
            </tbody> 
        </table> 
    </script> 
</div>

我想找到最优雅/优化的方法来有条件地识别表格行,以便根据我从 JSON 中获得的值突出显示该行。

这段代码从我的 PHP 类中获取 JSON:

$("#register").submit(function(event) {

    /* stop form from submitting normally */
    event.preventDefault();

    /* get some values from elements on the page: */
    var $form = $( this ),
        name = $form.find( 'input[name="name"]' ).val(),
        email = $form.find( 'input[name="email"]' ).val(), 
        func  

    /* Send the data using post */
    var posting = $.post( 'controller.php', { name: name, email: email, func: 'register'  } );

    /* Put the results into a template */
    posting.done(function(data) {
        var data = $.parseJSON(data);
        var userlist = Handlebars.compile( $("#userlist").html() );
        $(".users").html(userlist(data));
    }); /*end function */

});

我可以看到我的数据列表没问题。这很好用。我觉得我需要做一个 registerHelper 或一个 registerPartial 但不确定。

4

1 回答 1

0

如果你有一个布尔属性,比如说active,设置在你的对象上,表示一个活动的行,那么你只需在你的模板中做这样的事情:

{{#each this}}
<tr{{#if active}} class="active"{{/if}}>
    <td>{{name}}</td>
    <td>{{email}} {{lastName}}</td> 
    <td>{{time}}</td> 
</tr> 
{{/each}}
于 2013-02-09T06:27:18.243 回答