added total number of entries and export button for frontend

This commit is contained in:
Harry Bomrah 2016-09-21 20:25:41 +08:00
parent afb9be2d07
commit f72dec2e33
6 changed files with 159 additions and 64 deletions

View File

@ -61,66 +61,7 @@ class UniversalTablesController < ApplicationController
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
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"]} if !params["sort"].present?
entries = Kaminari.paginate_array(entries).page(params["page_no"]).per(OrbitHelper.page_data_count)
else
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
entries = get_entries(params, table, page)
total_pages = entries.total_pages
entries.each do |te|
cols = []
@ -156,18 +97,143 @@ class UniversalTablesController < ApplicationController
}
end
end
export_button = ""
if params["column"].present?
reset = ""
if !OrbitHelper.current_user.nil?
p = {}
params.each do |k,v|
p[k] = v if ["q","column","sort","sortcolumn"].include?(k)
end
export_button = "<a href='/xhr/universal_table/export.xlsx?cat=#{OrbitHelper.page_categories.first}&page_id=#{page.page_id}&#{p.to_param}'>Export</a>"
end
total_entries = t("universal_table.total_number_of_entries", :total_number => entries.total_count)
elsif params["sortcolumn"].present?
reset = ""
end
{
"head-columns" => table_heads,
"rows" => rows,
"total_pages" => total_pages,
"extras" => {
"total_entries" => total_entries,
"table-name" => table.title,
"export_button" => export_button,
"reset" => reset,
"url" => "/#{I18n.locale.to_s}#{page.url}"
}
}
end
def export_filtered
table = UTable.where(:category_id => params[:cat]).first rescue nil
page = Page.where(:page_id => params[:page_id]).first
if !table.nil?
@rows = []
@tablecolumns = table.table_columns.where(:display_in_index => true).asc(:order)
entries = get_entries(params, table, page, false)
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?
text = ""
case ce.type
when "text"
text = ce.text
when "editor"
text = ce.content
when "date"
text = format_date(ce.date, column.date_format)
when "period"
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 = "http://#{request.host_with_port}" + ce.image.thumb.url
end
cols << {"text" => text}
else
cols << {"text" => ""}
end
end
@rows << {
"columns" => cols
}
end
excel_name = table.title + ".xlsx"
excel_name = 'attachment; filename="' + excel_name + '"'
end
respond_to do |format|
format.xlsx {
response.headers['Content-Disposition'] = excel_name
}
end
end
def get_entries(params, table, page, paginated=true)
entries = []
if params["column"].present?
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
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
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
end
entries
end
def show
params = OrbitHelper.params
entry = TableEntry.where(:uid => params[:uid]).first rescue nil

View File

@ -13,8 +13,16 @@
<table class="table main-list">
<thead>
<tr class="sort-header">
<% @table_fields.each do |f| %>
<%= thead(f) %>
<% @table_fields.each do |field| %>
<%
sort = field.to_s.include?('.') ? field.to_s.split('.')[1] : field.to_s
active = params[:sort].eql? sort
order = active ? (["asc", "desc"]-[params[:order]]).first : "asc"
arrow = (order.eql? "desc") ? "<b class='icons-arrow-up-3'></b>" : "<b class='icons-arrow-down-4'></b>"
klass = field.eql?(:title) ? "span5" : "span2"
th_data = "<a href='?sort=#{sort}&order=#{order}'>#{field} #{active ? arrow : ""}</a>"
%>
<th class='<%= klass %> <%= active ? "active" : "" %>'><%= th_data.html_safe %></th>
<% end %>
</tr>
</thead>

View File

@ -0,0 +1,18 @@
# encoding: utf-8
wb = xlsx_package.workbook
wb.add_worksheet(name: "Table") do |sheet|
heading = sheet.styles.add_style(:b => true, :locked => true)
headings = @tablecolumns.collect{|tc| tc.title}
sheet.add_row headings, :style => heading
@rows.each do |r|
row = []
r["columns"].each do |col|
row << col["text"]
end
sheet.add_row row
end
end

View File

@ -7,4 +7,5 @@ en:
created_time: Created Time
total_no_of_entries: No of entries
export_structure: Download table structure
import_from_excel: Import From Excel
import_from_excel: Import From Excel
total_number_of_entries: "Total number of enteries found : %{total_number}"

View File

@ -7,4 +7,5 @@ zh_tw:
created_time: Created Time
total_no_of_entries: No of entries
export_structure: Download table Structure
import_from_excel: Import From Excel
import_from_excel: Import From Excel
total_number_of_entries: "Total number of enteries found : %{total_number}"

View File

@ -14,6 +14,7 @@ Rails.application.routes.draw do
get "export_structure"
end
end
get "/xhr/universal_table/export", to: 'universal_tables#export_filtered'
end
end