2014-04-22 111 views
0

我有一個問題 - 如何將數據從方法fetch()轉換爲HTML? 因爲當我試圖做到這一點,沒有渲染!如何使用Underscore將數據放入表中?

define([ 
     "underscore", 
     "backbone", 
     "jquery", 
     "pages/RestaurantPage/collections/UsersCollection", 
     "text!pages/RestaurantPage/templates/UsersTable.html" 
    ], 

    function(_, Backbone, $, UsersCollection, UsersTable) { 
     return Backbone.View.extend({ 
      el: $('#data_table'), 
      render: function() { 
       that = this; 
       var users = new UsersCollection; 
       users.fetch({ 
        success: function(users) { 
         var template = _.template($('#data_table').html(), { 
          'users': users.toArray() 
         }); 
        that.$el.html(UsersTable); 
        } 
       }); 
      } 
     }); 
    }); 

,我的HTML文件:

<table align="center" border="1" cellpadding="1" cellspacing="1" > 


    <caption> 
    All users 
    </caption> 

    <thead> 
    <tr> 

     <th scope="col">Name</th> 

     <th scope="col">Last name</th> 

     <th scope="col">Login</th> 

     <th scope="col">Email</th> 

     <th scope="col">Role</th> 

     <th scope="col">Edit</th> 

     <th scope="col">Delete</th> 

     <th scope="col"> 
     <form id="deleteall" method="post" action="/deleteall" name="deleteall"></form> 

     <div class="small success btn"> 
      <input type="submit" value="X" form="deleteall" /> 
     </div> 
     </th> 

    </tr> 
    </thead> 

    <tbody> 

<% _.each(users, function(user, key, list) { %> 
    <tr> 
     <td><%= key %></td> 

     <td><%- user.get('l_name') %></td> 

     <td><%- user.get('f_name') %></td> 

     <td><%- user.get('email') %></td> 

     <td><%- user.get('id_role') %></td> 
<% }) %> 

     <td> 

    </tr> 
    </tbody> 

</table> 

我想渲染我的表集...我能做些什麼? 因爲當我嘗試渲染它,我得到空白頁面沒有數據。

+0

什麼元素都有ID「data_table」?我希望它在你的'表' – Hazaart

+0

「data_table」是ID爲

元素,在y索引頁 – SemoleX

回答

0

您正在製作一個空格的模板。你想使你的HTML模板,例如:

<script type="text/template" id="my_template"> 
    <table align="center" border="1" cellpadding="1" cellspacing="1" > 
     ... 
    </table> 
</script> 

然後在模板函數中使用的模板的ID:

var template = _.template($('#my_template').html(), { 
     'users': users.toArray() 
}); 
相關問題