class Qwer::Helper::HelperMethod

Constants

DEFAULT_COL_NUM
MAX_LOOP_NUM

Public Class Methods

new(col_num, query, data_size, model) click to toggle source

查询项布局初始化 col_num : table的列数 data_size : 查询项数量

# File lib/qwer/layout_helper.rb, line 14
def initialize(col_num, query, data_size, model)
  @col_num = col_num
  @query = query
  @data_size = data_size
  @model = model
end

Public Instance Methods

form_render(data_params, form_params) click to toggle source
# File lib/qwer/layout_helper.rb, line 21
def form_render data_params, form_params
  html_str = ""
  # 适配单行是都能显示 先暂时定位3 以后做适配
  if data_params.size > DEFAULT_COL_NUM
    html_str << form_table_render(form_params, data_params)
  else
    data_params.reject { |key, value|
      if value.class.to_s.eql?('String')
        html_str << "<label class='control-label'>#{value}:</label><input class='form-control' type='search' name='#{key}' id='q_#{key}' value='#{@query.send(get_send_key key)}'>"
      else
        html_str << "<div><label class='control-label'>#{value[:name]}:</label>"
        if select? value
          html_str << add_selector(key, value)
        else
          html_str << "<input class='#{value[:css]} form-control' type='search' name='#{key}' id='q_#{key}' value='#{@query.send(get_send_key key)}'>"
        end
        html_str << "</div>"
      end

    }
    html_str << button_render(form_params)
  end
  html_str
end

Private Instance Methods

add_new_btn_tr(params) click to toggle source

添加一行新的tr(有必要的话)

# File lib/qwer/layout_helper.rb, line 157
def add_new_btn_tr params
  html_str = ""
  result = @data_size % @col_num
  if result.zero?
    html_str << "<tr>"
    (@col_num-1).times{
      html_str << "<td style='border:none;'>"
      html_str << "</td>"
    }
    html_str << "<td style='border:none;text-align: center;'>"
    html_str << button_render(params)
    html_str << "</td>"
    html_str << "</tr>"
  end
  html_str
end
add_selector(key, value) click to toggle source
# File lib/qwer/layout_helper.rb, line 48
def add_selector key, value
  html_str = "<select class='form-control' name='#{key}' id='#{get_input_id key}'>"
  if value[:data].present?
    html_str << "<option value=''>全部</option>" if value[:data].include? 'all'
    value[:data].each do |item|
      if @query.send(get_send_key key).present? && item.class.to_s.eql?('Hash') && (item[:k].to_s == @query.send(get_send_key key).to_s)
        html_str << "<option value='#{item[:k]}' selected>#{item[:v]}</option>"
      else
        html_str << "<option value='#{item[:k]}'>#{item[:v]}</option>" if item.class.to_s.eql? 'Hash'
      end
    end
  end
  html_str << "</select>"
end
add_td_or_blank_td(form_params, counter, total) click to toggle source

添加空td,或button td

# File lib/qwer/layout_helper.rb, line 101
def add_td_or_blank_td form_params, counter, total
  html_str = ""
  return html_str if has_any_td? total
  # 循环添加td,直至填满tr,在最后添加button
  while true
    break if tr_end?(counter + 1) || counter > MAX_LOOP_NUM
    html_str << "<td style='border: none;'></td>"
    counter += 1
  end
  html_str << "<td style='border:none;text-align:center;'>"
  html_str << button_render(form_params)
  html_str << "</td>"
end
button_render(form_params) click to toggle source
# File lib/qwer/layout_helper.rb, line 115
def button_render form_params
  new_url = "/#{@model}/new"
  html_str = "<a class='btn btn-success green' href='#{form_params[:new] ||= new_url }'>新增</a>" if !(form_params[:new] == false)
  html_str << "<button type='submit' class='btn btn-info' data-toggle='modal' data-target='#search_model'>查询</button>"
end
data?(value) click to toggle source
# File lib/qwer/layout_helper.rb, line 141
def data? value
  value[:type] == 'date'
end
data_time?(value) click to toggle source
# File lib/qwer/layout_helper.rb, line 145
def data_time? value
  value[:type] == 'data_time'
end
form_table_render(form_param, data_params) click to toggle source

得到查询项,table

# File lib/qwer/layout_helper.rb, line 64
def form_table_render form_param, data_params
  conter = 1
  total = 1
  html_str = "<table class='table text-left'>"
  data_params.reject { |key, value|
    html_str << "<tr>" if tr_start? conter
    html_str << "<td style='border: none'>"
    html_str << "<div class='form-group'>"
    if value.class.to_s.eql? 'String'
      html_str << "<label class='control-label'>#{value}:</label><input class='form-control' type='search' name='#{key}' id='#{get_input_id key}' value='#{@query.send(get_send_key key)}'></div>"
    else
      html_str << "<label class='control-label'>#{value[:name]}:</label>"
      if select? value
        html_str << add_selector(key, value)
      else
        input_value = @query.send(get_send_key key)
        input_value = input_value.nil? ? "" : DateTime.parse(input_value.to_s).strftime('%Y-%m-%d').to_s
        html_str << "<input class='#{value[:css]} form-control' type='search' name='#{key}' id='#{get_input_id key}' value='#{input_value}'>"
      end
    end
    html_str << "</div>"
    html_str << "</td>"
    if !tr_end? conter
      # 一行tr 还没有结束 需要填补td
      html_str << add_td_or_blank_td(form_param, conter, total)
    end
    html_str << "</tr>" if tr_end? conter
    conter = 0 if tr_end? conter
    conter += 1
    total += 1
  }
  # 查询项 正好占满整行,需要另起一个tr,添加 button
  html_str << add_new_btn_tr(form_param)
  html_str << "</table>"
end
get_input_id(key) click to toggle source
# File lib/qwer/layout_helper.rb, line 149
def get_input_id key
  k = key.to_s
  k['['] = "_"
  k[']'] = ""
  k
end
get_send_key(key) click to toggle source
# File lib/qwer/layout_helper.rb, line 174
def get_send_key key
  result = key.to_s.split('[').last
  result = result.split(']').first
end
has_any_td?(total) click to toggle source

有剩余td 没有添加

# File lib/qwer/layout_helper.rb, line 132
def has_any_td? total
  total < @data_size
end
select?(value) click to toggle source

是否是下拉栏

# File lib/qwer/layout_helper.rb, line 137
def select? value
  value[:type] == 'select'
end
tr_end?(counter) click to toggle source

单行tr 结束

# File lib/qwer/layout_helper.rb, line 127
def tr_end? counter
  counter == @col_num
end
tr_start?(counter) click to toggle source

单行tr 开始

# File lib/qwer/layout_helper.rb, line 122
def tr_start? counter
  counter == 1
end