Class: BootstrapTableHelper::Table
- Inherits:
-
Object
- Object
- BootstrapTableHelper::Table
- Defined in:
- app/helpers/bootstrap_table_helper.rb
Instance Attribute Summary collapse
-
#actions(&block) ⇒ Object
Pass a block of actions instead of actions options in table options.
-
#columns ⇒ Object
Returns the value of attribute columns.
-
#headers ⇒ Object
Returns the value of attribute headers.
-
#options ⇒ Object
Returns the value of attribute options.
-
#row_options(options = {}) ⇒ Object
Returns the value of attribute row_options.
-
#rows ⇒ Object
Returns the value of attribute rows.
-
#template ⇒ Object
Returns the value of attribute template.
Instance Method Summary collapse
- #additional_row(options = {}, &block) ⇒ Object
-
#column(options = {}, &block) ⇒ Object
options h - options of header.
-
#header(options = {}) ⇒ Object
Add header to table.
-
#initialize(template, headers = nil, options = {}, row_options = {}) ⇒ Table
constructor
A new instance of Table.
-
#seq(collection, obj, p_page, p_per_page = nil) ⇒ Object
计算序号 collection: 集合 obj: 集合中的元素 per_page: 每页显示记录数.
- #to_s ⇒ Object
Constructor Details
#initialize(template, headers = nil, options = {}, row_options = {}) ⇒ Table
Returns a new instance of Table.
41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'app/helpers/bootstrap_table_helper.rb', line 41 def initialize(template, headers=nil, ={}, ={}) , , headers = , headers, nil if headers.is_a?(Hash) = {show_seq: true, fixed: true} self.template = template self.headers = headers || [] self. = .reverse_merge!() self. = self.rows = [] self.columns = [] end |
Instance Attribute Details
#actions(&block) ⇒ Object
Pass a block of actions instead of actions options in table options.
72 73 74 |
# File 'app/helpers/bootstrap_table_helper.rb', line 72 def actions @actions end |
#columns ⇒ Object
Returns the value of attribute columns.
39 40 41 |
# File 'app/helpers/bootstrap_table_helper.rb', line 39 def columns @columns end |
#headers ⇒ Object
Returns the value of attribute headers.
39 40 41 |
# File 'app/helpers/bootstrap_table_helper.rb', line 39 def headers @headers end |
#options ⇒ Object
Returns the value of attribute options.
39 40 41 |
# File 'app/helpers/bootstrap_table_helper.rb', line 39 def @options end |
#row_options(options = {}) ⇒ Object
Returns the value of attribute row_options.
39 40 41 |
# File 'app/helpers/bootstrap_table_helper.rb', line 39 def @row_options end |
#rows ⇒ Object
Returns the value of attribute rows.
39 40 41 |
# File 'app/helpers/bootstrap_table_helper.rb', line 39 def rows @rows end |
#template ⇒ Object
Returns the value of attribute template.
39 40 41 |
# File 'app/helpers/bootstrap_table_helper.rb', line 39 def template @template end |
Instance Method Details
#additional_row(options = {}, &block) ⇒ Object
67 68 69 |
# File 'app/helpers/bootstrap_table_helper.rb', line 67 def additional_row(={}, &block) rows << Row.new(template, , &block) end |
#column(options = {}, &block) ⇒ Object
options
h - options of header
56 57 58 59 60 61 |
# File 'app/helpers/bootstrap_table_helper.rb', line 56 def column(={}, &block) = .delete(:h) headers << if columns << [, block] end |
#header(options = {}) ⇒ Object
Add header to table
options - options of header
w - width 1..12
t - text of header, align center
l - text of header, align left
r - text of header, align right
the other options that can be accepted by th
84 85 86 |
# File 'app/helpers/bootstrap_table_helper.rb', line 84 def header(={}) self.headers << end |
#seq(collection, obj, p_page, p_per_page = nil) ⇒ Object
计算序号collection: 集合obj: 集合中的元素per_page: 每页显示记录数
243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
# File 'app/helpers/bootstrap_table_helper.rb', line 243 def seq(collection, obj, p_page, p_per_page=nil) if p_per_page.nil? per_page = WillPaginate.per_page else per_page = p_per_page end page = p_page.to_i if page < 1 page = 1 end (page - 1) * per_page + collection.index(obj) + 1 end |
#to_s ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'app/helpers/bootstrap_table_helper.rb', line 88 def to_s table_id = [:id] || "tbl-#{Random.rand(100000000)}" table_class = ' table table-hover' table_class << ' table-striped' if .delete(:striped) table_class << ' table-fluid' unless .delete(:fixed) table_class << ' table-bordered' if .delete(:bordered) table_class << ' table-condensed' if .delete(:condensed) [:class] ||= '' [:class] << table_class table_caption = .delete(:caption) action_array = .delete(:actions) show_seq = .delete(:show_seq) check = .delete(:check) radio = .delete(:radio) obj = .delete(:obj) cust_row = .delete(:cust_row) no_page = .delete(:no_page) pagi_param_name = .delete(:pagi_param_name) pagi_params = .delete(:pagi_params) pagi_method = .delete(:pagi_method) || 'get' pagi_form = .delete(:pagi_form) [:style] ||= '' [:style] << ' margin-top: 25px; ' if actions || action_array && !action_array.empty? template.content_tag(:table, nil, ) do content = '' if table_caption || actions || action_array content << template.content_tag('caption') do caption_div = '' if actions caption_div = template.capture(&actions) elsif action_array && !action_array.empty? caption_div = template.bg { action_array.map { |a| template.btn(a) }.join.html_safe } end template.div do [template.content_tag('div', (table_caption||'').html_safe, style: 'font-weight: bold; font-size: large'), template.content_tag('div', caption_div.html_safe, class: 'pull-left', style: 'position: relative; top: -25px')].join.html_safe end end end # if header is null, then raise error headers_count = [] if headers.empty? && !show_seq raise 'Error! No Table Header! ' else # if 2 or more rows of header if headers.size > 0 && headers[0].is_a?(Array) first_header = headers.shift if check first_header.unshift({w: 1, t: template.check_box_tag(table_id, '1', false, style: 'margin-top: 0'), rowspan: headers.size + 1}) elsif show_seq first_header.unshift({w: 1, t: '#', rowspan: headers.size + 1}) end headers.unshift(first_header) content << template.content_tag('thead') do header_content = [] headers.each do |header| if headers.index(header) == 0 header_content << render_table_header(header, headers_count) else header_content << render_table_header(header) end end header_content.join.html_safe end elsif headers[0].is_a?(Hash) if check headers.unshift({w: 1, t: template.check_box_tag(table_id, '1', false, style: 'margin-top: 0')}) elsif show_seq headers.unshift({w: 1, t: '#'}) end content << template.content_tag('thead') { render_table_header(headers, headers_count) } end end content << template.content_tag(:tbody) do tbody_content = '' # render table content if obj && !cust_row && !obj.empty? obj_index = 0 obj.each do |o| tbody_content << template.content_tag(:tr, nil, ) do tr_content = [] if check tr_content << template.content_tag(:td, style: 'text-align: center') do template.check_box_tag(table_id, o.id, false, style: 'margin-top: 0') end elsif radio tr_content << template.content_tag(:td, style: 'text-align: center') do template.(table_id, o.id, false, style: 'margin-top: 0') end elsif show_seq if no_page tr_content << template.content_tag(:td, obj_index+1, style: 'text-align: center') else tr_content << template.content_tag(:td, seq(obj, o, obj.current_page), style: 'text-align: center') end end columns.each do |column| = column.first [:style] ||= '' [:style] << "text-align: #{[:align]||'left'};" tr_content << template.content_tag(:td, nil, .dup.delete_if { |k, v| k.to_s == 'align' }) do template.capture(o, obj_index, &column.second) end end tr_content.join.html_safe end obj_index += 1 end rows.each do |r| tbody_content << r.to_s end # 全自定义行 else rows.each do |r| tbody_content << r.to_s end end # render pagination if obj && !no_page && obj.respond_to?('total_pages') && obj.total_pages > 1 #根据headers_count 计算colspan colspan = headers_count.map { |c| c.to_i }.sum tbody_content << paginate_tr(colspan, obj, pagi_param_name, pagi_params, pagi_method, pagi_form) end tbody_content.html_safe end content.html_safe end end |