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
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
104 Devante Hegmann No Delete
105 Fausto Larkin No Delete
108 Leonardo Smith No Delete
109 Eunice Flatley No Delete
110 Myra Ullrich No Delete
111 Benedict Muller No Delete
113 Alycia Stracke No Delete
114 Itzel Littel No Delete
117 Joshuah Wilderman No Delete
119 Walter Cremin No Delete
28 Dorothea Schimmel No Delete
29 Hortense Leffler No Delete
32 Mathilde Huels No Delete
33 Demetrius Bergstrom No Delete
35 Rodolfo Lueilwitz No Delete