You need to sign in to do that
Don't have an account?
Sambit 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.
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
Please post your code here.
<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
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);