gpt4 book ai didi

javascript - 具有功能的 Cocoon 数据关联插入节点

转载 作者:太空狗 更新时间:2023-10-29 15:10:24 28 4
gpt4 key购买 nike

根据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()'} %>

还有这个:

<%= link_to_add_association 'Add Timeslot', f, :timeslots, :data => {'association-insertion-node' => 'get_row'} %>

还有这个(变得绝望):

<%= 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;}'} %>

但它们都不起作用。

Javascript:

function get_row(node){
var row = "<tr></tr>"
$("#table_body").append(row);
return row
}

我正在尝试将 tr 添加到 table,然后将嵌套的时隙形式附加到 tr

最佳答案

目前您无法执行此操作。当像这样设置 data-association-insertion-method 时,它只是 javascript 中的一个字符串,而不是对方法的引用。

作为documented在 README 中,您必须执行以下操作(假设您为链接指定了一个类 .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')
});
});

(该示例几乎是从文档中逐字复制的,仅用于演示目的——您必须填写您的 get_row 函数)

关于javascript - 具有功能的 Cocoon 数据关联插入节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42709119/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com