WiceGrid 3.6.0.pre4 examples

Callback to process records of the current page

In addition to current_page_records and current_page_records helpers a callback can be setup in the controller which is executed when a grid page is rendered. Though the results of that processing is rendered on page, normally such callbacks are used for some kind of background processing. Please notice that the callback is executed after the controller action and thus all controller instance variables have already been injected inti the view, that is why the current example has a definition of @one_page_records before the callback, so that the object is injected into the view and the callback block has access to the object as well. The callback can be setup separately as a block:

@grid.with_paginated_resultset do |records|
  ...
end
or as an argument of initialize_grid where the value is the name of the method to be invoked:
@grid = initialize_grid(Task,
  with_paginated_resultset: :process_records
)

  • # encoding: utf-8
    class ResultsetProcessingsController < ApplicationController
      def index
        @tasks_grid = initialize_grid(Task,
          include: [:priority, :status, :project, :assigned_users],
          order: 'statuses.name',
          name: 'g',
          per_page: 10,
          # :with_paginated_resultset => :process_records,
          custom_order: {
            'tasks.priority_id' => 'priorities.name',
            'tasks.status_id' => 'statuses.position',
            'tasks.project_id' => 'projects.name'
          }
    
        )
    
        @one_page_records = []
    
        @tasks_grid.with_paginated_resultset do |records|
          records.each { |rec| @one_page_records << rec }
        end
      end
    
      protected
    
      # def process_records(records)
      #   records.each{|rec| @one_page_records << rec}
      # end
    end
    
  • <%= grid(@tasks_grid, hide_submit_button: true) do |g|
    
      g.column name:  'ID', attribute: 'id', filter: false
    
      g.column name:  'Title', attribute: 'title', auto_reload:  true
    
      g.column name:  'Priority', attribute: 'priority_id', custom_filter: Priority.to_dropdown, auto_reload:  true do |task|
        task.priority.name if task.priority
      end
    
      g.column name:  'Status', attribute: 'status_id', custom_filter: Status.to_dropdown, auto_reload:  true  do |task|
        task.status.name if task.status
      end
    
      g.column name:  'Project Name', attribute: 'project_id', custom_filter: Project.to_dropdown, auto_reload:  true do |task|
        task.project.name if task.project
      end
    
      g.column  name:  'Archived', attribute: 'archived', auto_reload:  true do |task|
        task.archived? ? 'Yes' : 'No'
      end
    
      g.column name:  'Added', attribute: 'created_at', auto_reload:  true do |task|
        task.created_at.to_s(:short)
      end
    
    end -%>
    
  • .well
      %h2= current_page_title
      %p
        In addition to
        %a{href:  integration_with_application_view_index_path}
          %code current_page_records
        and
        %a{href: integration_with_application_view_index_path}
          %code current_page_records
        helpers
        a callback can be setup in the controller which is executed when a grid page is rendered.
        Though the results of that processing is rendered on page, normally such callbacks are used
        for some kind of background processing. Please notice that the callback is executed
        %em after
        the controller action and thus all controller instance variables have already been injected inti the view,
        that is why the current example has a definition of
        %code @one_page_records
        %em before
        the callback, so that the object is injected into the view and the callback block has access to the object as well.
    
        The callback can be setup separately as a block:
    
        :plain
          <pre>
          @grid.with_paginated_resultset do |records|
            ...
          end
          </pre>
    
        or as an argument of
        %code initialize_grid
        where the value is the name of the method to be invoked:
    
        :plain
          <pre>
          @grid = initialize_grid(Task,
            with_paginated_resultset: :process_records
          )
          </pre>
    
    = show_code
    
    = render   'grid'
    
    .row-fluid
      .col-md-12
        .well
          = @one_page_records.size
          records on the current page:
          = @one_page_records.map(&:id).to_sentence
    
IDTitlePriorityStatusProject NameArchivedAdded

41-50 / 500 show all
324natus dicta possimusAnecdoticAssignedDivine FirmwareNo16 May 12:37
334sitAssignedDivine FirmwareNo03 Apr 12:37
338impeditAssignedDivine FirmwareYes27 Apr 12:37
350et quoAnecdoticAssignedDivine FirmwareYes06 Apr 12:37
357optio provident quiAnecdoticAssignedSuper GameNo21 Apr 12:37
365eligendi delenitiAnecdoticAssignedSuper GameNo16 Mar 12:37
368magnamAssignedSuper GameNo15 May 12:37
377quam qui quibusdamAssignedDivine FirmwareNo24 Mar 12:37
386eveniet ipsam etUrgentAssignedUltimate WebsiteNo28 Mar 12:37
397tempore ut aliquidHighAssignedSuper GameYes05 Jun 12:37
10 records on the current page: 324, 334, 338, 350, 357, 365, 368, 377, 386, and 397

Fork me on GitHub