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
|
||||
|
|
|
@ -235,7 +235,7 @@ class UniversalTablesController < ApplicationController
|
|||
|
||||
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)
|
||||
entries = table.table_entries.sorting(params: params,table: table).page(params["page_no"]).per(OrbitHelper.page_data_count) if(paginated)
|
||||
end
|
||||
end
|
||||
entries
|
||||
|
|
|
@ -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,7 +14,7 @@
|
|||
<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">
|
||||
|
@ -54,6 +54,17 @@
|
|||
</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 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({
|
||||
|
|
|
@ -11,3 +11,6 @@ en:
|
|||
total_number_of_entries: "Total number of enteries found : %{total_number}"
|
||||
export_xls: Export XLSX
|
||||
add_column: Add Column
|
||||
default_ordered_field: Default Ordered Field
|
||||
asc: asc
|
||||
desc: desc
|
|
@ -11,3 +11,6 @@ zh_tw:
|
|||
total_number_of_entries: "搜尋結果數量: %{total_number}"
|
||||
export_xls: 匯出XLSX
|
||||
add_column: 新增欄位
|
||||
default_ordered_field: 預設排序欄位
|
||||
asc: 升序
|
||||
desc: 降序
|
Loading…
Reference in New Issue