TelerikCard Click

1 Answer 256 Views
Card
Igor
Top achievements
Rank 1
Igor asked on 17 May 2024, 09:44 AM

Hi there,

Is there a way to handle  the click anywhere on TelerikCard without wrapping it with a div and handling div.onclick?

 

Regards,

Igor

1 Answer, 1 is accepted

Sort by
0
Accepted
Svetoslav Dimitrov
Telerik team
answered on 22 May 2024, 05:39 AM

Hello Igor,

I can confirm that wrapping the TelerikCard in a <div> with an onclick event is the best way to achieve the desired behavior. 

Regards,
Svetoslav Dimitrov
Progress Telerik

Stay tuned by visiting our public roadmap and feedback portal pages! Or perhaps, if you are new to our Telerik family, check out our getting started resources!
Igor
Top achievements
Rank 1
commented on 22 May 2024, 06:59 AM

Can we have a way to handle OnClick out of the box in the future releases? (w/o wrapping manually)
Svetoslav Dimitrov
Telerik team
commented on 24 May 2024, 11:44 AM

Hello Igor,

What do you expect an OnClick on the <TelerikCard> to do more than what you can already achieve by wrapping the <TelerikCard> in a <div @onclick>?

Igor
Top achievements
Rank 1
commented on 27 May 2024, 11:06 AM

Hello Svetoslav,

I don't expect anything more. I just want to avoid wrapping it manually every time I need it. It's much simpler and more readable, convenient to have <TelerikCard OnClick="@..."> rather than <div @onclick="@..."><TelerikCard> ........

Svetoslav Dimitrov
Telerik team
commented on 29 May 2024, 11:51 AM | edited

Hello Igor,

You can wrap the TelerikCard, wrapped in a <div @onclick> in a custom component and expose parameters to make an easily reusable component (extract more parameters as needed):

<div @onclick="@OnCardClick">
    <TelerikCard Width="200px">
        <CardHeader>
            <CardTitle>Card Title</CardTitle>
        </CardHeader>
        <CardBody>
            <CardTitle>Rome</CardTitle>
            <CardSubTitle>Capital of Italy</CardSubTitle>
            <CardSeparator></CardSeparator>
            <p>
                Rome is a sprawling, cosmopolitan city with nearly 3,000 years of globally influential art, architecture and culture on display.

                Ancient ruins such as the Forum and the Colosseum evoke the power of the former Roman Empire.
            </p>
        </CardBody>
        <CardActions Layout="@CardActionsLayout.Stretch">
            <TelerikButton Class="k-flat" Icon="@SvgIcon.HeartOutline" Title="Like"></TelerikButton>
            <TelerikButton Class="k-flat" Icon="@SvgIcon.Comment" Title="Comment"></TelerikButton>
            <TelerikButton Class="k-flat">Read More</TelerikButton>
        </CardActions>
        <CardFooter>
            <span style="float:left">Created by @@john</span>
            <span style="float:right">March 05, 2021</span>
        </CardFooter>
    </TelerikCard>
</div>

<p>Card click event: @Test</p>

@code {
    private string Test { get; set; } = string.Empty;

    private void OnCardClick()
    {
        Test = DateTime.Now.ToLongTimeString();
    }
}

I can confirm that we have no intention of adding an OnClick event to the whole Card.

Lorenzo
Top achievements
Rank 1
Iron
commented on 05 Sep 2024, 10:04 AM

Hi,

sorry to jump in but I've tried incapsulating the TelerikCard inside a div with @onclick but nothing seems to happen.

Also, I noticed from the code posted above that the onclick event for the div is assigned directly to the EventCallback, shouldn't it instead be assigned to a method that then Invokes that EventCallback?

In my speficic case, what I am trying to accomplish is to create a clickable container that also contains other buttons inside, and at the moment is rendered using a TelerikCard.

Would this approach work? Or there is another way to do this?

Best Regards,

Lorenzo

Dimo
Telerik team
commented on 05 Sep 2024, 11:47 AM

@Lorenzo - the existing example was designed to work as a custom .razor component. I simplified it and now it should look the way you expect it to. If it doesn't work on your side, make sure the .razor file is interactive.
Lorenzo
Top achievements
Rank 1
Iron
commented on 05 Sep 2024, 12:43 PM

Thanks Dimo for changing the code, however what I'm trying to accomplish here is exactly what you mentioned: having a single component that represent the clickable container (rendered using TelerikCard) that caintains in itself also different action buttons, so the user can choose to click the container only or the action button only.

Should this work for InteractiveServer mode in .net8?

 
Dimo
Telerik team
commented on 05 Sep 2024, 12:59 PM

@Lorenzo - to distinguish clicks on the Card container and on nested clickable elements, you need to wrap the latter in containers with @onclick:stopPropagation.

In general, such nested clickability is a bit tricky from UX perspective.

And yes, this should work with InteractiveServer mode in .NET 8.

Lorenzo
Top achievements
Rank 1
Iron
commented on 06 Sep 2024, 08:09 AM | edited

Thanks Dimo, I've tried but the event does not get triggered. Below a part of my code, inserted inside a Instance.razor component (with OnCardClick function in Instance.razor.cs):

<div @onclick="@OnCardClick"> <TelerikCard> <CardBody> [...] </CardBody> </TelerikCard> </div>

Lorenzo
Top achievements
Rank 1
Iron
commented on 06 Sep 2024, 09:15 AM | edited

I've solved this incorporating a card inside a TelerikButton and it works properly.

Thanks anyway for your precious help,

Lorenzo

 

P.s. for anyone that might be reading this: the root cause of my issue with div @onclick was that Microsoft.AspNetCore.Components.Web, although being in _Imports.razor, wasn't correctly loaded by the component, so adding a using statement on top of the page made everything work correctly.

Tags
Card
Asked by
Igor
Top achievements
Rank 1
Answers by
Svetoslav Dimitrov
Telerik team
Share this question
or