Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add table footer support #128

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ To use table placeholders, you should create a Table in your document and give i

If you inform `header: true`, the first row will be treated as a *header* and left untouched. The remaining rows will be used as the template for the table.

If you inform `footer: true`, the last row will be treated as a *footer* and left untouched. The remaining rows will be used as the template for the table.

If you have more than one template row, they will be cycled. This is usefull for making zebra tables.

As with **Field placeholders**, just insert a `[FIELD_NAME]` in each cell and let the magic takes place.
Expand All @@ -56,7 +58,7 @@ report = ODFReport::Report.new("Users/john/my_template.odt") do |r|
r.add_field "USER_NAME", @user.nome
r.add_field "ADDRESS", @user.address

r.add_table("TABLE_1", @list_of_items, :header=>true) do |t|
r.add_table("TABLE_1", @list_of_items, :header=>true, :footer=>true) do |t|
t.add_column(:item_id, :id)
t.add_column(:description) { |item| "==> #{item.description}" }
end
Expand Down
6 changes: 5 additions & 1 deletion lib/odf-report/report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ def add_text(field_tag, value='')
end

def add_table(table_name, collection, opts={})
opts.merge!(:name => table_name, :collection => collection)
opts.merge!(
:name => table_name,
:collection => collection,
:parent_fields => @fields
)
tab = Table.new(opts)
@tables << tab

Expand Down
17 changes: 15 additions & 2 deletions lib/odf-report/table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ class Table < Nestable
def initialize(opts)
super(opts)

@template_rows = []
@template_rows = []
@header = opts[:header] || false
@skip_if_empty = opts[:skip_if_empty] || false
@parent_fields = opts[:parent_fields]
@footer = opts[:footer] || false
end

def replace!(doc)
Expand All @@ -16,6 +18,8 @@ def replace!(doc)

@header = table.xpath("table:table-header-rows").empty? ? @header : false

@footer = ((@header && template_length > 2) || (!@header && template_length > 1)) ? @footer : false

if @skip_if_empty && @data_source.empty?
table.remove
return
Expand All @@ -35,6 +39,12 @@ def replace!(doc)

end

if @footer
node = get_footer_row
@parent_fields.each { |f| f.replace!(node) }
table.add_child(node)
end

@template_rows.each_with_index do |r, i|
r.remove if (get_start_node..template_length) === i
end
Expand All @@ -52,7 +62,7 @@ def get_next_row
@row_cursor = get_start_node unless defined?(@row_cursor)

ret = @template_rows[@row_cursor]
if @template_rows.size == @row_cursor + 1
if @template_rows.size == @row_cursor + (@footer ? 2 : 1)
@row_cursor = get_start_node
else
@row_cursor += 1
Expand All @@ -78,5 +88,8 @@ def deep_clone(node)
Nokogiri::XML(wrap_with_ns(node)).at_xpath("//table:table-row")
end

def get_footer_row
@template_rows.last.dup
end
end
end