javascript – 在骨干视图中如何以及在何处初始化jquery数据表
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript – 在骨干视图中如何以及在何处初始化jquery数据表,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2507字,纯文字阅读大概需要4分钟。
内容图文
我的html模板看起来像这样:
<script type="text/template" id="players-template">
<table id="example" class="table table-striped table-bordered table-condensed table-hover">
<thead>
<tr>
<th>Name</th>
<th>group</th>
<th></th>
</tr>
</thead>
<tbody id="playersTable"></tbody>
</table>
</script>
<script type="text/template" id="player-list-item-template">
<td><@= name @></td>
<td>
<@ _.each(hroups, function(group) { @>
<@= group.role @>
<@ }); @>
</td>
</script>
我的骨干观点如下:
playerView = Backbone.View.extend({
template: _.template( $("#player-template").html() ),
initialize: function ()
if(this.collection){
this.collection.fetch();
},
render: function () {
this.$el.html( this.template );
this.collection.each(function(player) {
var itemView = new app.PlayerListItemView({ model: player });
itemView.render();
this.$el.find('#playersTable').append(itemView.$el);
},this
});
// view to generate each player for list of players
PlayerListItemView = Backbone.View.extend({
template: _.template($('#player-list-item-template').html()),
tagName: "tr",
render: function (eventName) {
this.$el.html( this.template(this.model.toJSON()) );
}
});
上面的代码完美无缺.现在,我想使用apply jquery datatable插件和bootstrap支持.你可以在这里找到详细信息:http://www.datatables.net/blog/Twitter_Bootstrap_2
所以,我刚刚在render中添加了一行:
render: function () {
this.$el.html( this.template );
this.collection.each(function(player) {
var itemView = new app.PlayerListItemView({ model: player });
itemView.render();
this.$el.find('#playersTable').append(itemView.$el);
$('#example').dataTable( {
console.log('datatable');
"sDom": "<'row'<'span6'l><'span6'f>r>t<'row'<'span6'i> <'span6'p>>",
"sPaginationType": "bootstrap",
"oLanguage": {
"sLengthMenu": "_MENU_ records per page"
},
"aoColumnDefs": [
{ 'bSortable': false, 'aTargets': [ 2 ] }
]
} );
},this);
},
现在,jquery datable未初始化.他们只是显示正常表.
>我应该在哪里初始化表以应用jquery数据表?
>他们完美无缺地工作.
解决方法:
最有可能的是,jQuery插件需要页面上的元素才能工作.你没有在那个视图上显示你在哪里调用render,但是我假设你正在做这样的事情:
var view = new PlayerView();
$('#foo').html(view.render().el); // this renders, then adds to page
如果这是真的,那么在渲染中使用插件太早,因为视图的html尚未添加到页面中.
你可以试试这个:
var view = new PlayerView();
$('#foo').html(view.el); // add the view to page before rendering
view.render();
或者你可以尝试这个:
var view = new PlayerView();
$('#foo').html(view.render().el);
view.setupDataTable(); // setup the jQuery plugin after rendering and adding to page
内容总结
以上是互联网集市为您收集整理的javascript – 在骨干视图中如何以及在何处初始化jquery数据表全部内容,希望文章能够帮你解决javascript – 在骨干视图中如何以及在何处初始化jquery数据表所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。