WiceGrid 3.6.0.pre4 examples

CSV export

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:

  1. Make sure that the grid helper is in its own partial and there are no other elements in that partial including whitespaces
  2. Enable CSV export by adding enable_export_to_csv: true to initialize_grid
  3. Set the name of the exported file using option :csv_file_name in initialize_grid
  4. Change the CSV field separator by adding 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
  5. The action code should be finished by a call to 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
    end
    The 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.
  6. If a column definition contains markup, first disable its export to CSV adding 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.
  7. If you need an external CSV export button , add class 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
    
  • <%= 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 -%>
  • .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 -%>
    

IDProject NameCustomer companySupplier company
1-3 / 3
5Super GameMNUShift-Command-4 Limited
4Ultimate WebsiteMNUCoders Unlimited
6Divine FirmwareSkyNetCoders Unlimited
461-480 / 500 Alle rijen tonen
IDTitlePriorityStatusProject NameArchivedAdded

461-480 / 500 Alle rijen tonen
206EditVerifiedUltimate WebsiteNo12 Mar 12:37Edit
250EditNormalVerifiedDivine FirmwareNo04 Apr 12:37Edit
258EditHighVerifiedSuper GameNo09 Jun 12:37Edit
261EditLowVerifiedDivine FirmwareNo05 Apr 12:37Edit
291EditLowVerifiedDivine FirmwareNo16 Jun 12:37Edit
292EditLowVerifiedUltimate WebsiteNo24 May 12:37Edit
294EditNormalVerifiedUltimate WebsiteNo08 Jun 12:37Edit
296EditVerifiedDivine FirmwareYes02 May 12:37Edit
305EditHighVerifiedSuper GameNo19 Apr 12:37Edit
306EditAnecdoticVerifiedDivine FirmwareNo13 May 12:37Edit
318EditVerifiedDivine FirmwareNo13 Mar 12:37Edit
323EditUrgentVerifiedDivine FirmwareNo15 Jun 12:37Edit
327EditUrgentVerifiedDivine FirmwareNo08 Apr 12:37Edit
328EditUrgentVerifiedUltimate WebsiteNo26 Apr 12:37Edit
332EditVerifiedSuper GameNo15 Apr 12:37Edit
335EditLowVerifiedDivine FirmwareNo22 Mar 12:37Edit
309EditLowVerifiedUltimate WebsiteNo25 May 12:37Edit
192EditVerifiedUltimate WebsiteNo19 Jun 12:37Edit
207EditLowVerifiedSuper GameNo13 Apr 12:37Edit
208EditAnecdoticVerifiedUltimate WebsiteNo17 Apr 12:37Edit

Fork me on GitHub