Gem source code Demo source code
Author: Bogdan Gusiev

User Grid (basic) Time Entry Grid (advanced) Document Grid (Mongoid)

Users Grid

Grid:

class UsersGrid
  include Datagrid

  #
  # Scope
  #
  
  scope do
    User
  end

  #
  # Filters
  #
  
  filter(:id, :string, :multiple => ',')
  filter(:email, :string)
  filter(:disabled, :xboolean)
  filter(:registration_type, :enum, :select => User::REGISTRATION_TYPES.map {|r| [r.humanize, r]})
  filter(:logins_count, :integer, :range => true, :default => proc { [User.minimum(:logins_count), User.maximum(:logins_count)]})
  filter(:registered_at, :date, :range => true)
  filter(:condition, :dynamic, :header => "Dynamic condition")
  column_names_filter(:header => "Extra Columns", checkboxes: true)

  #
  # Columns
  #

  column(:id, :mandatory => true)
  column(:email, :mandatory => true) do |model|
    format(model.email) do |value|
      link_to value, "mailto:#{value}"
    end
  end
  column(:name, :mandatory => true)
  column(:disabled, :mandatory => true) do
    disabled? ? "Yes" : "No"
  end

  column(:registration_type) do |record|
    record.registration_type.humanize
  end
  column(:logins_count)
  column(:registered_at) do |record|
    record.registered_at.to_date
  end
  column(:age, :header => "Registration Age") do |record|
    age = (DateTime.now.in_time_zone - record.registered_at) / 1.day
    "#{age.to_i} days"
  end

  column(:actions, :html => true, :mandatory => true) do |record|
    link_to "Delete", "javascript:alert('Oh common! This is demo.')"
  end




end

Controller:

class UsersController < ApplicationController

  def index
    @users_grid = UsersGrid.new(params[:users_grid]) do |scope|
      scope.page(params[:page])
    end
  end
end

View:

<h3>Users Grid</h3>

<div class="right">
  <%= render :partial => "shared/source", :object => @users_grid %>
</div>
<div class="left">
  <%= datagrid_form_for @users_grid, url: users_path %>


  <div class='total'>Total: <%= @users_grid.assets.total_count %></div>
  <%= datagrid_table(@users_grid) %>
  <%= paginate @users_grid.assets %>
</div>

-
-
Total: 100
Id Name Disabled Actions
82 Kasey Lemke Yes Delete
31 Syble Ziemann Yes Delete
40 Alayna Swaniawski No Delete
81 Clyde Bruen Yes Delete
84 Zackery Dibbert No Delete
100 Mikayla Goldner Yes Delete
58 Broderick Cartwright No Delete
118 Ernie Considine Yes Delete
29 Hortense Leffler No Delete
34 Keyon DuBuque Yes Delete
33 Demetrius Bergstrom No Delete
45 Jacky Kreiger No Delete
101 Makenna Jakubowski Yes Delete
89 Gretchen Lowe Yes Delete
27 Monserrate Swaniawski Yes Delete
59 Sheldon Harvey Yes Delete
74 Kolby Murazik No Delete
94 Colin Harber No Delete
93 Bernhard Buckridge No Delete
114 Itzel Littel No Delete
21 Albina Morissette Yes Delete
35 Rodolfo Lueilwitz No Delete
41 River Bergnaum No Delete
79 Isabel Okuneva Yes Delete
77 Jeffry Osinski Yes Delete