Browse Source

Fix BookStore Blazor tutorial for Blazorise 2.x and MudBlazor

pull/26317/head
maliming 4 weeks ago
parent
commit
c8c49edbcf
No known key found for this signature in database GPG Key ID: A646B9CB645ECEA4
  1. 26
      docs/en/tutorials/book-store/part-02.md
  2. 223
      docs/en/tutorials/book-store/part-03.md
  3. 67
      docs/en/tutorials/book-store/part-05.md
  4. 104
      docs/en/tutorials/book-store/part-09.md
  5. 55
      docs/en/tutorials/book-store/part-10.md

26
docs/en/tutorials/book-store/part-02.md

@ -584,14 +584,14 @@ Open the `Books.razor` and replace the content as the following:
Field="@nameof(BookDto.Type)"
Caption="@L["Type"]">
<DisplayTemplate>
@L[$"Enum:BookType.{context.Type}"]
@L[$"Enum:BookType.{(int)context.Item.Type}"]
</DisplayTemplate>
</DataGridColumn>
<DataGridColumn TItem="BookDto"
Field="@nameof(BookDto.PublishDate)"
Caption="@L["PublishDate"]">
<DisplayTemplate>
@context.PublishDate.ToShortDateString()
@context.Item.PublishDate.ToShortDateString()
</DisplayTemplate>
</DataGridColumn>
<DataGridColumn TItem="BookDto"
@ -602,7 +602,7 @@ Open the `Books.razor` and replace the content as the following:
Field="@nameof(BookDto.CreationTime)"
Caption="@L["CreationTime"]">
<DisplayTemplate>
@context.CreationTime.ToLongDateString()
@context.Item.CreationTime.ToLongDateString()
</DisplayTemplate>
</DataGridColumn>
</DataGridColumns>
@ -651,9 +651,11 @@ Open the `Books.razor` and replace the content as the following:
</CardHeaderContent>
</MudCardHeader>
<MudCardContent>
<MudDataGrid T="BookDto"
<MudDataGrid @ref="_booksDataGrid"
T="BookDto"
ServerData="OnDataGridReadAsync"
RowsPerPage="@PageSize">
RowsPerPage="@PageSize"
SortMode="SortMode.None">
<Columns>
<PropertyColumn Property="x => x.Name"
Title="@L["Name"]" />
@ -678,12 +680,17 @@ Open the `Books.razor` and replace the content as the following:
</CellTemplate>
</PropertyColumn>
</Columns>
<PagerContent>
<MudDataGridPager T="BookDto" />
</PagerContent>
</MudDataGrid>
</MudCardContent>
</MudCard>
@code
{
private MudDataGrid<BookDto> _booksDataGrid = default!;
public Books() // Constructor
{
LocalizationResource = typeof(BookStoreResource);
@ -697,6 +704,7 @@ Open the `Books.razor` and replace the content as the following:
* `Entities`, `TotalCount`, `PageSize`, `OnDataGridReadAsync` are defined in the base class.
* `LocalizationResource` is set to the `BookStoreResource` to localize the texts.
* This page uses the standard MudBlazor `MudDataGrid` with `<PropertyColumn>` definitions. ABP also ships an `AbpMudExtensibleDataGrid` that integrates with the [data table column extension system](../../framework/ui/blazor/data-table-column-extensions.md) when you need to extend module pages.
* `AbpMudCrudPageBase` uses its own `AbpMudExtensibleDataGrid` to reload the data and to resolve the sort columns. Since this page uses the standard `MudDataGrid`, it keeps a reference to the grid (`_booksDataGrid`) to reload it after the create, update and delete operations in the next part, and disables sorting with `SortMode.None`.
While the code above is pretty easy to understand, you can check the MudBlazor [Card](https://mudblazor.com/components/card) and [DataGrid](https://mudblazor.com/components/datagrid) documents to understand them better.
@ -712,6 +720,14 @@ You can run the application! The final UI of this part is shown below:
![blazor-bookstore-book-list](images/blazor-bookstore-book-list-2.png)
{{if BlazorUI == "MudBlazor"}}
This is a fully working, server side paged and localized table of books.
{{else}}
This is a fully working, server side paged, sorted and localized table of books.
{{end}}
{{end # UI }}

223
docs/en/tutorials/book-store/part-03.md

@ -1147,9 +1147,8 @@ Now, we can add a modal that will be opened when we click the button.
Open the `Books.razor` and add the following code to the end of the page:
````xml
<Modal @ref="@CreateModal">
<ModalBackdrop />
<ModalContent IsCentered="true">
<Modal @ref="@CreateModal" Centered="true">
<ModalContent>
<Form>
<ModalHeader>
<ModalTitle>@L["NewBook"]</ModalTitle>
@ -1160,16 +1159,16 @@ Open the `Books.razor` and add the following code to the end of the page:
<Validation MessageLocalizer="@LH.Localize">
<Field>
<FieldLabel>@L["Name"]</FieldLabel>
<TextEdit @bind-Text="@NewEntity.Name">
<TextInput @bind-Value="@NewEntity.Name">
<Feedback>
<ValidationError/>
</Feedback>
</TextEdit>
</TextInput>
</Field>
</Validation>
<Field>
<FieldLabel>@L["Type"]</FieldLabel>
<Select TValue="BookType" @bind-SelectedValue="@NewEntity.Type">
<Select TValue="BookType" @bind-Value="@NewEntity.Type">
@foreach (int bookTypeValue in Enum.GetValues(typeof(BookType)))
{
<SelectItem TValue="BookType" Value="@((BookType) bookTypeValue)">
@ -1180,11 +1179,11 @@ Open the `Books.razor` and add the following code to the end of the page:
</Field>
<Field>
<FieldLabel>@L["PublishDate"]</FieldLabel>
<DateEdit TValue="DateTime" @bind-Date="NewEntity.PublishDate"/>
<DateInput TValue="DateTime" @bind-Value="NewEntity.PublishDate"/>
</Field>
<Field>
<FieldLabel>@L["Price"]</FieldLabel>
<NumericEdit TValue="float" @bind-Value="NewEntity.Price"/>
<NumericInput TValue="float" @bind-Value="NewEntity.Price"/>
</Field>
</Validations>
</ModalBody>
@ -1236,7 +1235,7 @@ Open the `Books.razor` and add the following code to the end of the page:
<MudSelectItem Value="@bookTypeValue">@L[$"Enum:BookType.{(int)bookTypeValue}"]</MudSelectItem>
}
</MudSelect>
<MudDatePicker @bind-Date="@NewEntity.PublishDate"
<MudDatePicker @bind-Date="@NewEntityPublishDate"
Label="@L["PublishDate"]" />
<MudNumericField T="float"
@bind-Value="@NewEntity.Price"
@ -1257,7 +1256,30 @@ Open the `Books.razor` and add the following code to the end of the page:
* The `_createDialog` field, `CloseCreateDialogAsync`, `CreateFormRef` and `CreateEntityAsync` are all defined in `AbpMudCrudPageBase`. Check the [MudBlazor documentation](https://mudblazor.com/components/dialog) if you want to understand the `MudDialog` and other components.
* `MudDialog.Options` widens the dialog (`MaxWidth.Medium` + `FullWidth`) so the form fields are not cramped.
* `MudStack` with `Spacing="3"` keeps the inputs visually separated; without it MudBlazor inputs render flush against each other.
* `MudDatePicker.@bind-Date` requires a nullable `DateTime?`. If your DTO uses non-nullable `DateTime`, change it to `DateTime?` (`public DateTime? PublishDate { get; set; }`) when using the MudBlazor variant.
* `MudDatePicker.@bind-Date` requires a nullable `DateTime?`, while `CreateUpdateBookDto.PublishDate` is a non-nullable `DateTime`. So the date picker binds to the `NewEntityPublishDate` wrapper property below.
Add the following property to the `@code` block of the `Books.razor` file:
````csharp
// MudDatePicker requires nullable DateTime, while CreateUpdateBookDto.PublishDate is non-nullable.
// Bind to a nullable wrapper and sync back to the DTO.
private DateTime? NewEntityPublishDate
{
get => NewEntity?.PublishDate;
set { if (NewEntity != null && value.HasValue) NewEntity.PublishDate = value.Value; }
}
````
`AbpMudCrudPageBase` reloads its own `AbpMudExtensibleDataGrid` after creating a book. Override the `OnCreatedEntityAsync` method in the `@code` block to reload the `MudDataGrid` of this page instead:
````csharp
protected override async Task OnCreatedEntityAsync()
{
await InvokeAsync(() => _booksDataGrid.ReloadServerData());
await CloseCreateDialogAsync();
Snackbar.Add(GetCreateMessage(), Severity.Success);
}
````
{{end}}
@ -1281,7 +1303,7 @@ Open the `Books.razor` and add the following `DataGridEntityActionsColumn` secti
<EntityActions TItem="BookDto" EntityActionsColumn="@EntityActionsColumn">
<EntityAction TItem="BookDto"
Text="@L["Edit"]"
Clicked="() => OpenEditModalAsync(context)" />
Clicked="() => OpenEditModalAsync(context.Item)" />
</EntityActions>
</DisplayTemplate>
</DataGridEntityActionsColumn>
@ -1324,29 +1346,28 @@ We can now define a modal to edit the book. Add the following code to the end of
{{if BlazorUI == "Blazorise"}}
````xml
<Modal @ref="@EditModal">
<ModalBackdrop />
<ModalContent IsCentered="true">
<Modal @ref="@EditModal" Centered="true">
<ModalContent>
<Form>
<ModalHeader>
<ModalTitle>@EditingEntity.Name</ModalTitle>
<CloseButton Clicked="CloseEditModalAsync"/>
</ModalHeader>
<ModalBody>
<Validations @ref="@EditValidationsRef" Model="@NewEntity" ValidateOnLoad="false">
<Validations @ref="@EditValidationsRef" Model="@EditingEntity" ValidateOnLoad="false">
<Validation MessageLocalizer="@LH.Localize">
<Field>
<FieldLabel>@L["Name"]</FieldLabel>
<TextEdit @bind-Text="@EditingEntity.Name">
<TextInput @bind-Value="@EditingEntity.Name">
<Feedback>
<ValidationError/>
</Feedback>
</TextEdit>
</TextInput>
</Field>
</Validation>
<Field>
<FieldLabel>@L["Type"]</FieldLabel>
<Select TValue="BookType" @bind-SelectedValue="@EditingEntity.Type">
<Select TValue="BookType" @bind-Value="@EditingEntity.Type">
@foreach (int bookTypeValue in Enum.GetValues(typeof(BookType)))
{
<SelectItem TValue="BookType" Value="@((BookType) bookTypeValue)">
@ -1357,11 +1378,11 @@ We can now define a modal to edit the book. Add the following code to the end of
</Field>
<Field>
<FieldLabel>@L["PublishDate"]</FieldLabel>
<DateEdit TValue="DateTime" @bind-Date="EditingEntity.PublishDate"/>
<DateInput TValue="DateTime" @bind-Value="EditingEntity.PublishDate"/>
</Field>
<Field>
<FieldLabel>@L["Price"]</FieldLabel>
<NumericEdit TValue="float" @bind-Value="EditingEntity.Price"/>
<NumericInput TValue="float" @bind-Value="EditingEntity.Price"/>
</Field>
</Validations>
</ModalBody>
@ -1402,7 +1423,7 @@ We can now define a modal to edit the book. Add the following code to the end of
<MudSelectItem Value="@bookTypeValue">@L[$"Enum:BookType.{(int)bookTypeValue}"]</MudSelectItem>
}
</MudSelect>
<MudDatePicker @bind-Date="@EditingEntity.PublishDate"
<MudDatePicker @bind-Date="@EditingEntityPublishDate"
Label="@L["PublishDate"]" />
<MudNumericField T="float"
@bind-Value="@EditingEntity.Price"
@ -1419,6 +1440,27 @@ We can now define a modal to edit the book. Add the following code to the end of
</MudDialog>
````
Add the following property to the `@code` block of the `Books.razor` file, as the edit dialog's date picker binds to it:
````csharp
private DateTime? EditingEntityPublishDate
{
get => EditingEntity?.PublishDate;
set { if (EditingEntity != null && value.HasValue) EditingEntity.PublishDate = value.Value; }
}
````
Also override the `OnUpdatedEntityAsync` method to reload the grid after updating a book:
````csharp
protected override async Task OnUpdatedEntityAsync()
{
await InvokeAsync(() => _booksDataGrid.ReloadServerData());
await CloseEditDialogAsync();
Snackbar.Add(GetUpdateMessage(), Severity.Success);
}
````
{{end}}
### Mapperly Configuration
@ -1430,6 +1472,7 @@ Open the `BookStoreBlazorMappers` inside the {{ if UI == "BlazorServer" }}`Acme.
```csharp
using Riok.Mapperly.Abstractions;
using Volo.Abp.Mapperly;
using Acme.BookStore.Books;
{{ if UI == "BlazorServer" }}namespace Acme.BookStore.Blazor; {{ else if UI == "MAUIBlazor" }}namespace Acme.BookStore.MauiBlazor; {{ else }}namespace Acme.BookStore.Blazor.Client;{{ end }}
@ -1461,8 +1504,8 @@ Add the following `EntityAction` code under the "Edit" action inside `EntityActi
````xml
<EntityAction TItem="BookDto"
Text="@L["Delete"]"
Clicked="() => DeleteEntityAsync(context)"
ConfirmationMessage="() => GetDeleteConfirmationMessage(context)" />
Clicked="() => DeleteEntityAsync(context.Item)"
ConfirmationMessage="() => GetDeleteConfirmationMessage(context.Item)" />
````
{{end}}
@ -1472,16 +1515,32 @@ Add the following `EntityAction` code under the "Edit" action inside `EntityActi
Add the following `MudMenuItem` after the "Edit" item inside the actions `MudMenu`:
````razor
<MudMenuItem OnClick="@(async () => {
if (await Message.Confirm(GetDeleteConfirmationMessage(context.Item)))
{
await DeleteEntityAsync(context.Item);
}
})">
<MudMenuItem OnClick="@(() => DeleteEntityAsync(context.Item))">
@L["Delete"]
</MudMenuItem>
````
`DeleteEntityAsync` asks the user to confirm (with the message returned by `GetDeleteConfirmationMessage`) before deleting the book.
Override the `OnDeletedEntityAsync` method to reload the grid after deleting a book (and go back to the previous page if the current page becomes empty):
````csharp
protected override async Task OnDeletedEntityAsync()
{
// Go back to the previous page if the deleted book was the only one on the current page
if (_booksDataGrid.CurrentPage > 0 && _booksDataGrid.ServerItems.Count() == 1)
{
await InvokeAsync(() => _booksDataGrid.NavigateTo(Page.Previous));
}
else
{
await InvokeAsync(() => _booksDataGrid.ReloadServerData());
}
Snackbar.Add(GetDeleteMessage(), Severity.Success);
}
````
{{end}}
* `DeleteEntityAsync` is defined in the base class that deletes the entity by performing a call to the server.
@ -1526,7 +1585,7 @@ Here's the complete code to create the book management CRUD page, that has been
<DataGrid TItem="BookDto"
Data="Entities"
ReadData="OnDataGridReadAsync"
CurrentPage="CurrentPage"
Page="CurrentPage"
TotalItems="TotalCount"
ShowPager="true"
PageSize="PageSize">
@ -1536,11 +1595,11 @@ Here's the complete code to create the book management CRUD page, that has been
<EntityActions TItem="BookDto" EntityActionsColumn="@EntityActionsColumn">
<EntityAction TItem="BookDto"
Text="@L["Edit"]"
Clicked="() => OpenEditModalAsync(context)" />
Clicked="() => OpenEditModalAsync(context.Item)" />
<EntityAction TItem="BookDto"
Text="@L["Delete"]"
Clicked="() => DeleteEntityAsync(context)"
ConfirmationMessage="()=>GetDeleteConfirmationMessage(context)" />
Clicked="() => DeleteEntityAsync(context.Item)"
ConfirmationMessage="()=>GetDeleteConfirmationMessage(context.Item)" />
</EntityActions>
</DisplayTemplate>
</DataGridEntityActionsColumn>
@ -1551,14 +1610,14 @@ Here's the complete code to create the book management CRUD page, that has been
Field="@nameof(BookDto.Type)"
Caption="@L["Type"]">
<DisplayTemplate>
@L[$"Enum:BookType.{context.Type:D}"]
@L[$"Enum:BookType.{(int)context.Item.Type}"]
</DisplayTemplate>
</DataGridColumn>
<DataGridColumn TItem="BookDto"
Field="@nameof(BookDto.PublishDate)"
Caption="@L["PublishDate"]">
<DisplayTemplate>
@context.PublishDate.ToShortDateString()
@context.Item.PublishDate.ToShortDateString()
</DisplayTemplate>
</DataGridColumn>
<DataGridColumn TItem="BookDto"
@ -1569,7 +1628,7 @@ Here's the complete code to create the book management CRUD page, that has been
Field="@nameof(BookDto.CreationTime)"
Caption="@L["CreationTime"]">
<DisplayTemplate>
@context.CreationTime.ToLongDateString()
@context.Item.CreationTime.ToLongDateString()
</DisplayTemplate>
</DataGridColumn>
</DataGridColumns>
@ -1577,9 +1636,8 @@ Here's the complete code to create the book management CRUD page, that has been
</CardBody>
</Card>
<Modal @ref="@CreateModal">
<ModalBackdrop />
<ModalContent IsCentered="true">
<Modal @ref="@CreateModal" Centered="true">
<ModalContent>
<Form>
<ModalHeader>
<ModalTitle>@L["NewBook"]</ModalTitle>
@ -1590,16 +1648,16 @@ Here's the complete code to create the book management CRUD page, that has been
<Validation MessageLocalizer="@LH.Localize">
<Field>
<FieldLabel>@L["Name"]</FieldLabel>
<TextEdit @bind-Text="@NewEntity.Name">
<TextInput @bind-Value="@NewEntity.Name">
<Feedback>
<ValidationError/>
</Feedback>
</TextEdit>
</TextInput>
</Field>
</Validation>
<Field>
<FieldLabel>@L["Type"]</FieldLabel>
<Select TValue="BookType" @bind-SelectedValue="@NewEntity.Type">
<Select TValue="BookType" @bind-Value="@NewEntity.Type">
@foreach (int bookTypeValue in Enum.GetValues(typeof(BookType)))
{
<SelectItem TValue="BookType" Value="@((BookType) bookTypeValue)">
@ -1610,11 +1668,11 @@ Here's the complete code to create the book management CRUD page, that has been
</Field>
<Field>
<FieldLabel>@L["PublishDate"]</FieldLabel>
<DateEdit TValue="DateTime" @bind-Date="NewEntity.PublishDate"/>
<DateInput TValue="DateTime" @bind-Value="NewEntity.PublishDate"/>
</Field>
<Field>
<FieldLabel>@L["Price"]</FieldLabel>
<NumericEdit TValue="float" @bind-Value="NewEntity.Price"/>
<NumericInput TValue="float" @bind-Value="NewEntity.Price"/>
</Field>
</Validations>
</ModalBody>
@ -1630,29 +1688,28 @@ Here's the complete code to create the book management CRUD page, that has been
</ModalContent>
</Modal>
<Modal @ref="@EditModal">
<ModalBackdrop />
<ModalContent IsCentered="true">
<Modal @ref="@EditModal" Centered="true">
<ModalContent>
<Form>
<ModalHeader>
<ModalTitle>@EditingEntity.Name</ModalTitle>
<CloseButton Clicked="CloseEditModalAsync"/>
</ModalHeader>
<ModalBody>
<Validations @ref="@EditValidationsRef" Model="@NewEntity" ValidateOnLoad="false">
<Validations @ref="@EditValidationsRef" Model="@EditingEntity" ValidateOnLoad="false">
<Validation MessageLocalizer="@LH.Localize">
<Field>
<FieldLabel>@L["Name"]</FieldLabel>
<TextEdit @bind-Text="@EditingEntity.Name">
<TextInput @bind-Value="@EditingEntity.Name">
<Feedback>
<ValidationError/>
</Feedback>
</TextEdit>
</TextInput>
</Field>
</Validation>
<Field>
<FieldLabel>@L["Type"]</FieldLabel>
<Select TValue="BookType" @bind-SelectedValue="@EditingEntity.Type">
<Select TValue="BookType" @bind-Value="@EditingEntity.Type">
@foreach (int bookTypeValue in Enum.GetValues(typeof(BookType)))
{
<SelectItem TValue="BookType" Value="@((BookType) bookTypeValue)">
@ -1663,11 +1720,11 @@ Here's the complete code to create the book management CRUD page, that has been
</Field>
<Field>
<FieldLabel>@L["PublishDate"]</FieldLabel>
<DateEdit TValue="DateTime" @bind-Date="EditingEntity.PublishDate"/>
<DateInput TValue="DateTime" @bind-Value="EditingEntity.PublishDate"/>
</Field>
<Field>
<FieldLabel>@L["Price"]</FieldLabel>
<NumericEdit TValue="float" @bind-Value="EditingEntity.Price"/>
<NumericInput TValue="float" @bind-Value="EditingEntity.Price"/>
</Field>
</Validations>
</ModalBody>
@ -1716,15 +1773,17 @@ Here's the complete code to create the book management CRUD page, that has been
</CardHeaderActions>
</MudCardHeader>
<MudCardContent>
<MudDataGrid T="BookDto"
<MudDataGrid @ref="_booksDataGrid"
T="BookDto"
ServerData="OnDataGridReadAsync"
RowsPerPage="@PageSize">
RowsPerPage="@PageSize"
SortMode="SortMode.None">
<Columns>
<TemplateColumn T="BookDto" Title="@L["Actions"]" Sortable="false">
<CellTemplate>
<MudMenu Icon="@Icons.Material.Filled.MoreVert" Dense="true">
<MudMenuItem OnClick="@(() => OpenEditDialogAsync(context.Item))">@L["Edit"]</MudMenuItem>
<MudMenuItem OnClick="@(async () => { if (await Message.Confirm(GetDeleteConfirmationMessage(context.Item))) { await DeleteEntityAsync(context.Item); } })">@L["Delete"]</MudMenuItem>
<MudMenuItem OnClick="@(() => DeleteEntityAsync(context.Item))">@L["Delete"]</MudMenuItem>
</MudMenu>
</CellTemplate>
</TemplateColumn>
@ -1746,6 +1805,9 @@ Here's the complete code to create the book management CRUD page, that has been
</CellTemplate>
</PropertyColumn>
</Columns>
<PagerContent>
<MudDataGridPager T="BookDto" />
</PagerContent>
</MudDataGrid>
</MudCardContent>
</MudCard>
@ -1769,7 +1831,7 @@ Here's the complete code to create the book management CRUD page, that has been
<MudSelectItem Value="@bookTypeValue">@L[$"Enum:BookType.{(int)bookTypeValue}"]</MudSelectItem>
}
</MudSelect>
<MudDatePicker @bind-Date="@NewEntity.PublishDate"
<MudDatePicker @bind-Date="@NewEntityPublishDate"
Label="@L["PublishDate"]" />
<MudNumericField T="float"
@bind-Value="@NewEntity.Price"
@ -1804,7 +1866,7 @@ Here's the complete code to create the book management CRUD page, that has been
<MudSelectItem Value="@bookTypeValue">@L[$"Enum:BookType.{(int)bookTypeValue}"]</MudSelectItem>
}
</MudSelect>
<MudDatePicker @bind-Date="@EditingEntity.PublishDate"
<MudDatePicker @bind-Date="@EditingEntityPublishDate"
Label="@L["PublishDate"]" />
<MudNumericField T="float"
@bind-Value="@EditingEntity.Price"
@ -1822,6 +1884,51 @@ Here's the complete code to create the book management CRUD page, that has been
@code
{
// MudDatePicker requires nullable DateTime, while CreateUpdateBookDto.PublishDate is non-nullable.
// Bind to a nullable wrapper and sync back to the DTO.
private DateTime? NewEntityPublishDate
{
get => NewEntity?.PublishDate;
set { if (NewEntity != null && value.HasValue) NewEntity.PublishDate = value.Value; }
}
private DateTime? EditingEntityPublishDate
{
get => EditingEntity?.PublishDate;
set { if (EditingEntity != null && value.HasValue) EditingEntity.PublishDate = value.Value; }
}
private MudDataGrid<BookDto> _booksDataGrid = default!;
protected override async Task OnCreatedEntityAsync()
{
await InvokeAsync(() => _booksDataGrid.ReloadServerData());
await CloseCreateDialogAsync();
Snackbar.Add(GetCreateMessage(), Severity.Success);
}
protected override async Task OnUpdatedEntityAsync()
{
await InvokeAsync(() => _booksDataGrid.ReloadServerData());
await CloseEditDialogAsync();
Snackbar.Add(GetUpdateMessage(), Severity.Success);
}
protected override async Task OnDeletedEntityAsync()
{
// Go back to the previous page if the deleted book was the only one on the current page
if (_booksDataGrid.CurrentPage > 0 && _booksDataGrid.ServerItems.Count() == 1)
{
await InvokeAsync(() => _booksDataGrid.NavigateTo(Page.Previous));
}
else
{
await InvokeAsync(() => _booksDataGrid.ReloadServerData());
}
Snackbar.Add(GetDeleteMessage(), Severity.Success);
}
public Books() // Constructor
{
LocalizationResource = typeof(BookStoreResource);

67
docs/en/tutorials/book-store/part-05.md

@ -80,10 +80,6 @@ public class BookStorePermissionDefinitionProvider : PermissionDefinitionProvide
{
var bookStoreGroup = context.AddGroup(BookStorePermissions.GroupName, L("Permission:BookStore"));
//Dashboard permissions
bookStoreGroup.AddPermission(BookStorePermissions.Dashboard.Host, L("Permission:Dashboard"), MultiTenancySides.Host);
bookStoreGroup.AddPermission(BookStorePermissions.Dashboard.Tenant, L("Permission:Dashboard"), MultiTenancySides.Tenant);
//Books permissions
var booksPermission = bookStoreGroup.AddPermission(BookStorePermissions.Books.Default, L("Permission:Books"));
booksPermission.AddChild(BookStorePermissions.Books.Create, L("Permission:Books.Create"));
@ -421,19 +417,16 @@ The base `AbpCrudPageBase` class already has the necessary functionality for the
#### Set the Policy (Permission) Names
Add the following code block to the end of the `Books.razor` file:
Update the constructor in the `@code` block of the `Books.razor` file as shown below:
````csharp
@code
public Books() // Constructor
{
public Books() // Constructor
{
LocalizationResource = typeof(BookStoreResource);
CreatePolicyName = BookStorePermissions.Books.Create;
UpdatePolicyName = BookStorePermissions.Books.Edit;
DeletePolicyName = BookStorePermissions.Books.Delete;
}
LocalizationResource = typeof(BookStoreResource);
CreatePolicyName = BookStorePermissions.Books.Create;
UpdatePolicyName = BookStorePermissions.Books.Edit;
DeletePolicyName = BookStorePermissions.Books.Delete;
}
````
@ -449,6 +442,8 @@ The base `AbpCrudPageBase` class automatically checks these permissions on the r
Wrap the *New Book* button by an `if` block as shown below:
{{if BlazorUI == "Blazorise"}}
````xml
@if (HasCreatePermission)
{
@ -457,8 +452,25 @@ Wrap the *New Book* button by an `if` block as shown below:
}
````
{{end}}
{{if BlazorUI == "MudBlazor"}}
````razor
@if (HasCreatePermission)
{
<MudButton Variant="Variant.Filled"
Color="Color.Primary"
OnClick="OpenCreateDialogAsync">@L["NewBook"]</MudButton>
}
````
{{end}}
#### Hide the Edit/Delete Actions
{{if BlazorUI == "Blazorise"}}
`EntityAction` component defines `Visible` attribute (parameter) to conditionally show the action.
Update the `EntityActions` section as shown below:
@ -468,15 +480,36 @@ Update the `EntityActions` section as shown below:
<EntityAction TItem="BookDto"
Text="@L["Edit"]"
Visible=HasUpdatePermission
Clicked="() => OpenEditModalAsync(context)" />
Clicked="() => OpenEditModalAsync(context.Item)" />
<EntityAction TItem="BookDto"
Text="@L["Delete"]"
Visible=HasDeletePermission
Clicked="() => DeleteEntityAsync(context)"
ConfirmationMessage="()=>GetDeleteConfirmationMessage(context)" />
Clicked="() => DeleteEntityAsync(context.Item)"
ConfirmationMessage="()=>GetDeleteConfirmationMessage(context.Item)" />
</EntityActions>
````
{{end}}
{{if BlazorUI == "MudBlazor"}}
Wrap the menu items inside the actions `MudMenu` by `if` blocks as shown below:
````razor
<MudMenu Icon="@Icons.Material.Filled.MoreVert" Dense="true">
@if (HasUpdatePermission)
{
<MudMenuItem OnClick="@(() => OpenEditDialogAsync(context.Item))">@L["Edit"]</MudMenuItem>
}
@if (HasDeletePermission)
{
<MudMenuItem OnClick="@(() => DeleteEntityAsync(context.Item))">@L["Delete"]</MudMenuItem>
}
</MudMenu>
````
{{end}}
#### About the Permission Caching
You can run and test the permissions. Remove a book related permission from the admin role to see the related button/action disappears from the UI.

104
docs/en/tutorials/book-store/part-09.md

@ -873,7 +873,7 @@ Create a new Razor Component Page, `/Pages/Authors.razor`, in the {{ if UI == "B
ShowPager="true"
PageSize="PageSize">
<DataGridColumns>
<DataGridColumn Width="150px"
<DataGridColumn Width="Width.Px(150)"
TItem="AuthorDto"
Field="@nameof(AuthorDto.Id)"
Sortable="false"
@ -886,13 +886,13 @@ Create a new Razor Component Page, `/Pages/Authors.razor`, in the {{ if UI == "B
<DropdownMenu>
@if (CanEditAuthor)
{
<DropdownItem Clicked="() => OpenEditAuthorModal(context)">
<DropdownItem Clicked="() => OpenEditAuthorModal(context.Item)">
@L["Edit"]
</DropdownItem>
}
@if (CanDeleteAuthor)
{
<DropdownItem Clicked="() => DeleteAuthorAsync(context)">
<DropdownItem Clicked="() => DeleteAuthorAsync(context.Item)">
@L["Delete"]
</DropdownItem>
}
@ -907,7 +907,7 @@ Create a new Razor Component Page, `/Pages/Authors.razor`, in the {{ if UI == "B
Field="@nameof(AuthorDto.BirthDate)"
Caption="@L["BirthDate"]">
<DisplayTemplate>
@context.BirthDate.ToShortDateString()
@context.Item.BirthDate.ToShortDateString()
</DisplayTemplate>
</DataGridColumn>
</DataGridColumns>
@ -915,9 +915,8 @@ Create a new Razor Component Page, `/Pages/Authors.razor`, in the {{ if UI == "B
</CardBody>
</Card>
<Modal @ref="CreateAuthorModal">
<ModalBackdrop />
<ModalContent IsCentered="true">
<Modal @ref="CreateAuthorModal" Centered="true">
<ModalContent>
<Form>
<ModalHeader>
<ModalTitle>@L["NewAuthor"]</ModalTitle>
@ -928,25 +927,25 @@ Create a new Razor Component Page, `/Pages/Authors.razor`, in the {{ if UI == "B
<Validation MessageLocalizer="@LH.Localize">
<Field>
<FieldLabel>@L["Name"]</FieldLabel>
<TextEdit @bind-Text="@NewAuthor.Name">
<TextInput @bind-Value="@NewAuthor.Name">
<Feedback>
<ValidationError/>
</Feedback>
</TextEdit>
</TextInput>
</Field>
</Validation>
<Field>
<FieldLabel>@L["BirthDate"]</FieldLabel>
<DateEdit TValue="DateTime" @bind-Date="@NewAuthor.BirthDate"/>
<DateInput TValue="DateTime" @bind-Value="@NewAuthor.BirthDate"/>
</Field>
<Validation MessageLocalizer="@LH.Localize">
<Field>
<FieldLabel>@L["ShortBio"]</FieldLabel>
<MemoEdit Rows="5" @bind-Text="@NewAuthor.ShortBio">
<MemoInput Rows="5" @bind-Value="@NewAuthor.ShortBio">
<Feedback>
<ValidationError/>
</Feedback>
</MemoEdit>
</MemoInput>
</Field>
</Validation>
</Validations>
@ -967,9 +966,8 @@ Create a new Razor Component Page, `/Pages/Authors.razor`, in the {{ if UI == "B
</ModalContent>
</Modal>
<Modal @ref="EditAuthorModal">
<ModalBackdrop />
<ModalContent IsCentered="true">
<Modal @ref="EditAuthorModal" Centered="true">
<ModalContent>
<Form>
<ModalHeader>
<ModalTitle>@EditingAuthor.Name</ModalTitle>
@ -980,25 +978,25 @@ Create a new Razor Component Page, `/Pages/Authors.razor`, in the {{ if UI == "B
<Validation MessageLocalizer="@LH.Localize">
<Field>
<FieldLabel>@L["Name"]</FieldLabel>
<TextEdit @bind-Text="@EditingAuthor.Name">
<TextInput @bind-Value="@EditingAuthor.Name">
<Feedback>
<ValidationError/>
</Feedback>
</TextEdit>
</TextInput>
</Field>
</Validation>
<Field>
<FieldLabel>@L["BirthDate"]</FieldLabel>
<DateEdit TValue="DateTime" @bind-Date="@EditingAuthor.BirthDate"/>
<DateInput TValue="DateTime" @bind-Value="@EditingAuthor.BirthDate"/>
</Field>
<Validation>
<Field>
<FieldLabel>@L["ShortBio"]</FieldLabel>
<MemoEdit Rows="5" @bind-Text="@EditingAuthor.ShortBio">
<MemoInput Rows="5" @bind-Value="@EditingAuthor.ShortBio">
<Feedback>
<ValidationError/>
</Feedback>
</MemoEdit>
</MemoInput>
</Field>
</Validation>
</Validations>
@ -1048,7 +1046,8 @@ Create a new Razor Component Page, `/Pages/Authors.razor`, in the {{ if UI == "B
</CardHeaderActions>
</MudCardHeader>
<MudCardContent>
<MudDataGrid T="AuthorDto"
<MudDataGrid @ref="AuthorsGrid"
T="AuthorDto"
ServerData="OnDataGridReadAsync"
RowsPerPage="@PageSize">
<Columns>
@ -1079,6 +1078,9 @@ Create a new Razor Component Page, `/Pages/Authors.razor`, in the {{ if UI == "B
</CellTemplate>
</PropertyColumn>
</Columns>
<PagerContent>
<MudDataGridPager T="AuthorDto" PageSizeSelector="false" />
</PagerContent>
</MudDataGrid>
</MudCardContent>
</MudCard>
@ -1238,26 +1240,26 @@ public partial class Authors
await InvokeAsync(StateHasChanged);
}
private void OpenCreateAuthorModal()
private async Task OpenCreateAuthorModal()
{
CreateValidationsRef.ClearAll();
await CreateValidationsRef.ClearAll();
NewAuthor = new CreateAuthorDto();
CreateAuthorModal.Show();
await CreateAuthorModal.Show();
}
private void CloseCreateAuthorModal()
private async Task CloseCreateAuthorModal()
{
CreateAuthorModal.Hide();
await CreateAuthorModal.Hide();
}
private void OpenEditAuthorModal(AuthorDto author)
private async Task OpenEditAuthorModal(AuthorDto author)
{
EditValidationsRef.ClearAll();
await EditValidationsRef.ClearAll();
EditingAuthorId = author.Id;
EditingAuthor = ObjectMapper.Map<AuthorDto, UpdateAuthorDto>(author);
EditAuthorModal.Show();
await EditAuthorModal.Show();
}
private async Task DeleteAuthorAsync(AuthorDto author)
@ -1279,9 +1281,9 @@ public partial class Authors
}
}
private void CloseEditAuthorModal()
private async Task CloseEditAuthorModal()
{
EditAuthorModal.Hide();
await EditAuthorModal.Hide();
}
private async Task CreateAuthorAsync()
@ -1292,7 +1294,7 @@ public partial class Authors
{
await AuthorAppService.CreateAsync(NewAuthor);
await GetAuthorsAsync();
CreateAuthorModal.Hide();
await CreateAuthorModal.Hide();
}
}
catch(Exception ex)
@ -1309,7 +1311,7 @@ public partial class Authors
{
await AuthorAppService.UpdateAsync(EditingAuthorId, EditingAuthor);
await GetAuthorsAsync();
EditAuthorModal.Hide();
await EditAuthorModal.Hide();
}
}
catch(Exception ex)
@ -1328,6 +1330,7 @@ public partial class Authors
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Acme.BookStore.Authors;
using Acme.BookStore.Permissions;
@ -1355,6 +1358,8 @@ public partial class Authors
private Guid EditingAuthorId { get; set; }
private UpdateAuthorDto EditingAuthor { get; set; }
private MudDataGrid<AuthorDto> AuthorsGrid { get; set; }
private MudDialog CreateAuthorDialog { get; set; }
private MudDialog EditAuthorDialog { get; set; }
@ -1365,7 +1370,7 @@ public partial class Authors
// Bind to a nullable wrapper and sync back to the DTO before saving.
private DateTime? NewAuthorBirthDate
{
get => NewAuthor?.BirthDate;
get => NewAuthor == null || NewAuthor.BirthDate == default ? null : NewAuthor.BirthDate;
set { if (NewAuthor != null && value.HasValue) NewAuthor.BirthDate = value.Value; }
}
@ -1414,7 +1419,7 @@ public partial class Authors
TotalCount = (int)result.TotalCount;
}
private async Task<GridData<AuthorDto>> OnDataGridReadAsync(GridState<AuthorDto> state)
private async Task<GridData<AuthorDto>> OnDataGridReadAsync(GridState<AuthorDto> state, CancellationToken cancellationToken = default)
{
CurrentSorting = state.SortDefinitions
.Where(s => !string.IsNullOrWhiteSpace(s.SortBy?.ToString()))
@ -1463,7 +1468,16 @@ public partial class Authors
}
await AuthorAppService.DeleteAsync(author.Id);
await GetAuthorsAsync();
// Go back to the previous page if the deleted author was the only one on the current page
if (AuthorsGrid.CurrentPage > 0 && AuthorsGrid.ServerItems.Count() == 1)
{
await InvokeAsync(() => AuthorsGrid.NavigateTo(Page.Previous));
}
else
{
await InvokeAsync(() => AuthorsGrid.ReloadServerData());
}
}
catch(Exception ex)
{
@ -1475,11 +1489,11 @@ public partial class Authors
{
try
{
await CreateFormRef.Validate();
await CreateFormRef.ValidateAsync();
if (CreateFormRef.IsValid)
{
await AuthorAppService.CreateAsync(NewAuthor);
await GetAuthorsAsync();
await InvokeAsync(() => AuthorsGrid.ReloadServerData());
await InvokeAsync(() => CreateAuthorDialog.CloseAsync());
}
}
@ -1493,11 +1507,11 @@ public partial class Authors
{
try
{
await EditFormRef.Validate();
await EditFormRef.ValidateAsync();
if (EditFormRef.IsValid)
{
await AuthorAppService.UpdateAsync(EditingAuthorId, EditingAuthor);
await GetAuthorsAsync();
await InvokeAsync(() => AuthorsGrid.ReloadServerData());
await InvokeAsync(() => EditAuthorDialog.CloseAsync());
}
}
@ -1540,11 +1554,11 @@ public partial class AuthorDtoToUpdateAuthorDtoMapper : MapperBase<AuthorDto, Up
Open the `BookStoreMenuContributor.cs` in the {{ if UI == "BlazorServer" }}`Acme.BookStore.Blazor`{{ else if UI == "MAUIBlazor" }}`Acme.BookStore.MauiBlazor`{{ else }}`Acme.BookStore.Blazor.Client`{{ end }} project and add the following code to the end of the `ConfigureMainMenuAsync` method:
````csharp
context.Menu.AddItem(new ApplicationMenuItem(
"BooksStore.Authors",
l["Menu:Authors"],
url: "/authors"
).RequirePermissions(BookStorePermissions.Books.Default));
bookStoreMenu.AddItem(new ApplicationMenuItem(
"BooksStore.Authors",
l["Menu:Authors"],
url: "/authors"
).RequirePermissions(BookStorePermissions.Authors.Default));
````
### Localizations

55
docs/en/tutorials/book-store/part-10.md

@ -1160,6 +1160,12 @@ protected override async Task OnInitializedAsync()
* It is essential to call the `base.OnInitializedAsync()` since `AbpCrudPageBase` has some initialization code to be executed.
The code below throws a `UserFriendlyException`, which is defined in the `Volo.Abp` namespace. Add the following line to the top of the `Books.razor` file:
````html
@using Volo.Abp
````
{{if BlazorUI == "Blazorise"}}
Override the `OpenCreateModalAsync` method and adding the following code:
@ -1250,6 +1256,51 @@ The final `@code` block should be the following:
DeletePolicyName = BookStorePermissions.Books.Delete;
}
// MudDatePicker requires nullable DateTime, while CreateUpdateBookDto.PublishDate is non-nullable.
// Bind to a nullable wrapper and sync back to the DTO.
private DateTime? NewEntityPublishDate
{
get => NewEntity?.PublishDate;
set { if (NewEntity != null && value.HasValue) NewEntity.PublishDate = value.Value; }
}
private DateTime? EditingEntityPublishDate
{
get => EditingEntity?.PublishDate;
set { if (EditingEntity != null && value.HasValue) EditingEntity.PublishDate = value.Value; }
}
private MudDataGrid<BookDto> _booksDataGrid = default!;
protected override async Task OnCreatedEntityAsync()
{
await InvokeAsync(() => _booksDataGrid.ReloadServerData());
await CloseCreateDialogAsync();
Snackbar.Add(GetCreateMessage(), Severity.Success);
}
protected override async Task OnUpdatedEntityAsync()
{
await InvokeAsync(() => _booksDataGrid.ReloadServerData());
await CloseEditDialogAsync();
Snackbar.Add(GetUpdateMessage(), Severity.Success);
}
protected override async Task OnDeletedEntityAsync()
{
// Go back to the previous page if the deleted book was the only one on the current page
if (_booksDataGrid.CurrentPage > 0 && _booksDataGrid.ServerItems.Count() == 1)
{
await InvokeAsync(() => _booksDataGrid.NavigateTo(Page.Previous));
}
else
{
await InvokeAsync(() => _booksDataGrid.ReloadServerData());
}
Snackbar.Add(GetDeleteMessage(), Severity.Success);
}
//GET AUTHORS ON INITIALIZATION
protected override async Task OnInitializedAsync()
{
@ -1279,7 +1330,7 @@ Finally, add the following field definition into the *Create* modal/dialog, as t
````xml
<Field>
<FieldLabel>@L["Author"]</FieldLabel>
<Select TValue="Guid" @bind-SelectedValue="@NewEntity.AuthorId">
<Select TValue="Guid" @bind-Value="@NewEntity.AuthorId">
@foreach (var author in authorList)
{
<SelectItem TValue="Guid" Value="@author.Id">
@ -1324,7 +1375,7 @@ Add the following field definition into the *Edit* modal/dialog, as the first it
````xml
<Field>
<FieldLabel>@L["Author"]</FieldLabel>
<Select TValue="Guid" @bind-SelectedValue="@EditingEntity.AuthorId">
<Select TValue="Guid" @bind-Value="@EditingEntity.AuthorId">
@foreach (var author in authorList)
{
<SelectItem TValue="Guid" Value="@author.Id">

Loading…
Cancel
Save