blazor - 如何在 blazor 中动态显示图像标签?
问题描述
我有一个包含可排序表的页面。
在表头列中,我有:
<div>Inv. # @SortGlyph("inventory_number")</div>
在我的代码块中
public string SortGlyph(string field)
{
if (field == _orderBy)
{
return _direction == "asc" ? "<img src='/images/uparrow.gif'>" : "<img src='/images/downarrow.gif'>";
}
return "";
}
我认为我需要使用 RenderFragment 来完成此操作,但我无法弄清楚如何开始使用它。
解决方案
直接回答:
这是 Blazor 方式:
<div>Inv. #
@if IsSortedUpBy("inventory_number") {
<img src='/images/uparrow.gif'>
}
else if IsSortedDownBy("inventory_number") {
<img src='/images/downarrow.gif'>
}
</div>
代码:
public bool IsSortedUpBy(string field) => field == _orderBy && _direction == "asc";
public bool IsSortedUpBy(string field) => field == _orderBy && _direction != "asc";
奖励曲目:
请记住,您可以使用参数将行为封装在您自己的 Blazor 组件中:
<arrowdiv
label="Inv. # "
state="@GetArrowStateForField(inventory_number)">
</arrowdiv>
推荐阅读
- javascript - 如果服务器不读取内容而是发送响应,则 XMLHttpRequest 发布文件失败(即使使用代码 200)
- javascript - 当我将 KnockoutJS 导入我正在测试的 ViewModel 时,为什么 Jest 会出现最大调用堆栈大小超出的错误?
- hibernate - 在 Spring Boot 中,我的 jpa 模型针对数据库上的所有模式进行了验证
- angular - Angular:*ngIf 内部的函数会导致性能问题,但有什么替代方法?
- vb.net - 使用公共类在 2 个表单之间共享事件
- android - [编辑我的日志代码]尽管按下相机按钮,但没有保存纹理视图照片
- sql - 连接 Oracle 中多行的列值而不重复
- java - 在哪里可以找到 Spring Boot 指标的描述?
- http - 什么是 HttpClient 中的跃点?
- r - 如何在同一图表中将(n 个未知)数据系列数绘制为 geom_line