简体   繁体   English

使用js.erb文件重命名td而不是替换它?

[英]Use js.erb file to rename td instead of replace it?

I've got a page that toggles fields back and forth on button presses. 我有一个页面,可以在按下按钮时来回切换字段。 So far, my only solution is to put a nested td inside the original one making the page look bad. 到目前为止,我唯一的解决方案是在原始页面中放入嵌套的td,使页面看起来很糟糕。 You can see the tunnel effect of tds within tds: 您可以在tds中看到tds的隧道效应:

格式不正确

Is there a way to rename the containing td instead of putting a new one inside it. 有没有一种方法可以重命名包含的td而不是在其中放置一个新的td。 I have to swap the functionality each time so it's necessary to rename them. 我每次都必须交换功能,因此有必要对其进行重命名。 Here's the js.erb for each: 这是每个的js.erb:

remove.js.erb: remove.js.erb:

<% if @pos_range %>
    <% @pos_range.each do |p|%>
        $("td#remove_name_<%= p %>").html("<%= escape_javascript(render(:partial => 'some_partial'))%>");
        $("td#remove_button_<%= p %>").html("<%= escape_javascript(render(:partial => 'some_partial_2'))%>");
    <% end %>
<% else %>
    $("td#remove_name_<%= @td_num %>").html("<%= escape_javascript(render(:partial => 'some_partial'))%>");
    $("td#remove_button_<%= @td_num %>").html("<%= escape_javascript(render(:partial => 'some_partial_2'))%>");
<% end %>

update.js.erb: update.js.erb:

<% @pos_range.each do |p|%>
    $("td#add_device_<%= p %>").html("<%= escape_javascript(render(:partial => 'some_other_partial', :locals => { :id => @id, :position_str => @position_str} ))%>");
    $("td#add_device_button_<%= p %>").html("<%= escape_javascript(render(:partial => 'some_other_partial_2', :locals => { :id => @id, :position_str => @position_str} ))%>");
<% end %>

And here are the html.erb files: 这是html.erb文件:

some_partial: some_partial:

<td id=<%="add_device_#{@i.to_s}"%>>
    Nothing assigned
</td>

some_partial_2: some_partial_2:

<td id=<%="add_device_button_#{@i.to_s}"%>>
    <%= link_to "Change", {:controller => :some_controller, :action => :show, :id => @id, :position => @i }, :remote => true, :class => "btn" %>
</td>

some_other_partial: some_other_partial:

<td id=<%="remove_name_#{@i.to_s}"%>>
    <%= @position_str %>&nbsp;
</td>

some_other_partial_2: some_other_partial_2:

<td id=<%="remove_button_#{@i.to_s}"%>>
    <%= link_to "Remove", { :controller => :some_controller, :action => :remove, :id => @id, :position => @i}, :remote => true, :class => "btn btn-danger" %>
</td>

the jQuery command .html replaces the content of the selector with new content. jQuery命令.html用新内容替换选择器的内容。 What you want is to replace the entire node 您要替换整个节点

    $("td#remove_name_<%= p %>").replaceWith("<%= j render partial: 'some_partial' %>");

Here's a working example showing you: http://jsfiddle.net/K4c9x/ 这是一个工作示例,向您显示: http : //jsfiddle.net/K4c9x/

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

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