class AdminUI::FilterTable
Table rendering classes
Attributes
Public Class Methods
Source
# File app/lib/ui/table.rb, line 10 def self.empty(message = '', status: :SKIP, status_message: '') return FilterTable.new if message.empty? FilterTable.new( columns: [ Column.new('message') ], data: [ Row.new([message]) ], status: status, status_message: status_message ) end
Source
# File app/lib/ui/table.rb, line 25 def initialize(columns: [], data: [], filters: [], totals: false, description: '', status: :SKIP, status_message: '', pagination: { enabled: false }) @columns = columns @rows = data @filters = filters @filterable = false @totals = totals @description = description @pagination = pagination @columns.each do |col| @filterable = true if col.filterable end @status = status @status_message = status_message end
Public Instance Methods
Source
# File app/lib/ui/table.rb, line 73 def add_column(column) @columns.push(column) @filterable = true if column.filterable end
Source
# File app/lib/ui/table.rb, line 60 def add_filter(filter) @filters.push(filter) end
Source
# File app/lib/ui/table.rb, line 64 def add_row(row) @rows.push(row) i = get_column_index(:status) return if i.negative? statval = UC3::UC3Client.status_resolve(row.cols[i]) @status = UC3::UC3Client.status_compare(statval, @status) end
Source
# File app/lib/ui/table.rb, line 53 def get_column_index(colsym) @columns.each_with_index do |col, i| return i if col.sym.to_sym == colsym.to_sym end -1 end
Source
# File app/lib/ui/table.rb, line 111 def render s = %( <div class='table'> <table class='data sortable'> <caption> #{render_status} #{render_counts} #{render_description} #{render_filters} </caption> <thead> <tr class='header'> #{render_column_headers} </tr> #{render_column_filters} </thead> <tbody> ) @rows.each do |row| s += row.render(@columns) end s += %(</tbody>) if @totals s += %(<tfoot><tr class='totals'>) @columns.each_with_index do |col, i| s += %(<td class='#{col.cssclass}'>) s += i.zero? ? 'Total' : '' s += %(</td>) end s += %(</tr></tfoot>) end s += %(</table></div>) s end
Source
# File app/lib/ui/table.rb, line 86 def render_column_filters s = '' if filterable s += %(<tr class='filters'>) @columns.each_with_index do |col, i| s += %(<th class='#{col.cssclass}'>) s += %(<button class='filter' title='remove filters to make table sortable'>Clear</button>) if i.zero? if col.filterable s += %( <select data='#{col.cssclass}' class='filter'> <option value='ALLVALS'>All</option> </select> ) end s += %(</th>) end s += '</tr>' end s end
Source
# File app/lib/ui/table.rb, line 78 def render_column_headers s = '' @columns.each do |col| s += "<th class='#{col.cssclass}'>#{col.header}</th>" end s end
Source
# File app/lib/ui/table.rb, line 165 def render_counts counts = "#{@rows.length} Row(s)" nav = {} if pagination.fetch(:enabled, false) limit = pagination.fetch(:LIMIT, 0) offset = pagination.fetch(:OFFSET, 0) path = pagination.fetch(:path, '') urlparams = pagination.fetch(:urlparams, {}) counts += "; Limit: #{limit}; Offset: #{offset}" if offset.positive? offsetprev = [offset - limit, 0].max nav[:prev] = render_page_link('prev', path, urlparams, limit, offsetprev) nav[:first] = render_page_link('first', path, urlparams, limit, 0) if offsetprev.positive? end nav[:next] = render_page_link('next', path, urlparams, limit, offset + limit) if @rows.length == limit end return '' if @rows.empty? %( <div class='counts'> #{nav.fetch(:first, '')} #{nav.fetch(:prev, '')} <span>#{counts}<span> #{nav.fetch(:next, '')} </div> ) end
Source
# File app/lib/ui/table.rb, line 193 def render_description return '' if @description.empty? %(<div class='description'>#{Redcarpet::Markdown.new( Redcarpet::Render::HTML.new, fenced_code_blocks: true, tables: true ).render(@description)}</div>) end
Source
# File app/lib/ui/table.rb, line 146 def render_filters return '' if @filters.empty? s = '<div id="controls">' @filters.each do |filter| s += filter.render end s += '</div>' s end
Source
# File app/lib/ui/table.rb, line 157 def render_page_link(title, path, urlparams, limit, offset) params = urlparams params['limit'] = limit params['offset'] = offset query_string = params.map { |key, value| "#{key}=#{value}" }.join('&') %(<a href="#{path}?#{query_string}">#{title}</a>) end
Source
# File app/lib/ui/table.rb, line 107 def render_status %(<div class="#{@status}">#{@status}</div>) if @status end
Source
# File app/lib/ui/table.rb, line 41 def table_data d = [] @rows.each do |row| r = {} row.cols.each_with_index do |col, i| r[@columns[i].sym.to_sym] = col.is_a?(Hash) ? col.fetch(:value, '') : col end d.append(r) end d end
Source
# File app/lib/ui/table.rb, line 203 def to_csv CSV.generate do |csv| row = @columns.map(&:header) csv << row @rows.each do |row| data = [] row.cols.each_with_index do |col, _i| if col.is_a?(Hash) v = col.fetch(:value, '') v = v.to_s if v.is_a?(BigDecimal) else v = col.to_s end data << v end csv << data end end end