### User Grid Using Custom Base Class Source: https://github.com/bogdan/datagrid/blob/main/version-2/Readme.markdown An example of a `UsersGrid` that inherits from the custom `ApplicationGrid`, demonstrating the use of scopes and the custom `timestamp_column` helper. ```ruby # app/grids/users_grid.rb class UsersGrid < ApplicationGrid scope { User } column(:name) timestamp_column(:created_at) end ``` -------------------------------- ### Version 1 Datagrid Form HTML Source: https://github.com/bogdan/datagrid/blob/main/version-2/Readme.markdown Example HTML output for a Datagrid form in Version 1, showcasing its structure and CSS classes. ```html
-
Reset
``` -------------------------------- ### Run Tests with Appraisals Source: https://github.com/bogdan/datagrid/blob/main/CONTRIBUTING.md Use this method to run tests against different Rails versions using the appraisals gem. Ensure all dependencies are installed first. ```shell bundle install bundle exec appraisal install bundle exec appraisal rails-8.1 rake ``` ```shell bundle exec appraisal rails-7.0 rake ``` -------------------------------- ### Instantiate and Use a Datagrid Report Source: https://github.com/bogdan/datagrid/blob/main/README.md Instantiate a Datagrid report with specific parameters and access its data. This example demonstrates how to set filters, sorting, and retrieve the header, rows, and full data. The SQL query generated by the report is also shown. ```ruby report = UsersGrid.new( group_id: [1,2], logins_count: [1, nil], category: "first", order: :group, descending: true ) report.assets # => Array of User instances: # SELECT * FROM users WHERE users.group_id in (1,2) AND # users.logins_count >= 1 AND # users.category = 'first' # ORDER BY groups.name DESC report.header # => ["Name", "Group", "Activated"] report.rows # => [ # ["Steve", "Spammers", false], # [ "John", "Spoilers", false], # ["Berry", "Good people", true] # ] report.data # => [ header, *rows] report.to_csv # => Yes, it is ``` -------------------------------- ### Version 2 Datagrid Form HTML Source: https://github.com/bogdan/datagrid/blob/main/version-2/Readme.markdown Example HTML output for a Datagrid form in Version 2, demonstrating updated structure and CSS class names. ```html
-
Reset
``` -------------------------------- ### Export Datagrid to PDF using Ruport Source: https://github.com/bogdan/datagrid/wiki/PDF-Export Generates a PDF report from Datagrid data using the Ruport gem. Ensure 'ruport' is installed. ```ruby require 'ruport' # gem install ruport f = File.new("report.pdf", "w") f.write Ruport::Data::Table.new(:column_names =>report.header, :data => report.rows).to_pdf f.close ``` -------------------------------- ### Run Tests with BUNDLE_GEMFILE Source: https://github.com/bogdan/datagrid/blob/main/CONTRIBUTING.md Alternatively, run tests against specific Rails versions by setting the BUNDLE_GEMFILE environment variable. This requires installing dependencies for each specified Gemfile. ```shell BUNDLE_GEMFILE=gemfiles/rails_8.1.gemfile bundle install BUNDLE_GEMFILE=gemfiles/rails_8.1.gemfile bundle exec rake ``` ```shell BUNDLE_GEMFILE=gemfiles/rails_7.0.gemfile bundle install BUNDLE_GEMFILE=gemfiles/rails_7.0.gemfile bundle exec rake ``` -------------------------------- ### Migrating Column Class Option to Tag Options Source: https://github.com/bogdan/datagrid/blob/main/version-2/Readme.markdown Provides an example of migrating from the deprecated `class` column option to the more flexible `tag_options` in Datagrid V2. ```ruby # V1 column(:status, class: 'issue-status') # V2 column(:status, tag_options: {class: 'issue-status'}) ``` -------------------------------- ### V1 Filter Input Markup Source: https://github.com/bogdan/datagrid/blob/main/version-2/Readme.markdown Example of how filters were rendered in Datagrid V1, using CSS classes for meta-information like filter name and type. ```html
``` -------------------------------- ### Define a Datagrid Class Source: https://github.com/bogdan/datagrid/blob/main/README.md Define a Datagrid class by specifying the scope, filters, and columns. This example shows how to set up filters for category, disabled status, group ID, login counts, and group name, along with columns for name, group, and active status. ```ruby class UsersGrid < Datagrid::Base scope do User.includes(:group) end filter(:category, :enum, select: ["first", "second"]) filter(:disabled, :xboolean) filter(:group_id, :integer, multiple: true) filter(:logins_count, :integer, range: true) filter(:group_name, :string, header: "Group") do |value| self.joins(:group).where(groups: {name: value}) end column(:name) column(:group, order: -> { joins(:group).order(groups: :name) }) do |user| user.name end column(:active, header: "Activated") do |user| !user.disabled end end ``` -------------------------------- ### Configure Filter Input Type with input_options Source: https://github.com/bogdan/datagrid/blob/main/version-2/Readme.markdown Customize the HTML input type for filters using the `input_options` argument. This example shows how to revert to 'text' for date and integer types, and how to specify 'textarea' for string types. ```ruby filter(:created_at, :date, range: true, input_options: {type: 'text'}) filter(:salary, :integer, range: true, input_options: {type: 'text', step: nil}) ``` ```ruby class ApplicationGrid < Datagrid::Base def self.filter(name, type = :default, input_options: {}, **options) if [:date, :datetime, :float, :integer].include?(type) input_options[:type] ||= 'text' end super(name, type, input_options:, **options) end end ``` ```ruby # Rendered as