Dre*_*der 7 html javascript jquery ruby-on-rails cocoon-gem
根据https://github.com/nathanvda/cocoon#link_to_add_association你应该能够传递一个功能data-association-insertion-node
我试过这个:
<%= link_to_add_association 'Add Timeslot', f, :timeslots, :data => {'association-insertion-node' => 'get_row()'} %>
Run Code Online (Sandbox Code Playgroud)
还有这个:
<%= link_to_add_association 'Add Timeslot', f, :timeslots, :data => {'association-insertion-node' => 'get_row'} %>
Run Code Online (Sandbox Code Playgroud)
而这(变得绝望):
<%= link_to_add_association 'Add Timeslot', f, :timeslots, :data => {'association-insertion-node' => 'function get_row(node){var row = "<tr></tr>";$("#table_body").append(row);return row;}'} %>
Run Code Online (Sandbox Code Playgroud)
但它们都不起作用.
使用Javascript:
function get_row(node){
var row = "<tr></tr>"
$("#table_body").append(row);
return row
}
Run Code Online (Sandbox Code Playgroud)
我想一个添加tr到table再追加嵌套时隙形式向tr.
目前,您正在尝试做的事情是不可能的。当这样设置时data-association-insertion-method,它只是 javascript 中的一个字符串,而不是对方法的引用。
如自述文件中所述,您必须执行以下操作(假设您为链接指定了 class .add-timeslot):
$(document).on('turbolinks:load', function() {
$(".add-timeslot a").
data("association-insertion-method", 'append').
data("association-insertion-node", function(link){
return link.closest('.row').next('.row').find('.sub_tasks_form')
});
});
Run Code Online (Sandbox Code Playgroud)
(该示例几乎是从文档中逐字复制的,仅用于演示目的——您必须填写您的get_row函数)