Class: BootstrapTableHelper::Table

Inherits:
Object
  • Object
show all
Defined in:
app/helpers/bootstrap_table_helper.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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, options={}, row_options={})
  row_options, options, headers = options, headers, nil if headers.is_a?(Hash)

  default_options = {show_seq: true, fixed: true}

  self.template = template
  self.headers = headers || []
  self.options = options.reverse_merge!(default_options)
  self.row_options = row_options
  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

#columnsObject

Returns the value of attribute columns.



42
43
44
# File 'app/helpers/bootstrap_table_helper.rb', line 42

def columns
  @columns
end

#headersObject

Returns the value of attribute headers.



42
43
44
# File 'app/helpers/bootstrap_table_helper.rb', line 42

def headers
  @headers
end

#optionsObject

Returns the value of attribute options.



42
43
44
# File 'app/helpers/bootstrap_table_helper.rb', line 42

def options
  @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
  @row_options
end

#rowsObject

Returns the value of attribute rows.



42
43
44
# File 'app/helpers/bootstrap_table_helper.rb', line 42

def rows
  @rows
end

#templateObject

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(options={}, &block)
  rows << Row.new(template, options, &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, options={}, &block)
  width, method, options = nil, nil, width if width.is_a?(Hash)
  method, options = nil, method if method.is_a?(Hash)

  header_options = options.delete(:h) || {}
  header_options.reverse_merge!(w: width) if width && width.to_i < 12 && width.to_i > 0
  if method
    options.reverse_merge!(method: method)
    align = case options[:align]
      when 'center'
        :t
      when 'right'
        :r
      else
        :l
    end

    header_options.reverse_merge!(align => self.options[:obj].first.class.human_attribute_name(method)) unless self.options[:obj].empty?
  end

  headers << header_options unless header_options.empty?


  columns << [options, 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(options={})
  self.headers << options
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_sObject



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 = options[:id] || "tbl-#{Random.rand(100000000)}"

  table_class = ' table table-hover'
  table_class << ' table-striped' if options.delete(:striped)
  table_class << ' table-fluid' unless options.delete(:fixed)
  table_class << ' table-bordered' if options.delete(:bordered)
  table_class << ' table-condensed' if options.delete(:condensed)

  options[:class] ||= ''
  options[:class] << table_class

  table_caption = options.delete(:caption)
  action_array = options.delete(:actions)

  show_seq = options.delete(:show_seq)
  check = options.delete(:check)
  radio = options.delete(:radio)

  obj = options.delete(:obj)
  cust_row = options.delete(:cust_row)

  no_page = options.delete(:no_page)
  pagi_param_name = options.delete(:pagi_param_name)
  pagi_params = options.delete(:pagi_params)
  pagi_method = options.delete(:pagi_method) || 'get'
  pagi_form = options.delete(:pagi_form)

  options[:style] ||= ''
  options[:style] << ' margin-top: 25px; ' if actions || action_array && !action_array.empty?

  template.(:table, nil, options) do
    content = ''
    if table_caption || actions || action_array
      content << template.('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.('div', (table_caption||'').html_safe, style: 'font-weight: bold; font-size: large'),
           template.('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.('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.('thead') { render_table_header(headers, headers_count) }
      end
    end

    content << template.(:tbody) do
      tbody_content = ''
      # render table content
      if obj && !cust_row && !obj.empty?
        obj_index = 0
        obj.each do |o|
          tbody_content << template.(:tr, nil, row_options) do
            tr_content = []
            if check
              tr_content << template.(: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.(:td, style: 'text-align: center') do
                template.radio_button_tag(table_id, o.id, false, style: 'margin-top: 0')
              end
            elsif show_seq
              if no_page
                tr_content << template.(:td, obj_index+1, style: 'text-align: center')
              else
                tr_content << template.(:td, seq(obj, o, obj.current_page), style: 'text-align: center')
              end
            end

            columns.each do |column|
              column_options = column.first

              value = if method = column_options[:method]
                o.send(method) if o.respond_to?(method)
              end

              column_options[:style] ||= ''
              column_options[:style] << "text-align: #{column_options[:align]||'left'};"
              tr_content << template.(:td, nil, column_options.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