function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Agnibha Chakrabarti 10Agnibha Chakrabarti 10 

how to call an onclick method when click on a data in lightning data table LWC

Hi, i have created following data table
User-added image
column of data table are
const columns = [
    { label: 'Label', fieldName: 'name', type:'url',typeAttributes: {label: { fieldName: 'name' }, 
        target: '_blank'}, wrapText: true   },
    { label: 'Website', fieldName: 'website', type: 'url' },
    { label: 'Phone', fieldName: 'phone', type: 'phone' },
    { label: 'Balance', fieldName: 'amount', type: 'currency' },
    { label: 'CloseAt', fieldName: 'closeAt', type: 'date' },
];
My requirement is............ When i'll click on the name......it will do some Onclick action

Thanks
MagulanDuraipandianMagulanDuraipandian
You have to use rowActions. For example check - https://www.infallibletechie.com/2020/03/lightning-datatable-with-buttonsrow.html
Agnibha Chakrabarti 10Agnibha Chakrabarti 10
that is not working
 
Suraj Tripathi 47Suraj Tripathi 47
Hii Agnibha Chakrabarti ,

You can use onrowaction for function calling as like onclick.
below have the example .
<template>
    <div style="height: 300px;">
        <lightning-datatable
                key-field="id"
                data={data}
                columns={columns}
                onrowaction={viewRecord}/>>
        </lightning-datatable>
    </div>    
</template>
Please mark it as best answer if it helps you to fix the issue.

Thank you!

Regards,
Suraj Tripathi.
 
Fatima ParveenFatima Parveen
Hi Everyone,

If you are having these kind of requirements, then you can use onrowaction in the lightning datatable.
for example,
User-added image

Also, in the Table header you have to add the clickable element such as button; if in case you don't want to display button in the table just the text, then you can have button with the base variant.
for example,
User-added image

onrowaction : viewRecord, you can have you row details using event.detail.row whenever button will be clicked.
This will cover your requirement. Hope this help you out.
Please mark it as best answer if it helps you to fix the issue.