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)
|
@columns = @table.table_columns.asc(:order)
|
||||||
@table_fields = @columns.collect{|tc| tc.title}
|
@table_fields = @columns.collect{|tc| tc.title}
|
||||||
if params[:q].present?
|
if params[:q].present?
|
||||||
@entries = Kaminari.paginate_array(search_data(@table)).page(params[:page]).per(10)
|
@entries = search_data(@table)
|
||||||
else
|
else
|
||||||
@entries = @table.table_entries.desc(:created_at).page(params[:page]).per(10)
|
@entries = @table.table_entries.criteria
|
||||||
end
|
end
|
||||||
|
@entries = @entries.sorting(params: params,table: @table).page(params[:page]).per(10)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -242,11 +243,12 @@ class Admin::UniversalTablesController < OrbitAdminController
|
||||||
column.each do |c|
|
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))
|
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
|
end
|
||||||
entries = []
|
entry_ids = []
|
||||||
columns.each do |column|
|
columns.each do |column|
|
||||||
entries << column.table_entry
|
entry_ids << column.table_entry_id
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
def table_params
|
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)
|
entries = Kaminari.paginate_array(entries).page(params["page_no"]).per(OrbitHelper.page_data_count) if(paginated)
|
||||||
else
|
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
|
||||||
end
|
end
|
||||||
entries
|
entries
|
||||||
|
|
|
@ -17,4 +17,18 @@ class ColumnEntry
|
||||||
def type
|
def type
|
||||||
self.table_column.type
|
self.table_column.type
|
||||||
end
|
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
|
end
|
|
@ -10,7 +10,8 @@ class TableColumn
|
||||||
field :is_link_to_show, type: Boolean, default: false
|
field :is_link_to_show, type: Boolean, default: false
|
||||||
field :order, type: Integer
|
field :order, type: Integer
|
||||||
field :make_categorizable, type: Boolean, default: false
|
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
|
belongs_to :u_table
|
||||||
|
|
||||||
has_many :column_entries, :dependent => :destroy
|
has_many :column_entries, :dependent => :destroy
|
||||||
|
|
|
@ -3,8 +3,54 @@ class TableEntry
|
||||||
include Mongoid::Timestamps
|
include Mongoid::Timestamps
|
||||||
include Slug
|
include Slug
|
||||||
|
|
||||||
|
attr_accessor :sort_value
|
||||||
|
field :sort_number, type: Integer, default: 0
|
||||||
|
|
||||||
has_many :column_entries, :dependent => :destroy
|
has_many :column_entries, :dependent => :destroy
|
||||||
belongs_to :u_table
|
belongs_to :u_table
|
||||||
|
|
||||||
accepts_nested_attributes_for :column_entries, :allow_destroy => true
|
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
|
end
|
|
@ -13,4 +13,8 @@ class UTable
|
||||||
|
|
||||||
FIELD_TYPES = ["text", "editor", "image", "date", "period"]
|
FIELD_TYPES = ["text", "editor", "image", "date", "period"]
|
||||||
DATE_FORMATS = ["yyyy/MM/dd hh:mm", "yyyy/MM/dd","yyyy/MM", "yyyy"]
|
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
|
end
|
|
@ -14,7 +14,7 @@
|
||||||
<div class="control-group">
|
<div class="control-group">
|
||||||
<label class="control-label muted" for="key_0">Key</label>
|
<label class="control-label muted" for="key_0">Key</label>
|
||||||
<div class="controls">
|
<div class="controls">
|
||||||
<%= f.number_field :key, :autocomplete => "off", :'data-type' => 'key' %>
|
<%= f.text_field :key, :autocomplete => "off", :'data-type' => 'key' %>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="control-group">
|
<div class="control-group">
|
||||||
|
@ -54,6 +54,17 @@
|
||||||
</label>
|
</label>
|
||||||
</div>
|
</div>
|
||||||
</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">
|
<div class="control-group">
|
||||||
<label class="control-label muted" for="">Type</label>
|
<label class="control-label muted" for="">Type</label>
|
||||||
<div class="controls">
|
<div class="controls">
|
||||||
|
|
|
@ -100,6 +100,13 @@
|
||||||
label.addClass("hide");
|
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() {
|
function key_on_blur() {
|
||||||
$('input[data-type=key]').on('blur',function() {
|
$('input[data-type=key]').on('blur',function() {
|
||||||
var index_this = $(this).parents('.attributes').index()
|
var index_this = $(this).parents('.attributes').index()
|
||||||
|
@ -126,7 +133,7 @@
|
||||||
var now_ele = ui_child.eq(i);
|
var now_ele = ui_child.eq(i);
|
||||||
now_ele.find('input[data-type=key]').val(i+1);
|
now_ele.find('input[data-type=key]').val(i+1);
|
||||||
}
|
}
|
||||||
updateOrder
|
updateOrder();
|
||||||
}
|
}
|
||||||
$('#attributes-area').ready(function(){
|
$('#attributes-area').ready(function(){
|
||||||
$("#attributes-area").sortable({
|
$("#attributes-area").sortable({
|
||||||
|
|
|
@ -11,3 +11,6 @@ en:
|
||||||
total_number_of_entries: "Total number of enteries found : %{total_number}"
|
total_number_of_entries: "Total number of enteries found : %{total_number}"
|
||||||
export_xls: Export XLSX
|
export_xls: Export XLSX
|
||||||
add_column: Add Column
|
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}"
|
total_number_of_entries: "搜尋結果數量: %{total_number}"
|
||||||
export_xls: 匯出XLSX
|
export_xls: 匯出XLSX
|
||||||
add_column: 新增欄位
|
add_column: 新增欄位
|
||||||
|
default_ordered_field: 預設排序欄位
|
||||||
|
asc: 升序
|
||||||
|
desc: 降序
|
Loading…
Reference in New Issue