【问题标题】:ellipsis in server side blazor服务器端 Blazor 中的省略号
【发布时间】:2020-09-14 09:19:22
【问题描述】:

如何在服务器端 Blazor 中获得省略号?因此,如果显示的文本长度为 200 个字符,则要求截断并显示“显示更多”链接,但如果用户决定单击“显示更多”,则呈现整个文本。

注意:

  • 不想使用 css

我是 blazor 的新手,但做了一些研究,但没有找到任何东西。

【问题讨论】:

    标签: asp.net-core blazor ellipsis


    【解决方案1】:

    这是我创建的一个快速组件,您可以使用它。

    演示:https://blazorfiddle.com/s/yxnf021e

    <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>
    

    【讨论】:

    • 感谢您的回答,一切正常,但我无法理解的是它如何重新加载/刷新子组件,我期待有某种 StateHasChanged 或 EvenCallback。你能解释一下吗? @umair
    • 当您在父节点上更新属性时,子节点会自动收到更新通知并刷新 UI,反之亦然。
    猜你喜欢
    • 2019-10-03
    • 2020-03-25
    • 2021-05-28
    • 2020-05-15
    • 1970-01-01
    • 2020-09-19
    • 2020-04-14
    • 2020-09-26
    • 2020-12-09
    相关资源
    最近更新 更多