Skip to content

Commit eebdeab

Browse files
authored
Blazor movie tutorial QuickGrid styling (#34227)
1 parent 0e26ada commit eebdeab

File tree

3 files changed

+62
-6
lines changed

3 files changed

+62
-6
lines changed

aspnetcore/blazor/components/quickgrid.md

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,18 +138,36 @@ To provide a UI for pagination, add a [`Paginator` component](xref:Microsoft.Asp
138138

139139
In the running app, page through the items using a rendered `Paginator` component.
140140

141-
QuickGrid renders additional empty rows to fill in the final page of data when used with a `Paginator` component. In .NET 9 or later, empty data cells (`<td></td>`) are added to the empty rows. The empty rows are intended to facilitate rendering the QuickGrid with stable row height and styling across all pages. You can apply styles to the rows using [CSS isolation](xref:blazor/components/css-isolation) by wrapping the `QuickGrid` component in a wrapper element, such as a `<div>`, and applying a row style with `::deep` [pseudo-elements](https://developer.mozilla.org/docs/Web/CSS/Pseudo-elements):
141+
QuickGrid renders additional empty rows to fill in the final page of data when used with a `Paginator` component. In .NET 9 or later, empty data cells (`<td></td>`) are added to the empty rows. The empty rows are intended to facilitate rendering the QuickGrid with stable row height and styling across all pages.
142+
143+
## Apply row styles
144+
145+
Apply styles to rows using [CSS isolation](xref:blazor/components/css-isolation), which can include styling empty rows for `QuickGrid` components that [page data with a `Paginator` component](#page-items-with-a-paginator-component).
146+
147+
Wrap the `QuickGrid` component in a wrapper block element, for example a `<div>`:
148+
149+
```diff
150+
+ <div>
151+
<QuickGrid ...>
152+
...
153+
</QuickGrid>
154+
+ </div>
155+
```
156+
157+
Apply a row style with the `::deep` [pseudo-element](https://developer.mozilla.org/docs/Web/CSS/Pseudo-elements). In the following example, row height is set to `2em`, including for empty data rows.
158+
159+
`{COMPONENT}.razor.css`:
142160

143161
```css
144162
::deep tr {
145163
height: 2em;
146164
}
147165
```
148166

149-
To hide the empty row data cells rendered by the QuickGrid, use CSS styling. In the following isolated CSS styles:
167+
Alternatively, use the following CSS styling approach:
150168

151-
* Row cells populated with data are displayed.
152-
* Empty row data cells aren't displayed, which avoids empty row cell borders from rendering per Bootstrap styling.
169+
* Display row cells populated with data.
170+
* Don't display empty row cells, which avoids empty row cell borders from rendering per Bootstrap styling.
153171

154172
`{COMPONENT}.razor.css`:
155173

aspnetcore/blazor/tutorials/movie-database-app/part-8.md

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,11 @@ Run the app and navigate to the movies `Index` page. You can page through the mo
118118

119119
The component is *interactive*. The page doesn't reload for paging to occur. The paging is performed live over the SignalR connection between the browser and the server, where the paging operation is performed on the server with the rendered result sent back to the client for the browser to display.
120120

121-
Change <xref:Microsoft.AspNetCore.Components.QuickGrid.PaginationState.ItemsPerPage%2A> to a more reasonable value, such as 10 items per page:
121+
Change <xref:Microsoft.AspNetCore.Components.QuickGrid.PaginationState.ItemsPerPage%2A> to a more reasonable value, such as five items per page:
122122

123123
```diff
124124
- private PaginationState pagination = new PaginationState { ItemsPerPage = 2 };
125-
+ private PaginationState pagination = new PaginationState { ItemsPerPage = 10 };
125+
+ private PaginationState pagination = new PaginationState { ItemsPerPage = 5 };
126126
```
127127

128128
## Sortable `QuickGrid`
@@ -199,6 +199,44 @@ Filtering database records is performed on the server, and the server interactiv
199199

200200
Instead of an HTML form, submitting a GET request in this scenario could've also used JavaScript to submit the request to the server, either using the [Fetch API](https://developer.mozilla.org/docs/Web/API/Fetch_API)` or [XMLHttpRequest API](https://developer.mozilla.org/docs/Web/API/XMLHttpRequest). In most cases, JavaScript can be replaced by using Blazor and C# in an interactive component.
201201

202+
## Style the `QuickGrid` component
203+
204+
You can apply styles to the rendered `QuickGrid` component with a stylesheet isolated to the `Index` component using *CSS isolation*.
205+
206+
CSS isolation is applied by adding a stylesheet file using the file name format `{COMPONENT NAME}.razor.css`, where the `{COMPONENT NAME}` placeholder is the component name.
207+
208+
To apply styles to a child component, such as the `QuickGrid` component of the `Index` component, use the `::deep` pseudo-element.
209+
210+
In the `MoviePages` folder, add the following stylesheet for the `Index` component. Use `::deep` pseudo-elements to make the row height `3em` and vertically center the table cell content.
211+
212+
`Components/Pages/MoviePages/Index.razor.css`:
213+
214+
```css
215+
::deep tr {
216+
height: 3em;
217+
}
218+
219+
::deep tr > td {
220+
vertical-align: middle;
221+
}
222+
```
223+
224+
The `::deep` pseudo-element only works with descendant elements, so the `QuickGrid` component must be wrapped with a `<div>` or some other block-level element in order to apply the styles to it.
225+
226+
In `Components/Pages/MoviePages/Index.razor`, place `<div>` tags around the `QuickGrid` component:
227+
228+
```diff
229+
+ <div>
230+
<QuickGrid ...>
231+
...
232+
</QuickGrid>
233+
+ </div>
234+
```
235+
236+
Blazor rewrites CSS selectors to match the markup rendered by the component. The rewritten CSS styles are bundled and produced as a static asset, so you don't need to take further action to apply the styles to the rendered `QuickGrid` component.
237+
238+
![Movie list showing row heights at 3em with vertically-centered content](~/blazor/tutorials/movie-database-app/part-8/_static/styled-quickgrid.png)
239+
202240
## Clean up
203241

204242
:::zone pivot="vs"
125 KB
Loading

0 commit comments

Comments
 (0)