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.



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, 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.



72
73
74
# File 'app/helpers/bootstrap_table_helper.rb', line 72

def actions
  @actions
end

#columnsObject

Returns the value of attribute columns.



39
40
41
# File 'app/helpers/bootstrap_table_helper.rb', line 39

def columns
  @columns
end

#headersObject

Returns the value of attribute headers.



39
40
41
# File 'app/helpers/bootstrap_table_helper.rb', line 39

def headers
  @headers
end

#optionsObject

Returns the value of attribute options.



39
40
41
# File 'app/helpers/bootstrap_table_helper.rb', line 39

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

#rowsObject

Returns the value of attribute rows.



39
40
41
# File 'app/helpers/bootstrap_table_helper.rb', line 39

def rows
  @rows
end

#templateObject

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(options={}, &block)
  rows << Row.new(template, options, &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(options={}, &block)
  header_options = options.delete(:h)
  headers << header_options if header_options

  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


84
85
86
# File 'app/helpers/bootstrap_table_helper.rb', line 84

def header(options={})
  self.headers << options
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_sObject



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 = 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
              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
                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