From 14038cf90cf9a3821c5633f52b145b9e92414502 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B1=E5=8D=9A=E4=BA=9E?= Date: Thu, 24 Feb 2022 17:31:45 +0800 Subject: [PATCH] add default sorting option --- .../admin/universal_tables_controller.rb | 12 ++- .../universal_tables_controller.rb | 102 +++++++++--------- app/models/column_entry.rb | 14 +++ app/models/table_column.rb | 3 +- app/models/table_entry.rb | 46 ++++++++ app/models/u_table.rb | 4 + .../admin/universal_tables/_column.html.erb | 75 +++++++------ .../universal_tables/_table_form.html.erb | 9 +- config/locales/en.yml | 5 +- config/locales/zh_tw.yml | 5 +- 10 files changed, 183 insertions(+), 92 deletions(-) diff --git a/app/controllers/admin/universal_tables_controller.rb b/app/controllers/admin/universal_tables_controller.rb index 9c10f7a..0dc6b52 100644 --- a/app/controllers/admin/universal_tables_controller.rb +++ b/app/controllers/admin/universal_tables_controller.rb @@ -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 diff --git a/app/controllers/universal_tables_controller.rb b/app/controllers/universal_tables_controller.rb index 292d332..94763ae 100644 --- a/app/controllers/universal_tables_controller.rb +++ b/app/controllers/universal_tables_controller.rb @@ -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 diff --git a/app/models/column_entry.rb b/app/models/column_entry.rb index 3ccd0a7..06de2bf 100644 --- a/app/models/column_entry.rb +++ b/app/models/column_entry.rb @@ -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 \ No newline at end of file diff --git a/app/models/table_column.rb b/app/models/table_column.rb index 6d017b7..8194f63 100644 --- a/app/models/table_column.rb +++ b/app/models/table_column.rb @@ -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 diff --git a/app/models/table_entry.rb b/app/models/table_entry.rb index 453e12a..b23a990 100644 --- a/app/models/table_entry.rb +++ b/app/models/table_entry.rb @@ -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 \ No newline at end of file diff --git a/app/models/u_table.rb b/app/models/u_table.rb index 51e10fc..6b97bc5 100644 --- a/app/models/u_table.rb +++ b/app/models/u_table.rb @@ -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 \ No newline at end of file diff --git a/app/views/admin/universal_tables/_column.html.erb b/app/views/admin/universal_tables/_column.html.erb index bdd1632..d4f424c 100644 --- a/app/views/admin/universal_tables/_column.html.erb +++ b/app/views/admin/universal_tables/_column.html.erb @@ -14,46 +14,57 @@
- <%= f.number_field :key, :autocomplete => "off", :'data-type' => 'key' %> + <%= f.text_field :key, :autocomplete => "off", :'data-type' => 'key' %>
-
-
-
- <% @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}") %> -
- <%= f.fields_for :title_translations do |f| %> - <%= f.text_field locale, :value => column.title_translations[locale] %> - <% end %> -
- <% end %> -
-
- <% @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 %> -
-
+
+
+
+ <% @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}") %> +
+ <%= f.fields_for :title_translations do |f| %> + <%= f.text_field locale, :value => column.title_translations[locale] %> + <% end %> +
+ <% end %> +
+
+ <% @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 %> +
+
-
- -
- - +
+ +
+ + +
+
+
+ +
+
+ <%= f.check_box :default_ordered_field, class: 'default_ordered_field' %>
-
+
+ <%= f.select :order_direction,['desc','asc'].map{|v| [t("universal_table.#{v}"),v]} %> +
+
+
diff --git a/app/views/admin/universal_tables/_table_form.html.erb b/app/views/admin/universal_tables/_table_form.html.erb index fec150a..b2a6c4e 100644 --- a/app/views/admin/universal_tables/_table_form.html.erb +++ b/app/views/admin/universal_tables/_table_form.html.erb @@ -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({ diff --git a/config/locales/en.yml b/config/locales/en.yml index 8edb783..9221e62 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -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 \ No newline at end of file + add_column: Add Column + default_ordered_field: Default Ordered Field + asc: asc + desc: desc \ No newline at end of file diff --git a/config/locales/zh_tw.yml b/config/locales/zh_tw.yml index eec90b0..5360677 100644 --- a/config/locales/zh_tw.yml +++ b/config/locales/zh_tw.yml @@ -10,4 +10,7 @@ zh_tw: import_from_excel: 自Excel檔匯入 total_number_of_entries: "搜尋結果數量: %{total_number}" export_xls: 匯出XLSX - add_column: 新增欄位 \ No newline at end of file + add_column: 新增欄位 + default_ordered_field: 預設排序欄位 + asc: 升序 + desc: 降序 \ No newline at end of file