This is a migrated thread and some comments may be shown as answers.

Ellipsis and long text

3 Answers 1418 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Kieran
Top achievements
Rank 1
Kieran asked on 07 Jan 2021, 11:37 AM

 

Hi,

In my Blazor grid, I have a column which can have long text in it.  I need to use ellipsis on this column e.g put ellipsis after say 50 characters, with the full content then displayed in a tooltip when you hover on the ellipsis.
I know this was possible with your other grids, how can I do this with your blazor grid?  

 

Thanks,

3 Answers, 1 is accepted

Sort by
2
Accepted
Svetoslav Dimitrov
Telerik team
answered on 07 Jan 2021, 12:24 PM

Hello Kieran,

We have a Knowledge Base article that explains how to achieve the desired behavior. You can see it from this link. Also, you can tweak the CSS so that it fits the needs of your application. 

Regards,
Svetoslav Dimitrov
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

0
Kieran
Top achievements
Rank 1
answered on 07 Jan 2021, 12:53 PM

 

Thanks Svetoslav I had missed that article.

0
Leland
Top achievements
Rank 2
Iron
Iron
Iron
answered on 04 Apr 2024, 04:37 PM | edited on 04 Apr 2024, 04:37 PM

I used the code from the referenced knowledge base articles in many places throughout my project, so I ended up creating a self-contained component to handle this.  It can be used like this:

<GridColumn Field="@nameof(DataModel.Notes)" Title="Notes"
            Width="150px" >
    <Template>
        @{ var dataItem = (DataModel)context; }
        <OverflowTooltip Text="@dataItem.Notes" />
    </Template>
</GridColumn>

You'll need to have the TelerikTooltip set up somewhere in your project:

<TelerikTooltip TargetSelector=".tooltip-target" />


It uses the ellipsis and a tooltip to show the full text.  I even added JavaScript so that the tooltip is only displayed when the overflow is active.  Here's the code:

OverflowTooltip.razor:

<div @ref="DivRef" class="custom-ellipsis">
    @Text
</div>

OverflowTooltip.razor.css:

div.custom-ellipsis {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

OverflowTooltip.razor.cs:

using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;

namespace ProjectName.Components;

public partial class OverflowTooltip
{
    [Parameter]
    public string? Text { get; set; }

    private ElementReference DivRef { get; set; }

    [Inject]
    private IJSRuntime JSRuntime { get; set; } = default!;

    private static IJSObjectReference? JSModule { get; set; }

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (JSModule is null)
        {
            var path = "./Components/OverflowTooltip.razor.js";
            JSModule = await JSRuntime.InvokeAsync<IJSObjectReference>("import", path);
        }
        await JSModule.InvokeVoidAsync("initializeTooltipCheck", DivRef);
    }
}

OverflowTooltip.razor.js:

export function initializeTooltipCheck(element) {
    let isOverflowing = false;

    function checkOverflow() {
        isOverflowing = handleOverflowCheck(isOverflowing, element);
    }

    checkOverflow();

    const resizeObserver = new ResizeObserver(checkOverflow);
    resizeObserver.observe(element);

    element.addEventListener('DOMNodeRemoved', () => {
        resizeObserver.disconnect();
    });
}

function handleOverflowCheck(isOverflowing, element) {
    const currentOverflowing = element.offsetWidth < element.scrollWidth;
    if (currentOverflowing !== isOverflowing) {
        if (currentOverflowing) {
            element.classList.add("tooltip-target");
            element.setAttribute("title", element.textContent);
        } else {
            element.classList.remove("tooltip-target");
            element.removeAttribute("title");
        }
    }
    return currentOverflowing;
}
Tags
Grid
Asked by
Kieran
Top achievements
Rank 1
Answers by
Svetoslav Dimitrov
Telerik team
Kieran
Top achievements
Rank 1
Leland
Top achievements
Rank 2
Iron
Iron
Iron
Share this question
or