A grid can export itself in the form of a CSV file. The contents of the file corresponds to the selected subset of all records, that is, the CSV export follows the state of filters and sorting.
To enable CSV export for a grid please do the following:
enable_export_to_csv: true
to
initialize_grid
:csv_file_name
in
initialize_grid
csv_field_separator: 'your-separator'
to
initialize_grid
.
The default field separator is the comma, and you can change the default by editing the value of
Wice::Defaults::CSV_FIELD_SEPARATOR
in the config file
export_grid_if_requested
:
export_grid_if_requested('g1' => 'tasks_grid', 'g2' => 'projects_grid') do # usual render or redirect code executed if the request is not a CSV export request endThe block is executed if the call is not a request for a CSV file, so place your usual
redirect_to
,
render
inside the block, or just skip it if your action does not have an explicit render:
export_grid_if_requested('g1' => 'tasks_grid', 'g2' => 'projects_grid')The keys of the hash submitted as an argument to
export_grid_if_requested
are names of grids, and the values are names of partials where the grids are defined.
in_csv: false
to the column definition, and then duplicate the column definition without the markup in the block while adding
in_html: false
to the column definition. Any filter related parameters like
:attribute
,
:assoc
, or
:custom_filter
can be skipped in a CSV-only column.
wg-external-csv-export-button
to any clickable element on page
and set its attribute
data-grid-name
to the name of the grid. If you need to disable the default export icon in the grid, add
hide_csv_button: true
to the
grid
helper.
# encoding: utf-8 class CsvExportController < ApplicationController def index @tasks_grid = initialize_grid(Task, include: [:priority, :status, :project, :assigned_users], order: 'statuses.name', custom_order: { 'tasks.priority_id' => 'priorities.name', 'tasks.status_id' => 'statuses.position', 'tasks.project_id' => 'projects.name' }, name: 'g1', enable_export_to_csv: true, csv_field_separator: ';', csv_file_name: 'tasks' ) @projects_grid = initialize_grid(Project, name: 'g2', enable_export_to_csv: true, csv_file_name: 'projects' ) export_grid_if_requested('g1' => 'tasks_grid', 'g2' => 'projects_grid') do # usual render or redirect code executed if the request is not a CSV export request end end end
.well %h2= current_page_title %p A grid can export itself in the form of a CSV file. The contents of the file corresponds to the selected subset of all records, that is, the CSV export follows the state of filters and sorting. %p To enable CSV export for a grid please do the following: %ol %li Make sure that the grid helper is in its own partial and there are no other elements in that partial including whitespaces %li Enable CSV export by adding %code enable_export_to_csv: true to %code initialize_grid %li Set the name of the exported file using option %code :csv_file_name in %code initialize_grid %li Change the CSV field separator by adding %code csv_field_separator: 'your-separator' to %code initialize_grid \. The default field separator is the comma, and you can change the default by editing the value of %code Wice::Defaults::CSV_FIELD_SEPARATOR in the config file %li The action code should be finished by a call to %code export_grid_if_requested \: :plain <pre> export_grid_if_requested('g1' => 'tasks_grid', 'g2' => 'projects_grid') do # usual render or redirect code executed if the request is not a CSV export request end </pre> The block is executed if the call is not a request for a CSV file, so place your usual %code redirect_to , %code render inside the block, or just skip it if your action does not have an explicit render: :plain <pre> export_grid_if_requested('g1' => 'tasks_grid', 'g2' => 'projects_grid') </pre> The keys of the hash submitted as an argument to %code export_grid_if_requested are names of grids, and the values are names of partials where the grids are defined. %li If a column definition contains markup, first disable its export to CSV adding %code in_csv: false to the column definition, and then duplicate the column definition without the markup in the block while adding %code in_html: false to the column definition. Any filter related parameters like %code :attribute , %code :assoc , or %code :custom_filter can be skipped in a CSV-only column. %li If you need an external CSV export button , add class %code wg-external-csv-export-button to any clickable element on page and set its attribute %code data-grid-name to the name of the grid. If you need to disable the default export icon in the grid, add %code hide_csv_button: true to the %code grid helper. = show_code %p %button.btn.btn-primary.wg-external-csv-export-button{'data-grid-name' => 'g1'} Export To CSV .row-fluid .col-md-12 =render 'projects_grid' .row-fluid .col-md-12 =render 'tasks_grid'
<%= grid(@projects_grid, show_filters: :always, hide_csv_button: true) do |g|
g.column name: 'ID', attribute: 'id', filter: false
g.column name: 'Project Name', attribute: 'name'
g.column name: 'Customer company', assoc: :customer, attribute: 'name' do |task|
task.customer.name if task.customer
end
g.column name: 'Supplier company',
assoc: :supplier, attribute: 'name', table_alias: 'suppliers_projects' do |task|
task.supplier.name if task.supplier
end
end -%>
<%= grid(@tasks_grid, upper_pagination_panel: true) do |g|
g.column name: 'ID', attribute: 'id', filter: false
# html version
g.column name: 'Title', attribute: 'title', in_csv: false do |task|
link_to('Edit', edit_task_path(task.title))
end
# plain text version
g.column name: 'Title', in_html: false do |task|
task.title
end
g.column name: 'Priority', attribute: 'priority_id', custom_filter: Priority.to_dropdown do |task|
task.priority.name if task.priority
end
g.column name: 'Status', attribute: 'status_id', custom_filter: Status.to_dropdown do |task|
task.status.name if task.status
end
# html version
g.column name: 'Project Name', attribute: 'project_id', custom_filter: Project.to_dropdown, in_csv: false do |task|
link_to_if(task.project, task.project.name, project_path(task.project) )
end
# plain text version
g.column name: 'Project Name', in_html: false do |task|
task.project.name if task.project
end
g.column name: 'Archived', attribute: 'archived' do |task|
task.archived? ? 'Yes' : 'No'
end
g.column name: 'Added', attribute: 'created_at' do |task|
task.created_at.to_s(:short)
end
# is not included into csv
g.column in_csv: false do |task|
link_to('Edit', edit_task_path(task))
end
end -%>
ID | Project Name | Customer company | Supplier company | |
---|---|---|---|---|
1-3 / 3 | ||||
6 | Divine Firmware | SkyNet | Coders Unlimited | |
5 | Super Game | MNU | Shift-Command-4 Limited | |
4 | Ultimate Website | MNU | Coders Unlimited |
81-100 / 500 show all | |||||||
ID | Title | Priority | Status | Project Name | Archived | Added | |
---|---|---|---|---|---|---|---|
81-100 / 500 show all | |||||||
186 | Edit | Cancelled | Ultimate Website | No | 11 Apr 12:37 | Edit | |
190 | Edit | Cancelled | Ultimate Website | No | 30 Mar 12:37 | Edit | |
200 | Edit | Urgent | Cancelled | Super Game | No | 21 May 12:37 | Edit |
204 | Edit | High | Cancelled | Ultimate Website | No | 12 Apr 12:37 | Edit |
224 | Edit | Normal | Cancelled | Super Game | No | 19 Mar 12:37 | Edit |
229 | Edit | High | Cancelled | Divine Firmware | No | 08 Apr 12:37 | Edit |
255 | Edit | Cancelled | Ultimate Website | No | 24 Apr 12:37 | Edit | |
259 | Edit | Cancelled | Divine Firmware | No | 18 May 12:37 | Edit | |
267 | Edit | Low | Cancelled | Super Game | No | 14 Jun 12:37 | Edit |
280 | Edit | High | Cancelled | Ultimate Website | No | 13 May 12:37 | Edit |
286 | Edit | Low | Cancelled | Ultimate Website | No | 01 May 12:37 | Edit |
315 | Edit | Urgent | Cancelled | Super Game | No | 14 Jun 12:37 | Edit |
341 | Edit | Urgent | Cancelled | Divine Firmware | No | 17 May 12:37 | Edit |
342 | Edit | Cancelled | Divine Firmware | No | 26 Apr 12:37 | Edit | |
354 | Edit | Urgent | Cancelled | Super Game | No | 13 Apr 12:37 | Edit |
230 | Edit | High | Cancelled | Ultimate Website | Yes | 23 Apr 12:37 | Edit |
226 | Edit | Cancelled | Super Game | No | 08 May 12:37 | Edit | |
293 | Edit | Normal | Cancelled | Ultimate Website | Yes | 22 Apr 12:37 | Edit |
269 | Edit | High | Cancelled | Divine Firmware | No | 12 Mar 12:37 | Edit |
227 | Edit | High | Cancelled | Divine Firmware | No | 09 Jun 12:37 | Edit |