Change query field from single field to multiple field.
This commit is contained in:
parent
7a7ce6555d
commit
99d6d9a849
|
@ -7,18 +7,24 @@ class UniversalTablesController < ApplicationController
|
|||
if !table.nil?
|
||||
reset = "hide"
|
||||
csrf_value = (0...46).map { ('a'..'z').to_a[rand(26)] }.join
|
||||
params_column = params["column"].to_s.gsub("\"",'')
|
||||
params_q = params["q"].to_s.gsub("\"",'')
|
||||
params_q = params["q"].to_h.each{|_, v| v.gsub!("\"", '')} rescue {}
|
||||
params_no = params["page_no"].to_s.gsub("\"",'')
|
||||
if params_q.present?
|
||||
query_string = '&' + params_q.map{|k, v| ["q[#{k}]",v]}.to_h.to_query
|
||||
else
|
||||
query_string = ''
|
||||
end
|
||||
query_string += "&page_no=#{params_no}" if params_no.present?
|
||||
csrf_input = "<input type=\"hidden\" name=\"authenticity_token\" value=\"#{csrf_value}\">"
|
||||
table_heads = table.table_columns.where(:display_in_index => true).asc(:order).collect do |tc|
|
||||
field_key = tc.key
|
||||
field_value = params_q[field_key]
|
||||
field_value = nil if field_value.blank?
|
||||
search = ""
|
||||
sort_class = "sort"
|
||||
sort = ""
|
||||
form_field = "<input type=\"hidden\" name=\"authenticity_token\" value=\"#{csrf_value}\"><input type='search' class='form-control' name='q' placeholder='Search keyword'>"
|
||||
query_string = ""
|
||||
query_string = "&column=#{params_column}&q=#{params_q}" if params["column"].present?
|
||||
query_string = query_string + "&page_no=#{params_no}" if params["page_no"].present?
|
||||
sort_url = "/#{I18n.locale.to_s}#{page.url}?sortcolumn=#{tc.key}&sort=asc#{query_string}"
|
||||
form_field = "#{csrf_input}<input type=\"search\" class=\"form-control\" name=\"q[#{field_key}]\" value=\"#{field_value}\" placeholder=\"Search keyword\">"
|
||||
sort_url = "/#{I18n.locale.to_s}#{page.url}?sortcolumn=#{field_key}&sort=asc#{query_string}"
|
||||
title_class = ""
|
||||
case tc.type
|
||||
when "date"
|
||||
|
@ -35,26 +41,36 @@ class UniversalTablesController < ApplicationController
|
|||
when "text"
|
||||
if tc.make_categorizable
|
||||
select_values = tc.column_entries.distinct("text.#{I18n.locale.to_s}")
|
||||
form_field = "<input type=\"hidden\" name=\"authenticity_token\" value=\"#{csrf_value}\"><select class='form-control' name='q'>"
|
||||
form_field = "#{csrf_input}<select class=\"form-control\" name=\"q[#{field_key}]\">"
|
||||
select_values.each do |sv|
|
||||
form_field = form_field + "<option value='#{sv}'>#{sv}</option>"
|
||||
if field_value && sv == field_value
|
||||
selected = " selected"
|
||||
else
|
||||
selected = ""
|
||||
end
|
||||
form_field = form_field + "</select>"
|
||||
form_field += "<option value=\"#{sv}\"#{selected}>#{sv}</option>"
|
||||
end
|
||||
form_field += "</select>"
|
||||
end
|
||||
when "integer"
|
||||
if tc.make_categorizable
|
||||
select_values = tc.column_entries.distinct(:number)
|
||||
form_field = "<input type=\"hidden\" name=\"authenticity_token\" value=\"#{csrf_value}\"><select class='form-control' name='q'>"
|
||||
form_field = "#{csrf_input}<select class=\"form-control\" name=\"q[#{field_key}]\">"
|
||||
select_values.each do |sv|
|
||||
form_field = form_field + "<option value='#{sv}'>#{sv}</option>"
|
||||
if field_value && sv == field_value
|
||||
selected = " selected"
|
||||
else
|
||||
selected = ""
|
||||
end
|
||||
form_field = form_field + "</select>"
|
||||
form_field += "<option value=\"#{sv}\"#{selected}>#{sv}</option>"
|
||||
end
|
||||
form_field += "</select>"
|
||||
end
|
||||
end
|
||||
|
||||
if params["sortcolumn"] == tc.key
|
||||
if params["sortcolumn"] == field_key
|
||||
sort_class = params["sort"] == "asc" ? "sort-desc" : "sort-asc"
|
||||
sort_url = "/#{I18n.locale.to_s}#{page.url}?sortcolumn=#{tc.key}&sort=#{params["sort"] == "asc" ? "desc" : "asc"}#{query_string}"
|
||||
sort_url = "/#{I18n.locale.to_s}#{page.url}?sortcolumn=#{field_key}&sort=#{params["sort"] == "asc" ? "desc" : "asc"}#{query_string}"
|
||||
end
|
||||
|
||||
title_class = title_class + "no-sort" if sort == "sort"
|
||||
|
@ -63,7 +79,7 @@ class UniversalTablesController < ApplicationController
|
|||
{
|
||||
"title" => tc.title,
|
||||
"type" => tc.type,
|
||||
"key" => tc.key,
|
||||
"key" => field_key,
|
||||
"search" => search,
|
||||
"form-field" => form_field,
|
||||
"sort" => sort,
|
||||
|
@ -98,7 +114,7 @@ class UniversalTablesController < ApplicationController
|
|||
end
|
||||
end
|
||||
export_button = ""
|
||||
if params["column"].present?
|
||||
if get_query(params).present?
|
||||
reset = ""
|
||||
if !OrbitHelper.current_user.nil?
|
||||
p = {}
|
||||
|
@ -186,16 +202,31 @@ class UniversalTablesController < ApplicationController
|
|||
end
|
||||
end
|
||||
|
||||
def get_query(params)
|
||||
if params["column"].present?
|
||||
q = {params["column"] => params["q"]}
|
||||
else
|
||||
q = params["q"]
|
||||
end
|
||||
if q.present?
|
||||
q.map{|k,v| [k, v.to_s.strip] if v.present?}.compact.to_h
|
||||
else
|
||||
nil
|
||||
end
|
||||
end
|
||||
|
||||
def get_entries(params, table, page, paginated=true)
|
||||
entries = []
|
||||
if params["column"].present?
|
||||
keywords = params["q"]
|
||||
keywords = keywords.strip.nil? ? keywords : keywords.strip
|
||||
column = table.table_columns.where(:key => params["column"]).first
|
||||
q = get_query(params)
|
||||
if q.present?
|
||||
table_entry_ids = nil
|
||||
q.each do |k, v|
|
||||
column = table.table_columns.where(:key => k).first
|
||||
next if column.nil?
|
||||
if column.make_categorizable
|
||||
regex = Regexp.new(".*"+keywords+".*", "i")
|
||||
regex = Regexp.new(".*"+v+".*", "i")
|
||||
else
|
||||
regexes = keywords.split(/\s+(?=(?:[^"]*"[^"]*")*[^"]*$)/)
|
||||
regexes = v.split(/\s+(?=(?:[^"]*"[^"]*")*[^"]*$)/)
|
||||
regex = Regexp.union(regexes.map{|word| Regexp.new(".*"+word+".*", "i")})
|
||||
end
|
||||
if column.type == "text"
|
||||
|
@ -204,10 +235,21 @@ class UniversalTablesController < ApplicationController
|
|||
columns = column.column_entries.any_of([{"content.en" => regex}, {"content.zh_tw" => regex}])
|
||||
end
|
||||
# .or(:"content.en" => regex).or(:"content.zh_tw" => regex)
|
||||
if paginated
|
||||
entries = TableEntry.where(:u_table_id=>table.id).sorting(params: params,table: table,column_entries: columns,page_num: params["page_no"],per: OrbitHelper.page_data_count)
|
||||
tmp = columns.pluck(:table_entry_id)
|
||||
if table_entry_ids.nil?
|
||||
table_entry_ids = tmp
|
||||
else
|
||||
entries = TableEntry.where(:u_table_id=>table.id).sorting(params: params,table: table,column_entries: columns,paginated: false)
|
||||
table_entry_ids = table_entry_ids & tmp
|
||||
end
|
||||
end
|
||||
if table_entry_ids.nil?
|
||||
entries = TableEntry.where(:u_table_id=>table.id)
|
||||
else
|
||||
entries = TableEntry.where(:id.in=> table_entry_ids)
|
||||
end
|
||||
entries = TableEntry.sorted(entries: entries, params: params, table: table, paginated: paginated)
|
||||
if paginated
|
||||
entries = entries.page(params["page_no"]).per(OrbitHelper.page_data_count)
|
||||
end
|
||||
else
|
||||
if paginated
|
||||
|
|
|
@ -23,13 +23,13 @@ class TableEntry
|
|||
UTable.find(criteria.selector['u_table_id'])
|
||||
end
|
||||
|
||||
def self.sorting(params: nil,table: nil,field: nil,direction: nil,page_num: nil,per: nil,column_entries: nil,paginated: true)
|
||||
page_num = 1 if page_num.blank?
|
||||
page_num = page_num.to_i
|
||||
def self.get_sort_field(params: nil, table: nil)
|
||||
field = nil
|
||||
direction = nil
|
||||
if table.nil?
|
||||
table = u_table
|
||||
table = self.u_table
|
||||
end
|
||||
if field.nil? || direction.nil?
|
||||
if params
|
||||
if !params[:sortcolumn].blank?
|
||||
field = params[:sortcolumn]
|
||||
direction = params[:sort]
|
||||
|
@ -46,6 +46,39 @@ class TableEntry
|
|||
end
|
||||
end
|
||||
end
|
||||
[table, field, direction]
|
||||
end
|
||||
|
||||
def self.sorted(entries: nil, params: nil, table: nil, field: nil, direction: nil, paginated: true)
|
||||
if field.nil? || direction.nil?
|
||||
table, field, direction = self.get_sort_field(params: params, table: table)
|
||||
end
|
||||
if entries.nil?
|
||||
entries = table.table_entries
|
||||
end
|
||||
if (field=='created_at' || field == 'sort_number')
|
||||
values = entries.order_by({field => direction})
|
||||
else
|
||||
column_to_sort = field
|
||||
if entries.selector.present?
|
||||
column_entries = ColumnEntry.where(:table_column_id=>column_to_sort.id,:table_entry_id.in => entries.pluck(:id)).order_by(column_to_sort.sort_hash(direction))
|
||||
else
|
||||
column_entries = ColumnEntry.where(:table_column_id=>column_to_sort.id).order_by(column_to_sort.sort_hash(direction))
|
||||
end
|
||||
values = column_entries.map{|v| v.table_entry}
|
||||
if paginated
|
||||
values = Kaminari.paginate_array(values)
|
||||
end
|
||||
end
|
||||
values
|
||||
end
|
||||
|
||||
def self.sorting(params: nil,table: nil,field: nil,direction: nil,page_num: nil,per: nil,column_entries: nil,paginated: true)
|
||||
page_num = 1 if page_num.blank?
|
||||
page_num = page_num.to_i
|
||||
if field.nil? || direction.nil?
|
||||
table, field, direction = self.get_sort_field(params: params, table: table)
|
||||
end
|
||||
|
||||
if (field=='created_at' || field == 'sort_number')
|
||||
if column_entries.nil?
|
||||
|
|
|
@ -48,6 +48,7 @@
|
|||
width: 120px;
|
||||
}
|
||||
</style>
|
||||
<form class="form-inline universal-form-inline" action="{{url}}" method="get">
|
||||
<table class="table table-hover table-striped universal-table-index">
|
||||
<caption>
|
||||
<h3>{{table-name}}</h3>
|
||||
|
@ -64,13 +65,10 @@
|
|||
<span class="caret"></span>
|
||||
</button>
|
||||
<div class="dropdown-menu universal-dropdown-menu" aria-labelledby="dLabel">
|
||||
<form class="form-inline universal-form-inline" action="{{url}}" method="get">
|
||||
<div class="form-group">
|
||||
{{form-field}}
|
||||
<input type="hidden" value="{{key}}" name="column" >
|
||||
</div>
|
||||
<button class="btn btn-primary" type="submit" class="btn btn-default">Go</button>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</th>
|
||||
|
@ -82,6 +80,7 @@
|
|||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</form>
|
||||
<div>{{total_entries}}</div>
|
||||
<div>{{export_button}}</div>
|
||||
{{pagination_goes_here}}
|
||||
|
|
|
@ -66,6 +66,7 @@
|
|||
width: 120px;
|
||||
}
|
||||
</style>
|
||||
<form class="form-inline universal-form-inline" action="{{url}}" method="get">
|
||||
<table class="table table-hover table-striped universal-table-index">
|
||||
<caption>
|
||||
<h3>{{table-name}}</h3>
|
||||
|
@ -82,13 +83,10 @@
|
|||
<span class="caret"></span>
|
||||
</button>
|
||||
<div class="dropdown-menu universal-dropdown-menu" aria-labelledby="dLabel">
|
||||
<form class="form-inline universal-form-inline" action="{{url}}" method="get">
|
||||
<div class="form-group">
|
||||
{{form-field}}
|
||||
<input type="hidden" value="{{key}}" name="column" >
|
||||
</div>
|
||||
<button class="btn btn-primary" type="submit" class="btn btn-default">Go</button>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</th>
|
||||
|
@ -100,6 +98,7 @@
|
|||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</form>
|
||||
<div>{{total_entries}}</div>
|
||||
<div>{{export_button}}</div>
|
||||
{{pagination_goes_here}}
|
||||
|
|
Loading…
Reference in New Issue