updates for universal table. Added filtering, reordering, sorting and categorizable

This commit is contained in:
Harry Bomrah 2015-11-18 23:51:49 +08:00
parent 52c12560ef
commit 316d211b99
15 changed files with 269 additions and 47 deletions

File diff suppressed because one or more lines are too long

View File

@ -29,6 +29,16 @@
&:nth-child(even) {
background-color: #e8e8e8;
}
&:nth-child(odd) {
background-color: #fff;
}
.draggable {
cursor: move;
}
.draggable i{
cursor: move;
vertical-align: middle;
}
}
}
.attributes-checkbox {
@ -49,6 +59,13 @@
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
border-radius: 5px;
.select-holder{
margin-left: 20px;
display: inline-block;
span {
margin: 0 5px;
}
}
}
.main-forms fieldset .input-area:after {

View File

@ -6,7 +6,6 @@ class Admin::UniversalTablesController < OrbitAdminController
.order_by(sort)
.with_categories(filters("category"))
@tables = search_data(@tables,[:title])
end
def show
@ -15,7 +14,11 @@ class Admin::UniversalTablesController < OrbitAdminController
if !@table.nil?
@columns = @table.table_columns.asc(:order)
@table_fields = @columns.collect{|tc| tc.title}
@entries = @table.table_entries.desc(:created_at).page(params[:page_id]).per(10)
if params[:q].present?
@entries = Kaminari.paginate_array(search_data(@table)).page(params[:page]).per(10)
else
@entries = @table.table_entries.desc(:created_at).page(params[:page]).per(10)
end
end
end
@ -103,6 +106,24 @@ class Admin::UniversalTablesController < OrbitAdminController
private
def search_data(table)
keywords = params["q"]
keywords = keywords.strip.nil? ? keywords : keywords.strip
regexes = keywords.split(/\s+(?=(?:[^"]*"[^"]*")*[^"]*$)/)
regex = Regexp.union(regexes.map{|word| Regexp.new(".*"+word+".*", "i")})
column = table.table_columns.where(:type.in => ["text","editor"])
columns = []
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 = []
columns.each do |column|
entries << column.table_entry
end
entries = entries.sort{|k,v| v["created_at"] <=> k["created_at"]}
end
def table_params
params.require(:u_table).permit!
end
@ -110,4 +131,10 @@ class Admin::UniversalTablesController < OrbitAdminController
def table_entry_params
params.require(:table_entry).permit!
end
end
end

View File

@ -1,19 +1,50 @@
class UniversalTablesController < ApplicationController
include Admin::UniversalTablesHelper
def index
params = OrbitHelper.params
table = UTable.where(:category_id => OrbitHelper.page_categories.first).first rescue nil
page = Page.where(:page_id => params[:page_id]).first
if !table.nil?
reset = "hide"
table_heads = table.table_columns.where(:display_in_index => true).asc(:order).collect do |tc|
search = ""
sort = "hide"
sort_class = "sort"
sort = ""
form_field = "<input type='search' class='form-control' name='q' placeholder='Search keyword'>"
query_string = ""
query_string = "&column=#{params["column"]}&q=#{params["q"]}" if params["column"].present?
query_string = query_string + "&page_no=#{params["page_no"]}" if params["page_no"].present?
sort_url = "/#{I18n.locale.to_s}#{page.url}?sortcolumn=#{tc.key}&sort=asc#{query_string}"
title_class = ""
case tc.type
when "date","period","image"
when "date"
search = "hide"
when "period","image"
sort_class = "sort hide"
search = "hide"
sort_url = "#"
sort = "hide"
when "editor"
sort_url = "#"
sort = "hide"
sort_class = "sort hide"
when "text"
if tc.make_categorizable
select_values = tc.column_entries.distinct(:text).uniq
form_field = "<select class='form-control' name='q'>"
select_values.each do |sv|
form_field = form_field + "<option value='#{sv[I18n.locale.to_s]}'>#{sv[I18n.locale.to_s]}</option>"
end
form_field = form_field + "</select>"
end
end
title_class = title_class + "no-sort" if sort == "hide"
if params["sortcolumn"] == tc.key
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}"
end
title_class = title_class + "no-sort" if sort == "sort"
title_class = title_class + " no-search" if search == "hide"
{
@ -21,31 +52,79 @@ class UniversalTablesController < ApplicationController
"type" => tc.type,
"key" => tc.key,
"search" => search,
"form-field" => form_field,
"sort" => sort,
"sort-class" => sort_class,
"sort-url" => sort_url,
"title-class" => title_class
}
end
tablecolumns = table.table_columns.where(:display_in_index => true).asc(:order)
rows = []
if params["column"].present?
reset = ""
keywords = params["q"]
keywords = keywords.strip.nil? ? keywords : keywords.strip
regexes = keywords.split(/\s+(?=(?:[^"]*"[^"]*")*[^"]*$)/)
regex = Regexp.union(regexes.map{|word| Regexp.new(".*"+word+".*", "i")})
column = table.table_columns.where(:key => params["column"]).first
columns = column.column_entries.any_of(:"text.en" => regex).or(:"text.zh_tw" => regex).or(:"content.en" => regex).or(:"content.zh_tw" => regex)
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
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
entries = []
columns.each do |column|
entries << column.table_entry
end
entries = entries.sort{|k,v| v["created_at"] <=> k["created_at"]}
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)
else
entries = table.table_entries.desc(:created_at).page(params["page_no"]).per(OrbitHelper.page_data_count)
if params["sort"].present?
reset = ""
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"])
entries = []
columns.each do |column|
entries << column.table_entry
end
entries = Kaminari.paginate_array(entries).page(params["page_no"]).per(OrbitHelper.page_data_count)
else
entries = table.table_entries.desc(:created_at).page(params["page_no"]).per(OrbitHelper.page_data_count)
end
end
total_pages = entries.total_pages
entries.each do |te|
cols = []
sort_value = ""
tablecolumns.each do |column|
ce = te.column_entries.where(:table_column_id => column.id).first rescue nil
if !ce.nil?
@ -56,19 +135,20 @@ class UniversalTablesController < ApplicationController
when "editor"
text = ce.content
when "date"
text = ce.date.strftime("%Y-%m-%d %H:%M")
text = format_date(ce.date, column.date_format)
when "period"
text = (ce.period_from.strftime("%Y-%m-%d") rescue "") + " ~ " + (ce.period_to.strftime("%Y-%m-%d") rescue "")
text = "" if text.starts_with?("~")
text = format_date(ce.period_from, column.date_format) + " ~ " + format_date(ce.period_to, column.date_format)
text = "" if text.starts_with?(" ~")
when "image"
text = "<img src='#{ce.image.thumb.url}' class='image-preview' />"
end
if column.is_link_to_show
text = "<a href='#{OrbitHelper.url_to_show("-" + te.uid)}'>#{text}</a>"
end
cols << {
"text" => text
}
cols << {"text" => text}
else
cols << {"text" => ""}
end
end
rows << {
@ -79,9 +159,10 @@ class UniversalTablesController < ApplicationController
{
"head-columns" => table_heads,
"rows" => rows,
"total_pages" => entries.total_pages,
"total_pages" => total_pages,
"extras" => {
"table-name" => table.title,
"reset" => reset,
"url" => "/#{I18n.locale.to_s}#{page.url}"
}
}
@ -101,18 +182,19 @@ class UniversalTablesController < ApplicationController
when "editor"
text = ce.content
when "date"
text = ce.date.strftime("%Y-%m-%d %H:%M")
text = format_date(ce.date, ce.table_column.date_format)
when "period"
text = (ce.period_from.strftime("%Y-%m-%d") rescue "") + " ~ " + (ce.period_to.strftime("%Y-%m-%d") rescue "")
text = "" if text.starts_with?("~")
text = format_date(ce.period_from, ce.table_column.date_format) + " ~ " + format_date(ce.period_from, ce.table_column.date_format)
text = "" if text.starts_with?(" ~")
when "image"
text = "<img src='#{ce.image.thumb.url}' class='image-preview' />"
end
rows << {
"title" => ct.title,
"text" => text,
"order" => ct.order
}
} if text != ""
end
sorted = rows.sort{ |k,v| k["order"] <=> v["order"] }
{

View File

@ -0,0 +1,31 @@
module Admin::UniversalTablesHelper
def format_date(date, format, for_editing=false)
case format
when "yyyy/MM/dd hh:mm"
f = "%Y/%m/%d %H:%M"
when "yyyy/MM/dd"
f = "%Y/%m/%d"
when "yyyy/MM"
f = "%Y/%m"
when "yyyy"
f = "%Y"
f = "%Y/%m" if for_editing
end
d = date.strftime(f) rescue ""
return d
end
def render_unique_texts(f,column,i)
select_values = column.column_entries.distinct(:text).uniq
select = "<select id='#{column.key}_#{i}'>"
s = {"en" => "", "zh_tw" => ""}
select = select + "<option class='muted' value='#{s.to_json.html_safe}'>---------------Select---------------</option>"
select_values.each do |sv|
select = select + "<option value='#{sv.to_json.html_safe}'>#{sv[I18n.locale]}</option>"
end
select = select + "</select>"
"<div class='select-holder'> <span>Or</span> " + select + "</div>"
end
end

View File

@ -1,4 +0,0 @@
module UniversalTable
module ApplicationHelper
end
end

View File

@ -6,8 +6,10 @@ class TableColumn
field :title, localize: true
field :display_in_index, type: Boolean, default: true
field :type
field :date_format, default: "YYYY/MM//DD"
field :is_link_to_show, type: Boolean, default: false
field :order, type: Integer
field :make_categorizable, type: Boolean, default: false
belongs_to :u_table

View File

@ -12,4 +12,5 @@ class UTable
accepts_nested_attributes_for :table_columns, :allow_destroy => true
FIELD_TYPES = ["text", "editor", "image", "date", "period"]
DATE_FORMATS = ["yyyy/MM/dd hh:mm", "yyyy/MM/dd","yyyy/MM", "yyyy"]
end

View File

@ -2,12 +2,12 @@
<div class="attributes">
<% end %>
<div class="attributes-header clearfix">
<a class="btn btn-mini pull-right btn-danger delete" href="#"><i class="icon-trash"></i> Delete</a>
<a class="btn btn-mini pull-right btn-danger delete" href="#"><i class="icon-trash"></i> Delete</a>
<% if defined?(i) %>
<%= f.hidden_field :_destroy, :value => "false", :class => "attribute_field_to_delete" %>
<h4><%= column.title %></h4>
<h4 class="draggable"><i class="icons-list-2"></i> <%= column.title %></h4>
<% else %>
<h4>ColumnXX</h4>
<h4 class="draggable"><i class="icons-list-2"></i> ColumnXX</h4>
<% end %>
</div>
<div class="attributes-body">
@ -59,21 +59,30 @@
<div class="controls">
<% select_values = UTable::FIELD_TYPES.collect{|ft| [ft.capitalize,ft]} %>
<%= f.select :type, select_values, {}, {class: "type-selector"} %>
<label class="checkbox inline attributes-checkbox <%= !defined?(i) || column.type == "text" ? "" : "hide" %>">
<%= f.check_box :is_link_to_show %> Link to show
<span class="link_to_show <%= !defined?(i) || column.type == "text" ? "" : "hide" %>">
<label class="checkbox inline attributes-checkbox ">
<%= f.check_box :is_link_to_show %> Link to show
</label>
<label class="checkbox inline attributes-checkbox">
<%= f.check_box :make_categorizable %> Categorizable
</label>
</span>
<% select_values = UTable::DATE_FORMATS.collect{|ft| [ft.upcase,ft]} %>
<label class="checkbox date_format inline attributes-checkbox <%= column.type == "date" || column.type == "period" ? "" : "hide" %>">
Date Format <%= f.select :date_format, select_values%>
</label>
</div>
</div>
<% if defined?(i) %>
<% if column.order.nil? %>
<%= f.hidden_field :order, :value => i %>
<%= f.hidden_field :order, :value => i, :class => "order-hidden-field" %>
<% else %>
<%= f.hidden_field :order %>
<%= f.hidden_field :order, :class => "order-hidden-field" %>
<% end %>
<% else %>
<%= f.hidden_field :order, :value=> "XXX" %>
<%= f.hidden_field :order, :value=> "XXX", :class => "order-hidden-field" %>
<% end %>
</div>
<% if !defined?(i) %>
<div class="attributes">
</div>
<% end %>

View File

@ -3,7 +3,8 @@
<div class="controls">
<div>
<div class="default_picker input-append" style="">
<%= f.text_field :date, :value => (date_field.date.strftime("%Y/%m/%d %H:%M") rescue ""), :placeholder => "YYYY/MM/DD HH:MM", :data => {:format => "yyyy/MM/dd hh:mm"} %>
<% v = !date_field.new_record? ? format_date(date_field.date, column.date_format, true) : "" %>
<%= f.text_field :date, :value => v, :placeholder => column.date_format.upcase, :data => {:format => (column.date_format == "yyyy" ? "yyyy/MM" : column.date_format)} %>
<span class="add-on clearDate"><i class="icons-cross-3"></i></span>
<span class="add-on iconbtn"><i data-date-icon="icons-calendar" data-time-icon="icons-clock" class="icons-calendar"></i></span>
</div>

View File

@ -4,7 +4,8 @@
<div class="controls">
<div>
<div class="default_picker input-append" style="">
<%= f.text_field :period_from, :value => (period_field.period_from.strftime("%Y/%m/%d") rescue ""), :placeholder => "YYYY/MM/DD", :data => {:format => "yyyy/MM/dd"}, :class => "input-small" %>
<% v = !period_field.new_record? ? format_date(period_field.period_from, column.date_format, true) : "" %>
<%= f.text_field :period_from, :value => v, :placeholder => column.date_format.upcase, :data => {:format => (column.date_format == "yyyy" ? "yyyy/MM" : column.date_format)}, :class => "input-small" %>
<span class="add-on clearDate"><i class="icons-cross-3"></i></span>
<span class="add-on iconbtn"><i data-date-icon="icons-calendar" data-time-icon="icons-clock" class="icons-calendar"></i></span>
</div>
@ -16,7 +17,8 @@
<div class="controls">
<div>
<div class="default_picker input-append" style="">
<%= f.text_field :period_to,:value => (period_field.period_to.strftime("%Y/%m/%d") rescue ""), :placeholder => "YYYY/MM/DD", :data => {:format => "yyyy/MM/dd"}, :class => "input-small" %>
<% v = !period_field.new_record? ? format_date(period_field.period_to, column.date_format, true) : "" %>
<%= f.text_field :period_to,:value => v, :placeholder => column.date_format.upcase, :data => {:format => (column.date_format == "yyyy" ? "yyyy/MM" : column.date_format)}, :class => "input-small" %>
<span class="add-on clearDate"><i class="icons-cross-3"></i></span>
<span class="add-on iconbtn"><i data-date-icon="icons-calendar" data-time-icon="icons-clock" class="icons-calendar"></i></span>
</div>

View File

@ -1,6 +1,9 @@
<% content_for :page_specific_css do %>
<%= stylesheet_link_tag "universal_table/universal-table" %>
<% end %>
<% content_for :page_specific_javascript do %>
<%= javascript_include_tag "universal_table/jquery-ui.min" %>
<% end %>
<fieldset class="utable-heading-wrap">
<div class="utable-heading-header">
@ -68,10 +71,13 @@
if($(this).parent().find(".attribute_field_to_delete").length == 0){
$(this).parent().parent().slideUp(function(){
$(this).remove();
updateOrder();
});
}else{
if(confirm("Are you sure?")){
$(this).parent().parent().slideUp();
$(this).parent().parent().slideUp(function(){
updateOrder();
});
$(this).parent().find(".attribute_field_to_delete").val("true");
}
}
@ -80,15 +86,35 @@
$(document.body).on("change","select.type-selector",function(){
var el = $(this),
label = el.parent().find("label");
label = el.parent().find("span.link_to_show");
if(el.val() == "text"){
label.removeClass("hide");
}else{
label.addClass("hide");
label.find("input[type=checkbox]").prop("checked",false);
}
label = el.parent().find("label.date_format");
if(el.val() == "date" || el.val() == "period"){
label.removeClass("hide");
}else{
label.addClass("hide");
}
})
$("#attributes-area").sortable({
update: function( event, ui ) {
updateOrder();
}
});
var updateOrder = function(){
var attributes = $("#attributes-area").find(".attributes:visible");
attributes.each(function(i){
$(this).find("input.order-hidden-field").val(i);
})
}
</script>

View File

@ -8,7 +8,7 @@
<% id = "table_entry_column_entries_#{i}_text_translations_#{locale.to_s}" %>
<div class="tab-pane fade in <%= active %>" id="<%= id %>">
<%= f.fields_for :text_translations do |f| %>
<%= f.text_field locale, :value => text_field.text_translations[locale.to_s] %>
<%= f.text_field locale, :value => text_field.text_translations[locale.to_s], :for => locale.to_s %>
<% end %>
</div>
<% end %>
@ -21,6 +21,22 @@
<% end %>
</div>
</div>
<% if column.make_categorizable %>
<%= render_unique_texts(f,column,i).html_safe %>
<script type="text/javascript">
$("select#<%= column.key + "_" + i.to_s %>").on("change",function(){
var el = $(this),
locales = <%= @site_in_use_locales.to_json.html_safe %>,
values = JSON.parse(el.val()),
inputs = el.parent().parent().find("input[type=text]");
$.each(locales,function(i,locale){
var input = inputs.filter("input[for=" + locale + "]");
input.val(values[locale]);
})
})
</script>
<% end %>
</div>
<% if !text_field.new_record? %>
<%= f.hidden_field :id %>

View File

@ -1,11 +1,14 @@
<% content_for :page_specific_css do %>
<%= stylesheet_link_tag "universal_table/universal-table" %>
<% end %>
<form class="form-search">
<input type="text" class="input-large search-query" placeholder="Search keyword">
<form class="form-search" action="<%= admin_universal_table_path(@table) %>" method="get">
<input type="text" name="q" class="input-large search-query" placeholder="Search from text or editor columns">
<button type="submit" class="btn btn-primary">Search</button>
<% if params[:q].present? %>
<a href="<%= admin_universal_table_path(@table) %>" class="btn btn-info">Reset</a>
<% end %>
</form>
<div id="data-table" class="ut-table">
<table class="table main-list">
<thead>
@ -34,10 +37,10 @@
<% end %>
</div>
<% when "date" %>
<%= ce.date.strftime("%Y-%m-%d %H:%M") rescue "" %>
<%= format_date(ce.date, column.date_format) %>
<% when "period" %>
<% if !ce.period_from.nil? %>
<%= ce.period_from.strftime("%Y-%m-%d") rescue "" %> ~ <%= ce.period_to.strftime("%Y-%m-%d") rescue "" %>
<%= format_date(ce.period_from, column.date_format) %> ~ <%= format_date(ce.period_to, column.date_format) %>
<% end %>
<% end %>
<% if index == 0 %>
@ -49,6 +52,10 @@
</div>
<% end %>
</td>
<% else %>
<td>
&nbsp;
</td>
<% end %>
<% end %>
</tr>
@ -65,8 +72,6 @@
</div>
</div>
</script>

View File

@ -1,6 +1,6 @@
zh_tw:
universal_table:
universal_table: Universal Table
universal_table: 萬用表格
all_tables: All Tables
new_table: Create New Table
table_name: Table Name