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
Sambit Kumar Panda 4Sambit Kumar Panda 4 

Lwc data table

I am trying to create a custom lookup search in lwc and after selecting the record from the lookup I have to click an add button so that it would be added to a lwc datatable. But my problem is when I am clicking the add button only the data from current searc is coming to the table removing the previously added data.
Best Answer chosen by Sambit Kumar Panda 4
Danish HodaDanish Hoda
Hi Sambit,
To add more data into accountList to diplay it in the dataTable, you need to initialize it like:

@track accountList = [];

and push the data into this array, like below:
accountList.push(result);

All Answers

Danish HodaDanish Hoda
Hi Sambit,
Please post your code here.
Sambit Kumar Panda 4Sambit Kumar Panda 4
<template>
    
    <c-lookup-test onselected={handleSelect} ></c-lookup-test>
    <div if:true = {dataFlag}>
        <lightning-datatable data={accountList} columns={columns} key-field="Id">
        </lightning-datatable>
    </div>
    
</template>
----------Data table code mentioned above, here I am getting the selected data from the lookup through an event

import { LightningElement ,api,wire,track} from 'lwc';
import searchAccounts from '@salesforce/apex/AccountCont.searchAccount';
import ACCOUNT_OBJECT from '@salesforce/schema/Account';
import NAME_FIELD from '@salesforce/schema/Account.Name';
export default class Datatabletr1 extends LightningElement {
    @track columns = [{
            label: 'Account name',
            fieldName: 'Name',
            type: 'text',
            sortable: true,
            typeAttributes: {label: { fieldName: 'Name' }, target: '_blank'}
        }
    ];
    @track error;
    //@track data = [];
    @track retrievedData ;
    @track dataFlag = false;
    @track accountList;
    @track noRecordsFound = true;
    handleSelect(event) {
        console.log('onselect event');
        this.retrievedData = event.detail;
        console.log('retrievedData '+ this.retrievedData.selectName);
        
        if(this.retrievedData.selectName) {
            var accName = this.retrievedData.selectName;
            searchAccounts ( {accName}) 
            .then(result => {
                this.accountList = result;
                console.log('list '+this.accountList);
                this.noRecordsFound = false;
            })
        } else {
            this.accountList = undefined;
            this.noRecordsFound = true;
        }
        
        this.dataFlag = true;
    }  
}
--------------this is the js
Danish HodaDanish Hoda
Hi Sambit,
To add more data into accountList to diplay it in the dataTable, you need to initialize it like:

@track accountList = [];

and push the data into this array, like below:
accountList.push(result);
This was selected as the best answer
Sambit Kumar Panda 4Sambit Kumar Panda 4
I am getting a list [Object Object]....any suggestions how to retrieve data from there?
 
User 444User 444
Hi Sambit Kumar, Can you please give me the code if possible, I want to display records on datatable which are only related to custom lookup record selected . Custom lookup is taken from reusable component.