Skip to content

Grid view

Some resources are best displayed in a grid view. We can do that with Avo using a cover_url, a title, and a body.

Avo grid view

Enable grid view

To enable grid view for a resource, you need to configure the grid_view class attribute on the resource. That will add the grid view to the view switcher on the Index view.

ruby
class Avo::Resources::Post < Avo::BaseResource
  self.grid_view = {
    card: -> do
      {
        cover_url:
          if record.cover_photo.attached?
            main_app.url_for(record.cover_photo.url)
          end,
        title: record.name,
        body: record.truncated_body
      }
    end
  }
end

Options

Next, you should configure a few things for the grid card.

title

What should be used as the title of the card.

ruby
self.grid_view = {
  card: -> do
    {
      title: record.title
    }
  end
}

body

What should be used as the body of the card. You can use this field to display a description of the record.

ruby
self.grid_view = {
  card: -> do
    {
      body: record.truncated_body
    }
  end
}

cover_url

What should be used as the cover URL of the card.

ruby
self.grid_view = {
  card: -> do
    {
      cover_url: record.image.attached? ? main_app.url_for(record.image.variant(resize_to_fill: [300, 300])) : nil
    }
  end
}

If nil is given, a default placeholder image will be used.

badge

Optionally you may add a badge to give more context to the card or make it stand out.

See below a list of options you can configure for the badge.

Avo view switcher

Make grid the default view

To make the grid the default way of viewing a resource Index, we have to use the default_view_type class attribute.

ruby
class Avo::Resources::Post < Avo::BaseResource
  self.default_view_type = :grid
end

Custom style

You may want to customize the card a little bit. That's possible using the html option.

ruby
class Avo::Resources::Post < Avo::BaseResource
  self.grid_view = {
    card: -> do
      {
        cover_url:
          if record.cover_photo.attached?
            main_app.url_for(record.cover_photo.url)
          end,
        title: record.name,
        body: record.truncated_body
      }
    end,
    html: -> do
      {
        title: {
          index: {
            wrapper: {
              classes: "bg-blue-50 rounded-md p-2"
            }
          }
        },
        body: {
          index: {
            wrapper: {
              classes: "bg-gray-50 rounded-md p-1"
            }
          }
        },
        cover: {
          index: {
            wrapper: {
              classes: "blur-sm"
            }
          }
        }
      }
    end
  }
end
Grid html option

Grid Item Badge

Avo Grid View Badge Element

You can display and customize a badge on top of your grid items. Badges are useful for showing status indicators, labels, or other visual cues that help users quickly identify important information about each item.

Complete Example

ruby
# Dynamic badge based on record status
self.grid_view = {
  card: -> do
    {
      cover_url: record.image.attached? ? main_app.url_for(record.image.variant(resize_to_fill: [300, 300])) : nil,
      title: record.title,
      body: simple_format(record.description),
      badge: {
        label: record.new? ? "New" : "Updated",
        color: record.new? ? "green" : "orange",
        style: record.new? ? "solid" : "subtle",
        title: record.new? ? "New product available" : "Recently updated",
        icon: record.new? ? "heroicons/outline/arrow-trending-up" : "heroicons/outline/arrow-path"
      }
    }
  end
}

Options

label

The visible text displayed on the badge. This is the primary content that users will see on your grid items.

ruby
self.grid_view = {
  card: -> do
    {
      badge: { label: "New" }
    }
  end
}
Avo Grid View Badge Label

color

Sets the badge color. Accepts a static value or a proc for dynamic coloring based on the record.

Available colors

Base colors: red, orange, amber, yellow, lime, green, emerald, teal, cyan, sky, blue, indigo, violet, purple, fuchsia, pink, rose

Semantic colors: neutral, success, danger, warning, info

ruby
self.grid_view = {
  card: -> do
    {
      badge: {
        label: "New",
        color: "green"
      }
    }
  end
}

style

Controls the badge appearance style.

Available styles

  • subtle - Light background with colored text (default)
  • solid - Solid colored background with white text
ruby
self.grid_view = {
  card: -> do
    {
      badge: {
        label: "New",
        color: "green",
        style: "solid"
      }
    }
  end
}

title

The tooltip text that appears when users hover over the badge. Useful for providing additional context or detailed information.

ruby
self.grid_view = {
  card: -> do
    {
      badge: {
        label: "New",
        title: "New product available"
      }
    }
  end
}
Avo Grid View Badge Title

icon

Adds an icon to the badge.

ruby
self.grid_view = {
  card: -> do
    {
      badge: {
        label: "New",
        color: "green",
        icon: "tabler/outline/trending-up"
      }
    }
  end
}