Last active
December 26, 2015 08:15
-
-
Save lingceng/840f97f17128d8a9fd3b to your computer and use it in GitHub Desktop.
Export csv or excel by re-using existing HTML view
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
table | |
thead | |
tr | |
th = sort_link(@q, :number) | |
th = sort_link(@q, :production_status) | |
th = sort_link(@q, :current_step) | |
th = order_name(:production_type) | |
th = order_name(:type) | |
th = order_name(:brand) | |
th = order_name(:color) | |
th = order_name(:style) | |
th style="width:200px" = Order.human_attribute_name(:diagnose) | |
th = sort_link(@q, :created_at) | |
th = sort_link(@q, :finish_date) | |
th 实际开工日期 | |
th 实际完工日期 | |
tbody | |
- @orders.each do |order| | |
tr | |
td = link_to order.number, production_order_path(order) | |
td = order.current_state_name | |
td = order.current_step_name | |
td = order.production_order_type | |
td = order.show_item_type | |
td = order.show_brand | |
td = order.show_color | |
td = order.show_style | |
td = order.try(:full_diagnose) || order.diagnose | |
td = short_date order.created_at.to_date | |
td = short_date order.finish_date | |
td = short_date order.actual_open_at | |
td = short_date order.actual_finish_at | |
= paginate @orders | |
span.pull-right.mt20 | |
= page_entries_info @orders | |
= download_link | |
= download_link(500) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module PlainHelper | |
def download_link(per=nil) | |
query = params.merge(per: per).to_query | |
name = per ? "#{per}条" : '本页' | |
link_to "导出#{name}", "#{request.path}.csv?#{query}", class: 'ml10' | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ProductionOrdersController < PlainController | |
def index | |
@q = Order.ransack(params[:q]) | |
@current_stop = params[:current_stop] || 'center' | |
@orders = @q.result.by_stop(@current_stop). | |
page(params[:page]).per(params[:per]) | |
respond_html_and_csv | |
end | |
def generate_csv_data(template = nil) | |
template ||= "#{controller_name}/#{action_name}.html.slim" | |
content = render_to_string(template) | |
doc = Nokogiri::HTML(content) | |
table = doc.at_css('table') | |
data = table.css('tr').map do |r| | |
r.css('td,th').map(&:text).to_csv | |
end.join | |
# Convert from utf8 to gbk to make it compatible with Windows Office Excel | |
# And Mac number can work with GBK too | |
data = data.encode('GBK', undef: :replace, replace: "") | |
end | |
def respond_html_and_csv | |
respond_to do |format| | |
format.html | |
format.csv { send_data(generate_csv_data) } | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment