2016-03-11 45 views
0

我使用可点击行的表格来呈现控制器显示动作, 为了向数据库添加新记录,有一个按钮用于呈现新动作并打开一个弹出式菜单,不需刷新页面,在弹出框中提交表单后动态地将新行添加到数据表中而无需刷新,以前的所有工作都正常,除了新添加的行不可点击以外, 这里是表代码如何使用js将数据链接动态添加到数据表行

<%- model_class = Supplier -%> 
<table class="table table-custom pointer" id="editable-usage"> 
<thead> 
<tr> 
    <th class="table-text-center"><%= model_class.human_attribute_name(:name) %></th> 
    <th class="table-text-center"><%= model_class.human_attribute_name(:code) %></th> 
    <th class="table-text-center"><%= model_class.human_attribute_name(:email) %></th> 
    <th class="table-text-center"><%= model_class.human_attribute_name(:total_credit) %></th> 
    <th class="table-text-center"><%= model_class.human_attribute_name(:notes) %></th> 
</tr> 
</thead> 
<tbody> 
<% @suppliers.each do |supplier| %> 
    <tr data-link="<%= supplier_path(supplier) %>"> 
    <td><%= supplier.name %></td> 
    <td><%= supplier.id %></td> 
    <td><%= supplier.email %></td> 
    <td><%= supplier.total_credit %></td> 
    <td><%= supplier.notes %></td> 
    </tr> 
<% end %> 
</tbody> 
</table> 

create.js.erb文件代码:

// hide the popup 
$('#splash').modal('hide'); 
// insert the new row 
var t = $('#editable-usage').DataTable(); 
t.row.add([ '<%= @supplier.name %>','SC00<%[email protected] %>','<%[email protected] %>','','<%[email protected] %>']).draw(); 

如何添加(数据链路= “<%= supplier_path(供应商)%>”)这一新潮流

回答

0

我找到了解决办法@datatables reference

我加入create.js.erb

t.row.add([ '<%= @supplier.name %>','SC00<%[email protected] %>','<%[email protected] %>','','<%[email protected] %>']).draw().nodes().to$().attr("data-link", "<%= supplier_path(@supplier) %>"); 

,并添加到js文件

var oTable = $('#editable-usage').DataTable(); 
oTable.on('draw', function() { 
    $("tr[data-link]").click(function() { 
     window.location = this.dataset.link 
    }); 
}); 

好像我应该加入.nodes( ) 。到$(),以便我可以添加属性

0

你不能这样做$(tr).data("link", "<%= supplier_path(supplier) %>");

+0

其不工作我试过var t = $('#editable-usage')。DataTable(); t.row.add(['<%= @ supplier.name%>','SC00 <%= @ supplier.id%>','<%= @ supplier.email%>','','< %(@supplier.notes%>'])。draw(); t.row.data(“link”,“<%= supplier_path(@ supplier.id)%>”); –

0

也许这不是最优雅的解决方案,但你可以把桌子上重绘回调事件,像这样

t.on('draw', function() { 
    $(t+'>tr').attr(data-link="<%= supplier_path(supplier) %>"); 
}); 

我想会不会没有回调被添加。

+0

它不起作用,加上在控制台中没有js erros提交后弹出不关闭,这是令人困惑的 –

相关问题