The problem with traditional table layouts
Traditional tables are notorious for having bad responsiveness. On mobile, there is only so much flexibility you have when rendering content that is horizontally long:- Allow the user to scroll horizontally to see more table content
- Hide non-important columns on smaller devices
visibleFrom() or hiddenFrom() method:
- Simple stacking - Use
stackedOnMobile()to automatically stack all cells vertically on mobile without changing your column definitions - Custom layouts - Use
Split,Stack, and other layout components for fine-grained control over how content appears at each breakpoint
Stacking table cells on mobile
The simplest way to make your table responsive is to use thestackedOnMobile() method on the table. This automatically converts the traditional horizontal table layout into a vertical card-like layout on mobile screens, while preserving the standard table appearance on larger screens:
Custom column layouts
Filament lets you build responsive table-like interfaces, without touching HTML or CSS. These layouts let you define exactly where content appears in a table row, at each responsive breakpoint.Allowing columns to stack on mobile
Let’s introduce a component -Split:
Split component is used to wrap around columns, and allow them to stack on mobile.
By default, columns within a split will appear aside each other all the time. However, you may choose a responsive breakpoint where this behavior starts from(). Before this point, the columns will stack on top of each other:
md breakpoint devices onwards:
Preventing a column from creating whitespace
Splits, like table columns, will automatically adjust their whitespace to ensure that each column has proportionate separation. You can prevent this from happening, usinggrow(false). In this example, we will make sure that the avatar image will sit tightly against the name column:
grow() will adjust to consume the newly-freed space:
Stacking within a split
Inside a split, you may stack multiple columns on top of each other vertically. This allows you to display more data inside fewer columns on desktop:Hiding a stack on mobile
Similar to individual columns, you may choose to hide a stack based on the responsive breakpoint of the browser. To do this, you may use avisibleFrom() method:
Aligning stacked content
By default, columns within a stack are aligned to the start. You may choose to align columns within a stack to theAlignment::Center or Alignment::End:
grow(false) set, otherwise they will stretch to fill the entire width of the stack and follow their own alignment configuration instead of the stack’s.
Spacing stacked content
By default, stacked content has no vertical padding between columns. To add some, you may use thespace() method, which accepts either 1, 2, or 3, corresponding to Tailwind’s spacing scale:
Controlling column width using a grid
Sometimes, using aSplit creates inconsistent widths when columns contain lots of content. This is because it’s powered by Flexbox internally and each row individually controls how much space is allocated to content.
Instead, you may use a Grid layout, which uses CSS Grid Layout to allow you to control column widths:
lg breakpoint.
You may choose to customize the number of columns within the grid at other breakpoints:
Collapsible content
When you’re using a column layout like split or stack, then you can also add collapsible content. This is very useful for when you don’t want to display all data in the table at once, but still want it to be accessible to the user if they need to access it, without navigating away. Split and stack components can be madecollapsible(), but there is also a dedicated Panel component that provides a pre-styled background color and border radius, to separate the collapsible content from the rest:
collapsed(false) method:
Arranging records into a grid
Sometimes, you may find that your data fits into a grid format better than a list. Filament can handle that too! Simply use the$table->contentGrid() method:
- On mobile, they will be displayed in 1 column only.
- From the
mdbreakpoint, they will be displayed in 2 columns. - From the
xlbreakpoint onwards, they will be displayed in 3 columns.
sm to 2xl can contain 1 to 12 columns.
Custom HTML
You may add custom HTML to your table using aView component. It can even be collapsible():
/resources/views/users/table/collapsible-row-content.blade.php file, and add in your HTML. You can access the table record using $getRecord():
Embedding other components
You could even pass in columns or other layout components to thecomponents() method: