in my app that I am building to learn rails and js, I have this case re. to expanding a table row to show a partial. to help understand, see this image:
The CSS for edit-tag can set to display "none"; this is the table:
<table id="tags" class="table table-hover" style="background-color: white; word-wrap: break-word; font-size: 0.9em;" >
<thead>
<tr>
<th>Tagged content</th>
<th>as</th>
<th>in</th>
<th></th>
</tr>
</thead>
<tbody>
<% object.tags.each do |tag| %>
<% unless tag.content.blank? %>
<tr>
<td style="word-wrap: break-word;"><%= link_to tag.content, [object, tag], method: :patch %></td>
<td><%= tag.tagtype.name %></td>
<td><%= tag.tagtype.typeoftag %></td>
<td><%= link_to '', [object, tag], method: :delete, data: { confirm: 'Please confirm deletion!' }, :class => "glyphicon glyphicon-trash" %></td>
<tr id='edit-tag'>
<td colspan="5"><%= render 'tags/tag_update' %></td>
</tr>
</tr>
<% end -%>
<% end -%>
</tbody>
I created this function in JS yet it uses ID
$(function() {
var DisplayTagEdit = document.getElementById('DisplayTagEdit');
DisplayTagEdit.onclick = function() {
var edittag = document.getElementById('edit-tag');
if (edittag.style.display !== 'none') {
edittag.style.display = 'none';
}
else {
edittag.style.display = 'block'
}
}
});
So my issues are:
Use unique data attribute or class related to object'id to identify them, like:
<tr data-tag-id='<%= tag.id >' class='show-tag'>
<td style="word-wrap: break-word;"><%= link_to tag.content, [object, tag], method: :patch %></td>
<td><%= tag.tagtype.name %></td>
<td><%= tag.tagtype.typeoftag %></td>
<td><%= link_to '', [object, tag], method: :delete, data: { confirm: 'Please confirm deletion!' }, :class => "glyphicon glyphicon-trash" %></td>
</tr>
<tr data-tag-id='<%= tag.id %>' class='edit-tag'>
<td colspan="5"><%= render 'tags/tag_update' %></td>
</tr>
And in js(I use Jquery here):
$(".edit-tag").on("click", function(event) {
var tagId = $(this).data("tagId");
var $showRow = $("tr.show-tag[data-tag-id=" + tagId + "]"); // here you get the references of the row you need.
// do want you want ................
$showRow.toggle();
});
And in Rails, we have "conteng_tag_for", which add the id information to dom element.
<%= content_tag_for :tr, tag do %>
<td style="word-wrap: break-word;"><%= link_to tag.content, [object, tag], method: :patch %></td>
<td><%= tag.tagtype.name %></td>
<td><%= tag.tagtype.typeoftag %></td>
<td><%= link_to '', [object, tag], method: :delete, data: { confirm: 'Please confirm deletion!' }, :class => "glyphicon glyphicon-trash" %></td>
<% end %>
will generate html like:
<tr id='tags_16' class='tags'> <!-- assume tag'id is 16 -->
<td>......</td>
<td>......</td>
<td>......</td>
<td>......</td>
</tr>
This works (including show and hide other rows) generically:
$("td.show-tag").on("click", function(event) {
var tagId = $(this).data('tagid');
$('tr.edit-tag').not($('tr.edit-tag[data-tagid='+tagId+']')).hide();
$('tr.edit-tag[data-tagid='+tagId+']').toggle();
});
Update
Code above uses display: block To use display: table-row (to span the row with one td across all columns) use .css()
$("#tags td.show-tag").on("click", function(event) {
var tagId = $(this).data('tagid');
$('tr.edit-tag').not($('tr.edit-tag[data-tagid='+tagId+']')).css("display", "none");
$('tr.edit-tag[data-tagid='+tagId+']').css("display", "table-row");
});
Yet not yet in my rails view.
No need for own JS! Achievable with Bootstap's collapse.js
applied to the table.
<tr id='heading<%= tag.id %>', role="button", data-toggle="collapse", data-parent="tags", class='show-tag', href="#collapse<%= tag.id %>", aria-expanded="true" aria-controls="collapse<%= tag.id %>">
<td style="word-wrap: break-word;"><%= tag.content %></td>
<td><%= tag.tagtype.name %></td>
<td><%= tag.tagtype.typeoftag %></td>
<td class="delete-btn"><%= link_to '', [object, tag], method: :delete, data: { confirm: 'Please confirm!' }, :class => "glyphicon glyphicon-trash" %></td>
</tr>
<tr id='collapse<%= tag.id %>', class='collapse' aria-labelledby="collapse"<%= tag.id %>">
<td colspan="4"><%= render 'shared/tester' %><%#= render partial: 'tags/update', object: @tag.tagable, tag: @tag %></td>
</tr>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.