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
46 Anderson Brown No Delete
47 Makenna Stark Yes Delete
48 Austin Heathcote Yes Delete
49 Dell Champlin Yes Delete
50 Danika Bins No Delete
51 Kamron Graham Yes Delete
52 Lora Kilback No Delete
53 Erika Zboncak No Delete
54 Michael King No Delete
55 Floy Bergnaum No Delete
56 Moises Kuhlman No Delete
57 Ambrose Marvin No Delete
58 Broderick Cartwright No Delete
59 Sheldon Harvey Yes Delete
60 Cathryn Boehm Yes Delete
61 Garfield Altenwerth Yes Delete
62 Evert Lakin No Delete
63 Kayli Swaniawski No Delete
64 Lula Hyatt No Delete
65 Rosanna Schowalter Yes Delete
66 Talon Kuhlman No Delete
67 Wyman McClure No Delete
68 Louisa Franecki Yes Delete
69 Estevan Reilly Yes Delete
70 Mittie Quigley No Delete