-
Notifications
You must be signed in to change notification settings - Fork 138
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
Comments
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. #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.
#routes.rb
get 'alert/export', to: 'alert#export', defaults: { format: 'csv'} And make a routes. It'll use for csv, so set format <%# 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. //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 If you more question, please comment. Thanks. |
It will be awesome to have a way to export the filtered data.
The text was updated successfully, but these errors were encountered: