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
106 Oleta Rau Yes Delete
107 King Schmidt Yes Delete
27 Monserrate Swaniawski Yes Delete
34 Keyon DuBuque Yes Delete
47 Makenna Stark Yes Delete
59 Sheldon Harvey Yes Delete
112 Juliana Blick Yes Delete
77 Jeffry Osinski Yes Delete
78 Davion Koepp Yes Delete
115 Frida Goyette Yes Delete
116 Jennings Stroman Yes Delete
79 Isabel Okuneva Yes Delete
118 Ernie Considine Yes Delete
80 Nola Goyette Yes Delete
120 Domingo Franecki Yes Delete
86 Tod Heller No Delete
87 Blaise Lang No Delete
88 Triston Lynch No Delete
90 Jalyn Mante No Delete
92 Elaina Schiller No Delete
93 Bernhard Buckridge No Delete
94 Colin Harber No Delete
95 Monroe Rowe No Delete
97 Lemuel Cummings No Delete
103 Carmel Huel No Delete