Skip to content

Commit

Permalink
Implementing Rails Advanced Table Custom Cell with kits
Browse files Browse the repository at this point in the history
  • Loading branch information
carloslimasd committed Nov 26, 2024
1 parent 5bcb375 commit dd8413e
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@
}
] %>
<%= pb_rails("advanced_table", props: {table_data: @table_data, column_definitions: column_definitions}) %>
<%= pb_rails("advanced_table", props: { id: "beta_table", table_data: @table_data, column_definitions: column_definitions }) %>
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,5 @@
<%= pb_rails("advanced_table", props: { table_data: @table_data, column_definitions: column_definitions }) do %>
<%= pb_rails("advanced_table/table_header", props: { column_definitions: column_definitions }) %>
<%= pb_rails("advanced_table/table_body", props: { id: "subrow_headers", table_data: @table_data, column_definitions: column_definitions, subrow_headers: subrow_headers, enable_toggle_expansion: "all" }) %>
<%= pb_rails("advanced_table/table_body", props: { id: "beta_sort", table_data: @table_data, column_definitions: column_definitions, subrow_headers: subrow_headers, enable_toggle_expansion: "all" }) %>
<% end %>
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<%
column_definitions = [
{
accessor: "year",
label: "Year",
cellAccessors: ["quarter", "month", "day"],
custom_renderer: ->(row, value) {
capture do
pb_rails("flex") do
pb_rails("title", props: { text: value, size: 4 }) +
pb_rails("badge", props: { dark: true, margin_left: "xxs", text: row[:newEnrollments].to_i > 20 ? "High" : "Low", variant: "neutral" })
end
end
}
},
{
accessor: "newEnrollments",
label: "New Enrollments",
custom_renderer: ->(row, value) { pb_rails("pill", props: { text: value, variant: "success" }) }
},
{
accessor: "scheduledMeetings",
label: "Scheduled Meetings",
custom_renderer: ->(row, value) { content_tag(:a, value, href: "#") }
},
{
accessor: "attendanceRate",
label: "Attendance Rate",
custom_renderer: ->(row, value) {
capture do
pb_rails("flex", props: { align_items: "end", orientation: "column" }) do
pb_rails("detail", props: { bold: true, color: "default", text: value }) +
pb_rails("caption", props: { size: "xs", text: row[:graduatedStudents] })
end
end
}
},
{
accessor: "completedClasses",
label: "Completed Classes",
},
{
accessor: "classCompletionRate",
label: "Class Completion Rate",
},
{
accessor: "graduatedStudents",
label: "Graduated Students",
}
]
%>
<%= pb_rails("advanced_table", props: { id: "custom_cell", table_data: @table_data, column_definitions: column_definitions }) %>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
The Advanced Table also allows for rendering custom components within individual Cells. To achieve this, you can make use of the optional `custom_renderer` item within each column_definitions. This function gives you access to the current Cell's value if you just want to use that with a custom Kit, but it also gives you access to the entire `row` object. The row object provides all data for the current row.

See [here](https://playbook.powerapp.cloud/kits/advanced_table/rails#column_definitions) for more indepth information on columnDefinitions are how to use them.

See [here](https://github.com/powerhome/playbook/tree/master/playbook/app/pb_kits/playbook/pb_advanced_table#readme) for the structure of the tableData used.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ examples:
- advanced_table_beta: Default (Required Props)
- advanced_table_beta_subrow_headers: SubRow Headers
- advanced_table_beta_sort: Enable Sorting
- advanced_table_custom_cell_rails: Custom Components for Cells

react:
- advanced_table_default: Default (Required Props)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
</button>
<% end %>
<%= pb_rails("flex/flex_item", props:{padding_left: index.zero? && object.row[:children].present? ? "none" : "xs"}) do %>
<% if index.zero? %>
<% if column[:custom_renderer].present? %>
<%= raw(column[:custom_renderer].call(object.row, custom_renderer_value(column, index))) %>
<% elsif index.zero? %>
<% if object.depth.zero? %>
<%= object.row[column[:accessor].to_sym] %>
<% else %>
Expand Down
15 changes: 15 additions & 0 deletions playbook/app/pb_kits/playbook/pb_advanced_table/table_row.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,21 @@ def depth_accessors

private

def custom_renderer_value(column, index)
if index.zero?
if depth.zero?
row[column[:accessor].to_sym]
else
depth_accessors.each_with_index do |item, accessor_index|
key = item.to_sym
return row[key] if depth - 1 == accessor_index
end
end
else
row[column[:accessor].to_sym]
end
end

def subrow_depth_classname
depth.positive? ? "depth-sub-row-#{depth}" : ""
end
Expand Down

0 comments on commit dd8413e

Please sign in to comment.