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(width = nil, method = nil, options = {}, &block) ⇒ Object
options width - 1~12 method - object’s method, use send(method) to get value 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.
44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'app/helpers/bootstrap_table_helper.rb', line 44 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.
96 97 98 |
# File 'app/helpers/bootstrap_table_helper.rb', line 96 def actions @actions end |
#columns ⇒ Object
Returns the value of attribute columns.
42 43 44 |
# File 'app/helpers/bootstrap_table_helper.rb', line 42 def columns @columns end |
#headers ⇒ Object
Returns the value of attribute headers.
42 43 44 |
# File 'app/helpers/bootstrap_table_helper.rb', line 42 def headers @headers end |
#options ⇒ Object
Returns the value of attribute options.
42 43 44 |
# File 'app/helpers/bootstrap_table_helper.rb', line 42 def @options end |
#row_options(options = {}) ⇒ Object
Returns the value of attribute row_options.
42 43 44 |
# File 'app/helpers/bootstrap_table_helper.rb', line 42 def @row_options end |
#rows ⇒ Object
Returns the value of attribute rows.
42 43 44 |
# File 'app/helpers/bootstrap_table_helper.rb', line 42 def rows @rows end |
#template ⇒ Object
Returns the value of attribute template.
42 43 44 |
# File 'app/helpers/bootstrap_table_helper.rb', line 42 def template @template end |
Instance Method Details
#additional_row(options = {}, &block) ⇒ Object
91 92 93 |
# File 'app/helpers/bootstrap_table_helper.rb', line 91 def additional_row(={}, &block) rows << Row.new(template, , &block) end |
#column(width = nil, method = nil, options = {}, &block) ⇒ Object
options
width - 1~12
method - object's method, use send(method) to get value
h - options of header
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'app/helpers/bootstrap_table_helper.rb', line 61 def column(width=nil, method=nil, ={}, &block) width, method, = nil, nil, width if width.is_a?(Hash) method, = nil, method if method.is_a?(Hash) = .delete(:h) || {} .reverse_merge!(w: width) if width && width.to_i < 12 && width.to_i > 0 if method .reverse_merge!(method: method) align = case [:align] when 'center' :t when 'right' :r else :l end .reverse_merge!(align => self.[:obj].first.class.human_attribute_name(method)) unless self.[:obj].empty? end headers << unless .empty? 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
108 109 110 |
# File 'app/helpers/bootstrap_table_helper.rb', line 108 def header(={}) self.headers << end |
#seq(collection, obj, p_page, p_per_page = nil) ⇒ Object
计算序号 collection: 集合 obj: 集合中的元素 per_page: 每页显示记录数
276 277 278 279 280 281 282 283 284 285 286 287 288 289 |
# File 'app/helpers/bootstrap_table_helper.rb', line 276 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
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 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 |
# File 'app/helpers/bootstrap_table_helper.rb', line 112 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 = .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 || actions || action_array content << template.content_tag('caption') do = '' if actions = template.capture(&actions) elsif action_array && !action_array.empty? = template.bg { action_array.map { |a| template.btn(a) }.join.html_safe } end template.div do [template.content_tag('div', (||'').html_safe, style: 'font-weight: bold; font-size: large'), template.content_tag('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 value = if method = [:method] o.send(method) if o.respond_to?(method) end [:style] ||= '' [:style] << "text-align: #{[:align]||'left'};" tr_content << template.content_tag(:td, nil, .dup.delete_if { |k, v| k.to_s == 'align' }) do if column.second template.capture(o, obj_index, &column.second) else value end 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 |