How to get the selected record details in Lightning Webcomponents



public class AccountController {
    @AuraEnabled(cacheable=true)
    public static List<Account> displayAccounts(){
        return [select Id,Name,Site from Account LIMIT 2];
    }
}

<template>
	<table class="slds-table slds-table_cell-buffer slds-table_bordered">
		<tr>
			<td><b>Name</b></td>
			<td><b>Site</b></td>
			<td><b>Action</b></td>
		</tr>
		<template for:each={accounts} for:item="acc" for:index="index">
			<tr key={acc.Id}>
				<td>
					{acc.Name}
				</td>
				<td>
					{acc.Site}
				</td>
				<td>
					<lightning-button label="Select" data-index={index}  variant="brand" value={acc.Id} onclick={handleUpdate}>
					</lightning-button>
				</td>
			</tr>
		</template>
	</table>
</template>

import { LightningElement } from 'lwc';
import displayAccounts from '@salesforce/apex/AccountController.displayAccounts';
export default class UpdateAccount extends LightningElement {
    accounts;
    error;
    connectedCallback(){
        displayAccounts()
        .then(result=>{
            this.accounts=result;
            this.error=undefined;
        })
        .catch(error=>{
            this.error=error;
            this.accounts=undefined;
        }); 
    }
    handleUpdate(event){
        let index = event.target.dataset.index;
        alert('===slected record details==='+JSON.stringify(this.accounts[index].Name+this.accounts[index].Site));
    }
}

Comments