class UiBibz::Ui::Ux::Tables::Table

Create a Table

This element is an extend of UiBibz::Ui::Core::Component.

Attributes

Options

You can add HTML attributes using the html_options. You can pass arguments in options attribute:

Signatures

UiBibz::Ui::Ux::Tables::Table.new(store: @store)

UiBibz::Ui::Ux::Tables::Table.new(store: @store) do |t|
  t.columns do |c|
    c.column :id, name: '#'
  end
  t.actions do |a|
    a.link '', url: url, glyph: ''
  end
end

Examples

UiBibz::Ui::Ux::Tables::Table.new(store: @users).render

UiBibz::Ui::Ux::Tables::Table.new(store: @users).tap do |t|
  t.columns do |c|
    c.column :id, { name: '#' }
    c.column :name_fr', { link: edit_user_path(:id), order: 2 }
    c.column :name_en'
    c.column :state_id, { name: 'state', format: lambda{ |records, record| "Test #{ record.id}"} }
  end
  t.actions do |a|
    a.link 'state', url: users_path(:id), glyph: 'eye'
    a.divider
    a.link 'momo', url: users_path(:id), glyph: 'home'
  end
end.render

Helper

table(options = {}, html_options = {})

table(options = {}, html_options = {}) do |t|
  t.columns do |cls|
    cls.column(name, options = {}, html_options = {})
    cls.column(options = {}, html_options = {}) do
      name
    end
  end
  t.actions do |acs|
    acs.link(content, options = {}, html_options = {})
    acs.link(options = {}, html_options = {}) do
      content
    end
  end
end

Public Class Methods

new(content = nil, options = nil, html_options = nil, &block) click to toggle source

See UiBibz::Ui::Core::Component.initialize

Calls superclass method UiBibz::Ui::Core::Component::new
# File lib/ui_bibz/ui/ux/tables/table.rb, line 99
def initialize(content = nil, options = nil, html_options = nil, &block)
  super
  @columns = Columns.new
  @actions = Actions.new store
end

Public Instance Methods

actions(&block) click to toggle source

Add table actions items

# File lib/ui_bibz/ui/ux/tables/table.rb, line 111
def actions(&block)
  @actions.tap(&block)
end
actions_list() click to toggle source

for test

# File lib/ui_bibz/ui/ux/tables/table.rb, line 116
def actions_list
  @actions
end
columns(&block) click to toggle source

Add table columns items

# File lib/ui_bibz/ui/ux/tables/table.rb, line 106
def columns(&block)
  @columns.tap(&block)
end
pre_render() click to toggle source

Render html tag

# File lib/ui_bibz/ui/ux/tables/table.rb, line 121
def pre_render
  options[:responsive] ? content_tag(:div, table_html, class: responsive) : table_html
end
store() click to toggle source

Store must be generated by table_search_pagination method

# File lib/ui_bibz/ui/ux/tables/table.rb, line 126
def store
  raise 'Store is nil!' if @options[:store].nil?
  raise 'Store can be created only with "table_search_pagination" method!' if @options[:store].try(:records).nil?

  @store ||= Store.new @options[:store]
end

Protected Instance Methods

action() click to toggle source
# File lib/ui_bibz/ui/ux/tables/table.rb, line 139
def action
  @action ||= Actionable.new store, @options, @actions
end
cols() click to toggle source
# File lib/ui_bibz/ui/ux/tables/table.rb, line 143
def cols
  @columns.list.empty? ? store.columns.list : @columns.list
end
sort() click to toggle source
# File lib/ui_bibz/ui/ux/tables/table.rb, line 135
def sort
  @sort ||= Sortable.new store, @options
end
table_html() click to toggle source
# File lib/ui_bibz/ui/ux/tables/table.rb, line 151
def table_html
  content_tag(:table, html_options) do
    ths = cols.collect do |col|
      content_tag(:th, sort.header(col), class: col.class) unless col.hidden?
    end

    ths = action.header ths
    concat Thead.new(content_tag(:tr, ths.join.html_safe), @options[:thead]).render

    trs = store.records.collect do |record|
      tds = cols.collect do |col|
        content_tag(:td, td_content(record, col), class: col.class) unless col.hidden?
      end

      tds = action.body record, tds
      content_tag(:tr, tds.join.html_safe)
    end

    concat content_tag :tbody, trs.join.html_safe
  end
end
td_content(record, col) click to toggle source

Maybe create a class for td_content

# File lib/ui_bibz/ui/ux/tables/table.rb, line 174
def td_content(record, col)
  begin
    content = col.count ? record.send(col.data_index).count : record.send(col.data_index)
  rescue StandardError
    content = nil
  end
  unless content.nil?
    content = content.strftime(col.date_format)                    unless col.date_format.nil?
    content = link_to content, action.inject_url(col.link, record) unless col.link.nil?
  end
  content = col.format.call(@store.records, record)              unless col.format.nil?
  content = As.new(col, record, content, @options).render unless col.as.nil?
  content
end
type() click to toggle source
# File lib/ui_bibz/ui/ux/tables/table.rb, line 147
def type
  "table-#{@options[:type]}" unless @options[:type].nil?
end

Private Instance Methods

bordered() click to toggle source
# File lib/ui_bibz/ui/ux/tables/table.rb, line 203
def bordered
  'table-bordered' unless @options[:bordered].nil?
end
component_html_classes() click to toggle source
# File lib/ui_bibz/ui/ux/tables/table.rb, line 191
def component_html_classes
  ['table', striped, bordered, hoverable, size]
end
hoverable() click to toggle source
# File lib/ui_bibz/ui/ux/tables/table.rb, line 207
def hoverable
  'table-hoverable' unless @options[:hoverable].nil?
end
responsive() click to toggle source
# File lib/ui_bibz/ui/ux/tables/table.rb, line 215
def responsive
  ['table-responsive', @options[:breakpoint]].compact.join('-') unless @options[:responsive].nil?
end
size() click to toggle source
# File lib/ui_bibz/ui/ux/tables/table.rb, line 211
def size
  "table-#{@options[:size]}" unless @options[:size].nil?
end
status() click to toggle source
# File lib/ui_bibz/ui/ux/tables/table.rb, line 195
def status
  "table-#{@options[:status]}" unless @options[:status].nil?
end
striped() click to toggle source
# File lib/ui_bibz/ui/ux/tables/table.rb, line 199
def striped
  'table-striped' unless @options[:striped].nil?
end