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

Allow to set explicit column widths for tables #115

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
7 changes: 7 additions & 0 deletions lib/caracal/core/models/table_model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class TableModel < BaseModel
attr_reader :table_border_right # returns border model
attr_reader :table_border_horizontal # returns border model
attr_reader :table_border_vertical # returns border model
attr_reader :table_column_widths

# initialization
def initialize(options={}, &block)
Expand Down Expand Up @@ -148,6 +149,11 @@ def cell_style(models, options={})
instance_variable_set("@table_#{ m }", value.to_s.to_sym)
end
end

# column widths
def column_widths(value)
@table_column_widths = value.map(&:to_i) if value.is_a?(Array)
end

# .data
def data(value)
Expand Down Expand Up @@ -196,6 +202,7 @@ def option_keys
k << [:data, :align, :width]
k << [:border_color, :border_line, :border_size, :border_spacing]
k << [:border_bottom, :border_left, :border_right, :border_top, :border_horizontal, :border_vertical]
k << [:column_widths]
k.flatten
end

Expand Down
18 changes: 10 additions & 8 deletions lib/caracal/renderers/document_renderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -332,17 +332,19 @@ def render_table(xml, model)
xml['w'].tblLook({ 'w:val' => '0600' })
end
xml['w'].tblGrid do
model.rows.first.each do |tc|
(tc.cell_colspan || 1).times do
xml['w'].gridCol({ 'w:w' => tc.cell_width })
end
column_widths = model.table_column_widths
column_widths ||= model.rows.first.map do |tc|
[tc.cell_width] * (tc.cell_colspan || 1)
end.flatten

column_widths.each do |width|
xml['w'].gridCol({ 'w:w' => width })
end

xml['w'].tblGridChange({ 'w:id' => '0' }) do
xml['w'].tblGrid do
model.rows.first.each do |tc|
(tc.cell_colspan || 1).times do
xml['w'].gridCol({ 'w:w' => tc.cell_width })
end
column_widths.each do |width|
xml['w'].gridCol({ 'w:w' => width })
end
end
end
Expand Down