简体   繁体   English

如何使用js动态向数据表行添加数据链接

[英]How to add data-link dynamically to datatables row with js

I have table with clickable rows which renders the controller show action , in order to add new record to the database there is a button that render the new action and opens up a popup without refreshing the page, after submit the form in the popup it dynamically adds the new row to the datatables without refreshing, all the previous are working fine excepts the newly added row does is not clickable here is the table code 我有带有可点击行的表,该表可呈现控制器的show action,为了向数据库中添加新记录,有一个按钮可呈现新动作并在不刷新页面的情况下打开一个弹出窗口,而无需刷新页面,在动态提交表单之后在不刷新的情况下将新行添加到数据表中,所有以前的都工作正常,但新添加的行不可单击,这是表代码

<%- 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>

the create.js.erb file code : create.js.erb文件代码:

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

how to add ( data-link="<%= supplier_path(supplier) %>" ) to this new tr 如何将(data-link =“ <%= provider_path(supplier)%>”)添加到此新tr

Couldn't you do something like $(tr).data("link", "<%= supplier_path(supplier) %>"); 您是否可以做类似$(tr).data("link", "<%= supplier_path(supplier) %>");事情$(tr).data("link", "<%= supplier_path(supplier) %>"); .

Maybe it's not the most elegant solution, but you can put callback on table redraw event, like this 也许这不是最优雅的解决方案,但是您可以像这样在表重绘事件中添加回调

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

I think it wouldn't be added without a callback. 我认为没有回调就不会添加它。

I found a solution @ datatables reference 我找到了解决方案@ datatables参考

i added to create.js.erb 我添加到create.js.erb

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

and added to the js file 并添加到js文件

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

seems like i should added .nodes() .to$() so that i can add attribute 似乎我应该添加.nodes().to $()以便我可以添加属性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM