Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export data #13

Open
JNajera opened this issue Jun 10, 2014 · 1 comment
Open

Export data #13

JNajera opened this issue Jun 10, 2014 · 1 comment

Comments

@JNajera
Copy link

JNajera commented Jun 10, 2014

It will be awesome to have a way to export the filtered data.

@say8425
Copy link

say8425 commented Feb 15, 2016

I made the export feature with smartlisting. I'll show my code.

#alert.rb
class Alert < ActiveRecord::Base
  def self.to_csv
    CSV.generate do |csv|
      csv << column_names
      all.each do |result|
        csv << result.attributes.values_at(*column_names)
      end
    end
  end
end

First, you need to make to_csv method.
But don't worry. Rails already have generating CSV. So it is not hard.

#alert_controller.rb
def export
  alerts_scope = get_alerts_scope

  send_data alerts_scope.to_csv,
            filename: "#{params[:date_start]}-#{params[:date_end]}-#{params[:filter]}.csv",
            type: 'text/csv',
            disposition: 'attachment'
end

private

def get_alerts_scope
  alerts_scope = Alert.all
  alerts_scope = Alert.search(params[:filter]) if params[:filter].present?
  alerts_scope = alerts_scope.where(updated_at: DateTime.now.beginning_of_month..DateTime.now) if params[:date_start].blank? && params[:date_end].blank?
  alerts_scope = alerts_scope.where(updated_at: params[:date_start].to_date..params[:date_end].to_date) if params[:date_start].present? && params[:date_end].present?
  alerts_scope
end

Next make a action for exporting. Of course, if you want you don't have to that.
You can use index action. But in this time I'll show this way.

get_alerts_scope action will get params and generate filtered model.
We will make CSV file with it. So after generating. send data with this file.
You can set name dynamically too.

#routes.rb
get 'alert/export', to: 'alert#export', defaults: { format: 'csv'}

And make a routes. It'll use for csv, so set format csv

<%# alert/index.html.erb %>
<%= link_to content_tag(:span, '', class: 'glyphicon glyphicon-export'), '',
                  class: 'btn btn-brightDarkTwt', id: 'btn-export' %>

then make a tag. You can find this a tag have a blank href.
It'll filled dynamically. So I make it blank.

//alert/index.js.erb
generate_export_path();

function generate_export_path() {
  var date_start = $('#date_start').val();
  var date_end = $('#date_end').val();
  var filter = $('input#filter').val();
  var export_path = location.origin + '/alert/export?date_start=' + date_start + '&date_end=' + date_end;
  if (filter !== '') {export_path += '&filter=' + filter;}

  $('#btn-export').attr('href', export_path);
};

and then put generating href method in js.erb. Then it'll generating href for a tag every smartlisting updated.

If you more question, please comment. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants