You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
112 lines
4.4 KiB
112 lines
4.4 KiB
![]()
2 years ago
|
---
|
||
|
sidebar_position: 8
|
||
|
title: Image display
|
||
|
---
|
||
|
|
||
|
We have now added a new item with its image, so it's time to manage it in our table.
|
||
|
|
||
|
Open the `Pages/List.razor` file and add the highlighted lines:
|
||
|
|
||
|
```cshtml title="Pages/List.razor"
|
||
|
<DataGrid TItem="Item"
|
||
|
Data="@items"
|
||
|
ReadData="@OnReadData"
|
||
|
TotalItems="@totalItem"
|
||
|
PageSize="10"
|
||
|
ShowPager
|
||
|
Responsive>
|
||
|
<DataGridColumn TItem="Item" Field="@nameof(Item.Id)" Caption="#" />
|
||
|
// highlight-start
|
||
|
<DataGridColumn TItem="Item" Field="@nameof(Item.Id)" Caption="Image">
|
||
|
<DisplayTemplate>
|
||
|
<img src="images/@(context.Name).png" class="img-thumbnail" title="@context.DisplayName" alt="@context.DisplayName" style="max-width: 150px" />
|
||
|
</DisplayTemplate>
|
||
|
</DataGridColumn>
|
||
|
// highlight-end
|
||
|
<DataGridColumn TItem="Item" Field="@nameof(Item.DisplayName)" Caption="Display name" />
|
||
|
<DataGridColumn TItem="Item" Field="@nameof(Item.StackSize)" Caption="Stack size" />
|
||
|
<DataGridColumn TItem="Item" Field="@nameof(Item.MaxDurability)" Caption="Maximum durability" />
|
||
|
<DataGridColumn TItem="Item" Field="@nameof(Item.EnchantCategories)" Caption="Enchant categories">
|
||
|
<DisplayTemplate>
|
||
|
@(string.Join(", ", ((Item)context).EnchantCategories))
|
||
|
</DisplayTemplate>
|
||
|
</DataGridColumn>
|
||
|
<DataGridColumn TItem="Item" Field="@nameof(Item.RepairWith)" Caption="Repair with">
|
||
|
<DisplayTemplate>
|
||
|
@(string.Join(", ", ((Item)context).RepairWith))
|
||
|
</DisplayTemplate>
|
||
|
</DataGridColumn>
|
||
|
<DataGridColumn TItem="Item" Field="@nameof(Item.CreatedDate)" Caption="Created date" DisplayFormat="{0:d}" DisplayFormatProvider="@System.Globalization.CultureInfo.GetCultureInfo("fr-FR")" />
|
||
|
</DataGrid>
|
||
|
```
|
||
|
|
||
|
Our image is well displayed but on the other hand the images of our false data are not present.
|
||
|
|
||
|
Let's modify the code to take into account the following image as the default image.
|
||
|
|
||
|

|
||
|
|
||
|
Download the image and place it in the `wwwroot/images/default.png` folder.
|
||
|
|
||
|

|
||
|
|
||
|
Open the `Pages/List.razor.cs` file and add the highlighted lines:
|
||
|
|
||
|
```csharp title="Pages/List.razor.cs"
|
||
|
...
|
||
|
|
||
|
[Inject]
|
||
|
public ILocalStorageService LocalStorage { get; set; }
|
||
|
|
||
|
// highlight-start
|
||
|
[Inject]
|
||
|
public IWebHostEnvironment WebHostEnvironment { get; set; }
|
||
|
// highlight-end
|
||
|
|
||
|
[Inject]
|
||
|
public NavigationManager NavigationManager { get; set; }
|
||
|
|
||
|
...
|
||
|
```
|
||
|
|
||
|
Open the `Pages/List.razor` file and edit the highlighted lines:
|
||
|
|
||
|
```cshtml title="Pages/List.razor"
|
||
|
<DataGrid TItem="Item"
|
||
|
Data="@items"
|
||
|
ReadData="@OnReadData"
|
||
|
TotalItems="@totalItem"
|
||
|
PageSize="10"
|
||
|
ShowPager
|
||
|
Responsive>
|
||
|
<DataGridColumn TItem="Item" Field="@nameof(Item.Id)" Caption="#" />
|
||
|
// highlight-start
|
||
|
<DataGridColumn TItem="Item" Field="@nameof(Item.Id)" Caption="Image">
|
||
|
<DisplayTemplate>
|
||
|
@if (File.Exists($"{WebHostEnvironment.WebRootPath}/images/{context.Name}.png"))
|
||
|
{
|
||
|
<img src="images/@(context.Name).png" class="img-thumbnail" title="@context.DisplayName" alt="@context.DisplayName" style="max-width: 150px"/>
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
<img src="images/default.png" class="img-thumbnail" title="@context.DisplayName" alt="@context.DisplayName" style="max-width: 150px"/>
|
||
|
}
|
||
|
</DisplayTemplate>
|
||
|
</DataGridColumn>
|
||
|
// highlight-end
|
||
|
<DataGridColumn TItem="Item" Field="@nameof(Item.DisplayName)" Caption="Display name" />
|
||
|
<DataGridColumn TItem="Item" Field="@nameof(Item.StackSize)" Caption="Stack size" />
|
||
|
<DataGridColumn TItem="Item" Field="@nameof(Item.MaxDurability)" Caption="Maximum durability" />
|
||
|
<DataGridColumn TItem="Item" Field="@nameof(Item.EnchantCategories)" Caption="Enchant categories">
|
||
|
<DisplayTemplate>
|
||
|
@(string.Join(", ", ((Item)context).EnchantCategories))
|
||
|
</DisplayTemplate>
|
||
|
</DataGridColumn>
|
||
|
<DataGridColumn TItem="Item" Field="@nameof(Item.RepairWith)" Caption="Repair with">
|
||
|
<DisplayTemplate>
|
||
|
@(string.Join(", ", ((Item)context).RepairWith))
|
||
|
</DisplayTemplate>
|
||
|
</DataGridColumn>
|
||
|
<DataGridColumn TItem="Item" Field="@nameof(Item.CreatedDate)" Caption="Created date" DisplayFormat="{0:d}" DisplayFormatProvider="@System.Globalization.CultureInfo.GetCultureInfo("fr-FR")" />
|
||
|
</DataGrid>
|
||
|
```
|