Asp.net core 服务器端blazor中的省略号
如何在服务器端blazor中获得省略号?因此,如果显示的文本长度为200个字符,则需要截断并显示“显示更多”链接,但如果用户决定单击“显示更多”,则需要渲染整个文本 注:Asp.net core 服务器端blazor中的省略号,asp.net-core,blazor,ellipsis,Asp.net Core,Blazor,Ellipsis,如何在服务器端blazor中获得省略号?因此,如果显示的文本长度为200个字符,则需要截断并显示“显示更多”链接,但如果用户决定单击“显示更多”,则需要渲染整个文本 注: 我不想使用css 我是blazor的新手,但做了一些研究,没有找到任何东西。这是我创建的一个快速组件,您可以使用它 演示: @GetDisplayText() @如果(可以扩展) { @如果(扩展) { 少展示 } 其他的 { 显示更多 } } @代码{ [参数]公共字符串文本{get;set;} [参数]公共整数Max
- 我不想使用css
我是blazor的新手,但做了一些研究,没有找到任何东西。这是我创建的一个快速组件,您可以使用它 演示:
@GetDisplayText()
@如果(可以扩展)
{
@如果(扩展)
{
少展示
}
其他的
{
显示更多
}
}
@代码{
[参数]公共字符串文本{get;set;}
[参数]公共整数MaxCharacters{get;set;}=200;
公共布尔展开{get;set;}
public bool CanBeExpanded=>Text.Length>MaxCharacters;
公共字符串GetDisplayText()
{
返回IsExpanded?文本:截断(文本,最大字符);
}
公共字符串截断(字符串值,int-maxChars)
{
返回值。长度谢谢你的回答,一切都很好,但我不明白的是它是如何重新加载/刷新子组件的,我希望有某种状态已更改或甚至回调。你能解释一下吗?@umair当你更新父组件上的属性时,会自动通知子组件更新用户界面会刷新,反过来也是一样的。
<div>
@GetDisplayText()
@if (CanBeExpanded)
{
@if (IsExpanded)
{
<a @onclick="@(() => { IsExpanded = false; })" style="font-style: initial; font-size: 0.7em; color: dimgray; cursor: pointer;">Show less</a>
}
else
{
<a @onclick="@(() => { IsExpanded = true; })" style="font-style: initial; font-size: 0.7em; color: dimgray; cursor: pointer;">Show more</a>
}
}
</div>
@code {
[Parameter] public string Text { get; set; }
[Parameter] public int MaxCharacters { get; set; } = 200;
public bool IsExpanded { get; set; }
public bool CanBeExpanded => Text.Length > MaxCharacters;
public string GetDisplayText()
{
return IsExpanded ? Text : Truncate(Text, MaxCharacters);
}
public string Truncate(string value, int maxChars)
{
return value.Length <= maxChars ? value : value.Substring(0, maxChars) + "...";
}
}
<Component Text="SomeLongTextString"></Component>
// or if you want to change the max characters:
<Component Text="SomeLongTextString" MaxCharacters="350"></Component>