Backbone.js 如何使用下划线将数据放入表中?
我有一个问题-如何将方法fetch()中的数据放入HTML? 因为当我试着这么做的时候,什么都没有Backbone.js 如何使用下划线将数据放入表中?,backbone.js,underscore.js,Backbone.js,Underscore.js,我有一个问题-如何将方法fetch()中的数据放入HTML? 因为当我试着这么做的时候,什么都没有 define([ "underscore", "backbone", "jquery", "pages/RestaurantPage/collections/UsersCollection", "text!pages/RestaurantPage/templates/UsersTable.html" ],
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>
所有用户
名称
姓
登录
电子邮件
角色
编辑
删除
我想在表中呈现我的收藏…我能做什么?
因为当我尝试渲染它时,我得到的是一个没有数据的空白页面。您正在制作一个空div的模板。您想制作一个HTML模板,如下所示:
<script type="text/template" id="my_template">
<table align="center" border="1" cellpadding="1" cellspacing="1" >
...
</table>
</script>
哪个元素的id为“数据表”?我希望它出现在您的表上
“data\u table”是y索引页中元素的ID
var template = _.template($('#my_template').html(), {
'users': users.toArray()
});