Method: BootstrapTableHelper::Table#to_s

Defined in:
app/helpers/bootstrap_table_helper.rb

#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