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