I am very new to LWC and Javascript. I have an LWC component with a search bar and a table (created in HTML file) showing the search result. I want to add another column in the table with a button on each row which allows me to add that whole row into another table in other component.
This is my HTML file.
' <template>
<lightning-card title="Account Search Bar" icon-name="standard:account">
<lightning-input type="search" onchange={handleKeyChange} class="slds-m-bottom_small" label="Search"
value={accountName}>
</lightning-input>
<table id="table">
<thead>
<tr>
<th class="" scope="col">
<div class="slds-truncate" title="Account Name">Name</div>
</th>
<th class="" scope="col">
<div class="slds-truncate" title="Account Phone">Phone</div>
</th>
<th class="" scope="col">
<div class="slds-truncate" title="Select Account">Select Account</div>
</th>
</tr>
</thead>
<tbody>
<template for:each={accountList} for:item="accountObj" for:index="index">
<tr class="table" key={accountObj.Id}>
<td class="name">{accountObj.Name}</td>
<td class="phone">{accountObj.Phone}</td>
<td class="addButton"><input type="submit" value="Add" onclick={addField}></td>
</tr>
</template>
</tbody>
</table>
</lightning-card>
'
I have added the buttons nd I am trying to get the data of the row from which the button is clicked. But I am getting 'undefined' in every object.
This is my js file.
import { LightningElement,wire,track } from 'lwc';
import getAccounts from '@salesforce/apex/AccountSearchCls.getAccounts';
const DELAY = 300;
export default class TestComponentSearch extends LightningElement {
accountName='';
@track accountList =[];
@track buttonId=1;
@wire(getAccounts,{actName:'$accountName'})
retrieveAccouts({error,data}){
if(data){
this.accountList = data;
}
else if(error){
}
}
handleKeyChange(event){
const searchString= event.target.value;
window.clearTimeout(this.delayTimeout);
this.delayTimeout = setTimeout(()=>{
this.accountName =searchString;
},DELAY);
}
addField()
{
console.log("In addField");
var table = document.getElementById('table');
selected = table.getElementsByClassName('tbody')[0];
var rows= selected.getelEmentsByTagName('tr');
}
}
I am not sure what is the problem or if this is the right way to do it. I would appreciate if anyone helps me in it.