diff --git a/lib/caracal/core/models/table_model.rb b/lib/caracal/core/models/table_model.rb index 2f41a22e..5949a3c0 100644 --- a/lib/caracal/core/models/table_model.rb +++ b/lib/caracal/core/models/table_model.rb @@ -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) @@ -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) @@ -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 diff --git a/lib/caracal/renderers/document_renderer.rb b/lib/caracal/renderers/document_renderer.rb index 5b14b3fb..83771124 100644 --- a/lib/caracal/renderers/document_renderer.rb +++ b/lib/caracal/renderers/document_renderer.rb @@ -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