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
40 Alayna Swaniawski No Delete
21 Albina Morissette Yes Delete
113 Alycia Stracke No Delete
57 Ambrose Marvin No Delete
44 Anahi Rice Yes Delete
46 Anderson Brown No Delete
83 Ashlynn Wilderman No Delete
48 Austin Heathcote Yes Delete
111 Benedict Muller No Delete
93 Bernhard Buckridge No Delete
38 Berniece Weimann No Delete
87 Blaise Lang No Delete
58 Broderick Cartwright No Delete
103 Carmel Huel No Delete
60 Cathryn Boehm Yes Delete
25 Charley Carter No Delete
24 Christa Smith No Delete
81 Clyde Bruen Yes Delete
94 Colin Harber No Delete
72 Constance Ward No Delete
39 Cristopher Considine No Delete
50 Danika Bins No Delete
102 Darius Bins Yes Delete
78 Davion Koepp Yes Delete
49 Dell Champlin Yes Delete