Last active
January 4, 2018 14:34
-
-
Save jakl/6612826 to your computer and use it in GitHub Desktop.
Rails Table Partial
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<td width="<%= width %>"> | |
<% if cell %> | |
<%= render partial: cell_partial, object: cell %> | |
<% end %> | |
</td> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<tr> | |
<%= render partial: 'cell', collection: row, | |
locals: { | |
cell_partial: cell_partial, | |
width: "#{100/row.length}%", | |
} %> | |
</tr> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<% # Nil elements in the rows will be rendered as empty cells %> | |
<table class="table table-no-hover table-bordered full-width table-show-empty"> | |
<tr><td colspan="100%"> | |
<%= render partial: table[:header_partial], object: table[:header], | |
as: 'header' %> | |
</td></tr> | |
<%= render partial: 'row', collection: table[:rows], | |
locals: { cell_partial: table[:cell_partial] } %> | |
</table> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div><%= thing.name %></div> | |
<div><%= thing.description %></div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div>We have <%= count %> things</div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
things = Thing.all # My self descriptive model | |
@table = { | |
header_partial: 'thing_header', | |
header: { | |
count: things.length, | |
}, | |
cell_partial: 'thing', | |
rows: groups_of(2, things), | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def groups_of(length, arr) | |
arr.reduce([[]]) do |group, elem| | |
if group.last.length != length | |
group.last.push(elem) | |
else | |
group.push([elem]) | |
end | |
group | |
end.tap do |groups| | |
# Fill in gaps in last group with nils | |
(groups.last.length...length).each do | |
groups.last.push(nil) | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.table-show-empty { | |
empty-cells: show; | |
} | |
.full-width { | |
width: 100%; | |
} | |
.table-no-hover { | |
tbody tr:hover td, | |
tbody tr:hover th { | |
background-color: transparent; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For CSS:
table-bordered is from boostrap
table-no-hover was just my personal bug, might do nothing in isolation like this