2016-03-11 46 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提交後彈出不關閉,這是令人困惑的 –

相關問題