leikind / wice_grid

A Rails grid plugin to create grids with sorting, pagination, and (automatically generated) filters
MIT License
536 stars 215 forks source link

Export to CSV doesn't download directly #326

Open zarlife opened 7 years ago

zarlife commented 7 years ago

Plugin version: 3.6.0.pre4 Rails: 4.2.5.1

I followed the instructions outlined here: https://github.com/leikind/wice_grid#csv-export

Even though the export to CSV icon is visible on my index page, it is only clickable after I refresh the page. Then, when clicking on the export to CSV icon, a new tab is opened with the exported CSV in HTML format. If I refresh this page or simply access the link directly, the CSV file will finally be downloaded directly to the computer.

I have spent hours on this problem and have still not found a solution! Any help at all is really appreciated!

My controller:

` def index @Logbook = Logbook.all

@logs_grid = initialize_grid(Logbook, name: 'logs', enable_export_to_csv: true,
  csv_file_name: 'logbook')

export_grid_if_requested('logs' => 'logs_grid')

end`

Just wanted to add that this is a great plugin and I appreciate the work put into it!

zarlife commented 7 years ago

Tested on wice_grid 3.6.0.pre3, the same occurs.

My partial has no extra HTML/ERB as well.

This is driving me crazy!!

zarlife commented 7 years ago

It seems to be a problem with Turbolinks. When I disable it by removing //= require turbolinks in assets/javascripts/application.js it works.

However, I can't work out a way to make it work with Turbolinks enabled. Adding data-turbolinks = "false" to the tag allows me to no longer refresh the page in order for the button to work. I tried adding the same to the Export button.

So now, when pressing the Export button in a page with tag disabled, the same HTML page with the CSV contents loads up again.

At the moment, the only solution seems to be disabling Turbolinks for the entire project.

Any suggestion is appreciated! Thanks

miqs1992 commented 7 years ago

Hi, I have similar issue and I'm also using wice_grid: 3.6.0.pre4 and Rails: 4.2.5.1 When I render my tables for the first time icon to filter, export to CSV just do literally nothing - no log in terminal or console. After I press name of a column to sort, they become active and I can do all the filters and export. I tried to remove //= require turbolinks, but it makes icons inactive for all time

controller:

def index
    prefix = params[:prefix] || ''
    items = Item.where("ItemCode LIKE :prefix", prefix: "#{prefix}%")
    @items_grid = initialize_grid(items,
                                  order: 'ItemCode',
                                  include: :item_group_code,
                                  name: 'g1',
                                  enable_export_to_csv: true,
                                  csv_file_name: 'items'
    )
    export_grid_if_requested
  end

_g1_grid.html.erb:

<%= grid(@items_grid, show_filters: :when_filtered) do |g|

  g.blank_slate  do
    'There are no records'
  end

  g.column name: 'Item Code', attribute: 'ItemCode', in_csv: false, auto_reload: true do |item|
    link_to(item.ItemCode, item)
  end

  g.column name: 'Item Code', in_html: false do |item|
    item.ItemCode
  end

  g.column name: 'Description', attribute: 'ItemName', auto_reload: true do |item|
    item.ItemName
  end

  g.column name: 'Group Code', assoc: :item_group_code, attribute: 'ItmsGrpNam', custom_filter: :auto do |item|
    item.item_group_code.ItmsGrpNam
  end

  g.column name: 'Dflt WH', attribute: 'DfltWH', custom_filter: :auto, ordering: false  do |item|
    item.DfltWH
  end

  g.column name: 'Price'  do |item|
    item.LastPurPrc
  end
end -%>

edit: fixed in https://github.com/leikind/wice_grid/issues/313

nathanvda commented 7 years ago

In wice_grid_processor.js.coffee, when pressing the export link it checks if turbolinks is available and will then visit the link. This is not wanted behaviour when actually we want to download the csv. So I changed this as follows:

exportToCsv : ->
  window.location = @linkForExport

(I now have a local copy of wice_grid_processor which i include instead). I will make a PR for this later.

kreintjes commented 7 years ago

I can confirm the problem and @nathanvda's fix (thanks!). However, I noticed there was also an deprecation warning in combination with Rails 5:

DEPRECATION WARNING: `render :text` is deprecated because it does not actually render a `text/plain` response. Switch to `render plain: 'plain text'` to render as `text/plain`, `render html: '<strong>HTML</strong>'` to render as `text/html`, or `render body: 'raw'` to match the deprecated behavior and render with the default Content-Type, which is `text/html`. (called from block in send_file_rails2 at lib/wice/wice_grid_controller.rb:182)

I fixed the deprecation by replacing send_file_rails2 with Rails' send_file method, which is available since Rails 3, and this also fixes the download problem: https://github.com/leikind/wice_grid/pull/342. I don't think we need @nathanvda's fix any longer, even though it does seem a bit strange to send file downloads through Turbolinks.

kreintjes commented 7 years ago

Whoops. Apparently @nathanvda's fix is needed after all. Probably messed up due to caching. So my PR now fixes two things: this issue and a deprecation warning. Apologies for the confusion!