【问题标题】:how and where to initialize jquery datatable in backbone view在主干视图中如何以及在何处初始化 jquery 数据表
【发布时间】:2013-03-20 12:14:12
【问题描述】:

我的 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()) );
         }
  });

上面的代码完美运行。现在,我想使用带有引导支持的应用 jquery 数据表插件。您可以在这里找到详细信息:http://www.datatables.net/blog/Twitter_Bootstrap_2 所以,我只是在渲染中添加了一行:

        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 数据表没有初始化。他们只是显示普通的表格。

  • 我应该在哪里初始化表以应用 jquery 数据表?
  • 他们在没有骨干的情况下工作得很好。

【问题讨论】:

    标签: javascript jquery-plugins backbone.js datatable backbone-views


    【解决方案1】:

    很可能,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
    

    【讨论】:

    • render中,如果你写this.$el.html(this.template(this.model.toJSON())),那么是否可以调用this.setupDataTable(),只要你在实例化数据表时写this.$('#tableId').DataTable(..)?这样,jQuery 选择器的作用域就是视图的$el。编辑:我试过这个并收到一个关于缺少 aDataSort 的错误。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-19
    • 2011-12-16
    • 2012-11-13
    • 2016-11-23
    • 2018-02-24
    • 2016-12-05
    相关资源
    最近更新 更多