2013-05-09 126 views
1

我有与渲染函数中的模板创建DataTable的行查看:刷新一个jQuery的dataTable

render: function(){ 

    this.collection.fetch({async:false}); 
    var tmpl = _.template(this.template); 
    var that = this; 
    _.each(this.collection.toJSON(), function(item) { 
     $(that.el).append(tmpl(item)); 
    }); 

    return this; 
}, 

这之后,我初始化数据表是这样的:

$("#idTable").dataTable(); 

我有另一种观点,修改/添加集合调用模型后,再次渲染该数据表的重绘。 修改/添加的行在表格中正确显示,但如果使用分页或搜索框,修改/添加的行会消失并显示旧的行。

如何刷新数据表?

我发现只有这样:How do I tell dataTables to check for updated data in a backbone collection?,但我需要使用Undercore的模板创建数据表。

回答

0
+1

我看过了,但大家都在谈论fnReloadajax。在我的情况下,它不,我猜是因为我使用下划线模板构建表格,而不是像这样:$(“#example”)。dataTable({“{”sTitle“:”Id“}) , { “sTitle”: “姓名”}], “bProcessing”:真实, “bServerSide”:真实, “sAjaxSource”: '@ Url.Action( “FetchData”, “家”)' , “sPaginationType”:“full_numbers”, }); – riccio82 2013-05-09 22:09:34

0

我通过删除每一次而不是仅仅是表来解决它,即使是在它周围创建jQuery的包装器div,每次都用新集合重新绘制整个表。

我发现这里的解决方案:How to refresh DataTables

If doing a complete reload of the whole table, wrap your initial datatables 
initialization code in a function. Call that function on page load. When replacing 
the table completely with ajax you likely should remove the table parent div that is 
created by plugin as a wrapper for all the non table DOm elements it creates. 
If table ID="example" , wrapper id="example_wrapper". 

谢谢!