add default sorting option
This commit is contained in:
parent
22ce8d5f1a
commit
14038cf90c
|
@ -15,10 +15,11 @@ class Admin::UniversalTablesController < OrbitAdminController
|
|||
@columns = @table.table_columns.asc(:order)
|
||||
@table_fields = @columns.collect{|tc| tc.title}
|
||||
if params[:q].present?
|
||||
@entries = Kaminari.paginate_array(search_data(@table)).page(params[:page]).per(10)
|
||||
@entries = search_data(@table)
|
||||
else
|
||||
@entries = @table.table_entries.desc(:created_at).page(params[:page]).per(10)
|
||||
@entries = @table.table_entries.criteria
|
||||
end
|
||||
@entries = @entries.sorting(params: params,table: @table).page(params[:page]).per(10)
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -242,11 +243,12 @@ class Admin::UniversalTablesController < OrbitAdminController
|
|||
column.each do |c|
|
||||
columns = (columns | c.column_entries.any_of(:"text.en" => regex).or(:"text.zh_tw" => regex).or(:"content.en" => regex).or(:"content.zh_tw" => regex))
|
||||
end
|
||||
entries = []
|
||||
entry_ids = []
|
||||
columns.each do |column|
|
||||
entries << column.table_entry
|
||||
entry_ids << column.table_entry_id
|
||||
end
|
||||
entries = entries.sort{|k,v| v["created_at"] <=> k["created_at"]}
|
||||
entry_ids = entry_ids.uniq
|
||||
entries = TableEntry.where(:id.in=> entry_ids)
|
||||
end
|
||||
|
||||
def table_params
|
||||
|
|
|
@ -179,66 +179,66 @@ class UniversalTablesController < ApplicationController
|
|||
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
|
||||
if column.make_categorizable
|
||||
regex = Regexp.new(".*"+keywords+".*", "i")
|
||||
else
|
||||
regexes = keywords.split(/\s+(?=(?:[^"]*"[^"]*")*[^"]*$)/)
|
||||
regex = Regexp.union(regexes.map{|word| Regexp.new(".*"+word+".*", "i")})
|
||||
keywords = params["q"]
|
||||
keywords = keywords.strip.nil? ? keywords : keywords.strip
|
||||
column = table.table_columns.where(:key => params["column"]).first
|
||||
if column.make_categorizable
|
||||
regex = Regexp.new(".*"+keywords+".*", "i")
|
||||
else
|
||||
regexes = keywords.split(/\s+(?=(?:[^"]*"[^"]*")*[^"]*$)/)
|
||||
regex = Regexp.union(regexes.map{|word| Regexp.new(".*"+word+".*", "i")})
|
||||
end
|
||||
if params["sort"].present?
|
||||
column_to_sort = table.table_columns.where(:key => params["sortcolumn"]).first
|
||||
case column_to_sort.type
|
||||
when "text"
|
||||
field_name = :text
|
||||
when "editor"
|
||||
field_name = :content
|
||||
when "date"
|
||||
field_name = :date
|
||||
end
|
||||
if params["sort"].present?
|
||||
column_to_sort = table.table_columns.where(:key => params["sortcolumn"]).first
|
||||
case column_to_sort.type
|
||||
when "text"
|
||||
field_name = :text
|
||||
when "editor"
|
||||
field_name = :content
|
||||
when "date"
|
||||
field_name = :date
|
||||
end
|
||||
if params["column"] == params["sortcolumn"]
|
||||
columns = column.column_entries.any_of(:"text.en" => regex).or(:"text.zh_tw" => regex).or(:"content.en" => regex).or(:"content.zh_tw" => regex).order_by(field_name => params["sort"])
|
||||
else
|
||||
temp = column.column_entries.any_of(:"text.en" => regex).or(:"text.zh_tw" => regex).or(:"content.en" => regex).or(:"content.zh_tw" => regex)
|
||||
columns = []
|
||||
temp.each do |c|
|
||||
columns << c.table_entry.column_entries.where(:table_column_id => column_to_sort.id).first
|
||||
end
|
||||
sorted_columns = column_to_sort.column_entries.order_by(field_name => params["sort"]).to_a
|
||||
columns = sorted_columns & columns
|
||||
end
|
||||
if params["column"] == params["sortcolumn"]
|
||||
columns = column.column_entries.any_of(:"text.en" => regex).or(:"text.zh_tw" => regex).or(:"content.en" => regex).or(:"content.zh_tw" => regex).order_by(field_name => params["sort"])
|
||||
else
|
||||
columns = column.column_entries.any_of(:"text.en" => regex).or(:"text.zh_tw" => regex).or(:"content.en" => regex).or(:"content.zh_tw" => regex)
|
||||
temp = column.column_entries.any_of(:"text.en" => regex).or(:"text.zh_tw" => regex).or(:"content.en" => regex).or(:"content.zh_tw" => regex)
|
||||
columns = []
|
||||
temp.each do |c|
|
||||
columns << c.table_entry.column_entries.where(:table_column_id => column_to_sort.id).first
|
||||
end
|
||||
sorted_columns = column_to_sort.column_entries.order_by(field_name => params["sort"]).to_a
|
||||
columns = sorted_columns & columns
|
||||
end
|
||||
else
|
||||
columns = column.column_entries.any_of(:"text.en" => regex).or(:"text.zh_tw" => regex).or(:"content.en" => regex).or(:"content.zh_tw" => regex)
|
||||
end
|
||||
columns.each do |column|
|
||||
entries << column.table_entry
|
||||
end
|
||||
entries = entries.sort{|k,v| v["created_at"] <=> k["created_at"]} if !params["sort"].present?
|
||||
entries = Kaminari.paginate_array(entries).page(params["page_no"]).per(OrbitHelper.page_data_count) if(paginated)
|
||||
else
|
||||
if params["sort"].present?
|
||||
column_to_sort = table.table_columns.where(:key => params["sortcolumn"]).first
|
||||
case column_to_sort.type
|
||||
when "text"
|
||||
field_name = :text
|
||||
when "editor"
|
||||
field_name = :content
|
||||
when "date"
|
||||
field_name = :date
|
||||
end
|
||||
columns = column_to_sort.column_entries.order_by(field_name => params["sort"])
|
||||
columns.each do |column|
|
||||
entries << column.table_entry
|
||||
end
|
||||
entries = entries.sort{|k,v| v["created_at"] <=> k["created_at"]} if !params["sort"].present?
|
||||
|
||||
entries = Kaminari.paginate_array(entries).page(params["page_no"]).per(OrbitHelper.page_data_count) if(paginated)
|
||||
else
|
||||
if params["sort"].present?
|
||||
column_to_sort = table.table_columns.where(:key => params["sortcolumn"]).first
|
||||
case column_to_sort.type
|
||||
when "text"
|
||||
field_name = :text
|
||||
when "editor"
|
||||
field_name = :content
|
||||
when "date"
|
||||
field_name = :date
|
||||
end
|
||||
columns = column_to_sort.column_entries.order_by(field_name => params["sort"])
|
||||
columns.each do |column|
|
||||
entries << column.table_entry
|
||||
end
|
||||
|
||||
entries = Kaminari.paginate_array(entries).page(params["page_no"]).per(OrbitHelper.page_data_count) if(paginated)
|
||||
else
|
||||
entries = table.table_entries.desc(:created_at).page(params["page_no"]).per(OrbitHelper.page_data_count) if(paginated)
|
||||
end
|
||||
entries = table.table_entries.sorting(params: params,table: table).page(params["page_no"]).per(OrbitHelper.page_data_count) if(paginated)
|
||||
end
|
||||
entries
|
||||
end
|
||||
entries
|
||||
end
|
||||
|
||||
def show
|
||||
|
|
|
@ -17,4 +17,18 @@ class ColumnEntry
|
|||
def type
|
||||
self.table_column.type
|
||||
end
|
||||
def sort_value
|
||||
case self.type
|
||||
when "text"
|
||||
self.text
|
||||
when "editor"
|
||||
self.content
|
||||
when "image"
|
||||
self[:image]
|
||||
when "date"
|
||||
self.date
|
||||
when "period"
|
||||
[self.period_from,ce.period_to]
|
||||
end
|
||||
end
|
||||
end
|
|
@ -10,7 +10,8 @@ class TableColumn
|
|||
field :is_link_to_show, type: Boolean, default: false
|
||||
field :order, type: Integer
|
||||
field :make_categorizable, type: Boolean, default: false
|
||||
|
||||
field :default_ordered_field, type: Boolean, default: false
|
||||
field :order_direction,type: String,default: 'desc'
|
||||
belongs_to :u_table
|
||||
|
||||
has_many :column_entries, :dependent => :destroy
|
||||
|
|
|
@ -3,8 +3,54 @@ class TableEntry
|
|||
include Mongoid::Timestamps
|
||||
include Slug
|
||||
|
||||
attr_accessor :sort_value
|
||||
field :sort_number, type: Integer, default: 0
|
||||
|
||||
has_many :column_entries, :dependent => :destroy
|
||||
belongs_to :u_table
|
||||
|
||||
accepts_nested_attributes_for :column_entries, :allow_destroy => true
|
||||
|
||||
def self.u_table
|
||||
UTable.find(criteria.selector['u_table_id'])
|
||||
end
|
||||
|
||||
def self.sorting(params: nil,table: nil,field: nil,direction: nil)
|
||||
if table.nil?
|
||||
table = u_table
|
||||
end
|
||||
if field.nil? || direction.nil?
|
||||
if !params[:sortcolumn].blank?
|
||||
field = params[:sortcolumn]
|
||||
direction = params[:sort]
|
||||
else
|
||||
field = params[:sort].blank? ? nil : params[:sort]
|
||||
direction = params[:order].blank? ? 'desc' : params[:order]
|
||||
end
|
||||
if field.nil?
|
||||
field = table.default_ordered_field
|
||||
if field != 'created_at'
|
||||
columns = table.table_columns
|
||||
sort_column = columns.where(:key=>field).first || columns.where(:title=>field).first
|
||||
direction = sort_column.order_direction
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
field = field.to_s
|
||||
if field=='created_at' || field == 'sort_number'
|
||||
self.order_by({field => direction})
|
||||
else
|
||||
values = criteria.to_a
|
||||
field_id = table.table_columns.where(key: field).first || table.table_columns.where(title: field).first
|
||||
values.each do |v|
|
||||
v.sort_value = v.column_entries.where(:table_column_id=>field_id).first.sort_value
|
||||
end
|
||||
values = values.sort_by{|v| v.sort_value}
|
||||
if direction=='desc'
|
||||
values = values.reverse
|
||||
end
|
||||
Kaminari.paginate_array(values)
|
||||
end
|
||||
end
|
||||
end
|
|
@ -13,4 +13,8 @@ class UTable
|
|||
|
||||
FIELD_TYPES = ["text", "editor", "image", "date", "period"]
|
||||
DATE_FORMATS = ["yyyy/MM/dd hh:mm", "yyyy/MM/dd","yyyy/MM", "yyyy"]
|
||||
def default_ordered_field
|
||||
f = self.table_columns.where(default_ordered_field: true).first
|
||||
f ? (f.key||f.title) : 'created_at'
|
||||
end
|
||||
end
|
|
@ -14,46 +14,57 @@
|
|||
<div class="control-group">
|
||||
<label class="control-label muted" for="key_0">Key</label>
|
||||
<div class="controls">
|
||||
<%= f.number_field :key, :autocomplete => "off", :'data-type' => 'key' %>
|
||||
<%= f.text_field :key, :autocomplete => "off", :'data-type' => 'key' %>
|
||||
</div>
|
||||
</div>
|
||||
<div class="control-group">
|
||||
<label class="control-label muted" for="">Title</label>
|
||||
<div class="controls">
|
||||
<div class="input-append">
|
||||
<div class="tab-content">
|
||||
<% @site_in_use_locales.each do |locale| %>
|
||||
<% active = (locale == @site_in_use_locales.first ? "active in" : "") %>
|
||||
<% id = (defined?(i) ? "table_column_#{i}_title_translations_#{locale.to_s}" : "table_column_XXX_title_translations_#{locale.to_s}") %>
|
||||
<div class="tab-pane fade in <%= active %>" id="<%= id %>">
|
||||
<%= f.fields_for :title_translations do |f| %>
|
||||
<%= f.text_field locale, :value => column.title_translations[locale] %>
|
||||
<% end %>
|
||||
</div>
|
||||
<% end %>
|
||||
</div>
|
||||
<div class="btn-group" data-toggle="buttons-radio">
|
||||
<% @site_in_use_locales.each do |locale| %>
|
||||
<% active = (locale == @site_in_use_locales.first ? "active" : "") %>
|
||||
<% id = (defined?(i) ? "table_column_#{i}_title_translations_#{locale.to_s}" : "table_column_XXX_title_translations_#{locale.to_s}") %>
|
||||
<%= link_to t(locale).to_s,"##{id}",:class=>"btn #{active}",:data=>{:toggle=>"tab"}%>
|
||||
<% end %>
|
||||
</div>
|
||||
</div>
|
||||
<div class="controls">
|
||||
<div class="input-append">
|
||||
<div class="tab-content">
|
||||
<% @site_in_use_locales.each do |locale| %>
|
||||
<% active = (locale == @site_in_use_locales.first ? "active in" : "") %>
|
||||
<% id = (defined?(i) ? "table_column_#{i}_title_translations_#{locale.to_s}" : "table_column_XXX_title_translations_#{locale.to_s}") %>
|
||||
<div class="tab-pane fade in <%= active %>" id="<%= id %>">
|
||||
<%= f.fields_for :title_translations do |f| %>
|
||||
<%= f.text_field locale, :value => column.title_translations[locale] %>
|
||||
<% end %>
|
||||
</div>
|
||||
<% end %>
|
||||
</div>
|
||||
<div class="btn-group" data-toggle="buttons-radio">
|
||||
<% @site_in_use_locales.each do |locale| %>
|
||||
<% active = (locale == @site_in_use_locales.first ? "active" : "") %>
|
||||
<% id = (defined?(i) ? "table_column_#{i}_title_translations_#{locale.to_s}" : "table_column_XXX_title_translations_#{locale.to_s}") %>
|
||||
<%= link_to t(locale).to_s,"##{id}",:class=>"btn #{active}",:data=>{:toggle=>"tab"}%>
|
||||
<% end %>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="control-group">
|
||||
<label class="control-label muted" for="">Display in index</label>
|
||||
<div class="controls">
|
||||
<label class="radio inline">
|
||||
<%= f.radio_button :display_in_index, "true" %>Yes
|
||||
</label>
|
||||
<label class="radio inline">
|
||||
<%= f.radio_button :display_in_index, "false" %>No
|
||||
</label>
|
||||
<div class="control-group">
|
||||
<label class="control-label muted" for="">Display in index</label>
|
||||
<div class="controls">
|
||||
<label class="radio inline">
|
||||
<%= f.radio_button :display_in_index, "true" %>Yes
|
||||
</label>
|
||||
<label class="radio inline">
|
||||
<%= f.radio_button :display_in_index, "false" %>No
|
||||
</label>
|
||||
</div>
|
||||
</div>
|
||||
<div class="control-group">
|
||||
<label class="control-label muted" for=""><%= t('universal_table.default_ordered_field') %></label>
|
||||
<div class="controls">
|
||||
<div>
|
||||
<%= f.check_box :default_ordered_field, class: 'default_ordered_field' %>
|
||||
</div>
|
||||
</div>
|
||||
<div class="order_direction<%= ' hidden' if !f.object.default_ordered_field %>">
|
||||
<%= f.select :order_direction,['desc','asc'].map{|v| [t("universal_table.#{v}"),v]} %>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="control-group">
|
||||
<label class="control-label muted" for="">Type</label>
|
||||
<div class="controls">
|
||||
|
|
|
@ -100,6 +100,13 @@
|
|||
label.addClass("hide");
|
||||
}
|
||||
})
|
||||
$(document).on('change','.default_ordered_field',function(){
|
||||
$('.order_direction').addClass('hidden');
|
||||
if ($(this).prop('checked')){
|
||||
$('.default_ordered_field').not(this).prop('checked',false);
|
||||
$(this).parents('.controls').eq(0).find('.order_direction').removeClass('hidden');
|
||||
}
|
||||
});
|
||||
function key_on_blur() {
|
||||
$('input[data-type=key]').on('blur',function() {
|
||||
var index_this = $(this).parents('.attributes').index()
|
||||
|
@ -126,7 +133,7 @@
|
|||
var now_ele = ui_child.eq(i);
|
||||
now_ele.find('input[data-type=key]').val(i+1);
|
||||
}
|
||||
updateOrder
|
||||
updateOrder();
|
||||
}
|
||||
$('#attributes-area').ready(function(){
|
||||
$("#attributes-area").sortable({
|
||||
|
|
|
@ -10,4 +10,7 @@ en:
|
|||
import_from_excel: Import From Excel
|
||||
total_number_of_entries: "Total number of enteries found : %{total_number}"
|
||||
export_xls: Export XLSX
|
||||
add_column: Add Column
|
||||
add_column: Add Column
|
||||
default_ordered_field: Default Ordered Field
|
||||
asc: asc
|
||||
desc: desc
|
|
@ -10,4 +10,7 @@ zh_tw:
|
|||
import_from_excel: 自Excel檔匯入
|
||||
total_number_of_entries: "搜尋結果數量: %{total_number}"
|
||||
export_xls: 匯出XLSX
|
||||
add_column: 新增欄位
|
||||
add_column: 新增欄位
|
||||
default_ordered_field: 預設排序欄位
|
||||
asc: 升序
|
||||
desc: 降序
|
Loading…
Reference in New Issue