Class: DataTableColumn

Inherits:
Object
  • Object
show all
Defined in:
lib/data-table/data_table_column.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, description = "", opts = {}, &renderer) ⇒ DataTableColumn

Returns a new instance of DataTableColumn.



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/data-table/data_table_column.rb', line 6

def initialize(name, description="", opts={}, &renderer)
  @name, @description, = name, description
  @data_type = opts[:data_type] || :text
  @help_text = opts[:help_text] || ""
  @css_class = opts[:css_class]
  @attributes = opts[:attributes] || {}
  @width = opts[:width]
  @options = opts
  
  @display = true
  @index = 0
  @renderer = renderer
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



4
5
6
# File 'lib/data-table/data_table_column.rb', line 4

def attributes
  @attributes
end

#css_classObject

Returns the value of attribute css_class.



4
5
6
# File 'lib/data-table/data_table_column.rb', line 4

def css_class
  @css_class
end

#displayObject

Returns the value of attribute display.



4
5
6
# File 'lib/data-table/data_table_column.rb', line 4

def display
  @display
end

#indexObject

Returns the value of attribute index.



4
5
6
# File 'lib/data-table/data_table_column.rb', line 4

def index
  @index
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/data-table/data_table_column.rb', line 3

def name
  @name
end

#optionsObject

Returns the value of attribute options.



4
5
6
# File 'lib/data-table/data_table_column.rb', line 4

def options
  @options
end

Instance Method Details

#css_class_namesObject



52
53
54
55
56
57
58
# File 'lib/data-table/data_table_column.rb', line 52

def css_class_names
  class_names = []
  class_names << @name.to_s
  class_names << @data_type.to_s
  class_names << @css_class
  class_names.compact.join(' ')
end

#custom_attributesObject



48
49
50
# File 'lib/data-table/data_table_column.rb', line 48

def custom_attributes
  @attributes.map{|k, v| "#{k}='#{v}'"}.join ' '
end

#render_cell(cell_data, row = nil, row_index = 0, col_index = 0) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/data-table/data_table_column.rb', line 20

def render_cell(cell_data, row=nil, row_index=0, col_index=0)
  html = ""
  if @renderer && row
    cell_data = cell_data.to_s
    html << case @renderer.arity
          when 1; @renderer.call(cell_data).to_s
          when 2; @renderer.call(cell_data, row).to_s
          when 3; @renderer.call(cell_data, row, row_index).to_s
          when 4; @renderer.call(cell_data, row, row_index, self).to_s
          when 5; @renderer.call(cell_data, row, row_index, self, col_index).to_s
        end
  else
    html << cell_data.to_s
  end
  
  html << "</td>"
  # Doing this here b/c you can't change @css_class if this is done before the renderer is called
  html = "<td class='#{css_class_names}' #{custom_attributes}>" + html
end

#render_column_headerObject



40
41
42
43
44
45
46
# File 'lib/data-table/data_table_column.rb', line 40

def render_column_header
  header = "<th class='#{css_class_names}' #{custom_attributes}" 
  header << "title='#{@help_text}' " if @help_text
  header << "style='width: #{@width}'" if @width
  header << ">#{@description}</th>"
  header
end