How can I add records to a lwc data table based on input, for example, I have a search input text on my component that looks for a custom object, and I need to add that specific custom object record to the data-table once the record is selected on the search input, how can I accomplish this considering that I already know what are the columns that I need to display on the table?
1 Answer
Q: Are your searched SObjects same SObject or not?
If you are searching the same SObject with different records, when you select a record, add your selected record's data to your data array.
Since you already know how data-table works. I will give you a simple code example:
columns = [
{ label: 'Id', fieldName: 'Id', type: 'text'},
{ label: 'Name', fieldName: 'Name', type: 'text'}
]
@track
records = [
{Id: 1, Name: record1}
]
// your input onchange event handler
onChange(e) {
var input = e.detail.value
// an @AuraEnable method in your apex
getRecordInfo({inputInfo: input}).then(result => {
// in your apex method you should return a SObject type
this.records.push(result)
}.catch(error => {
console.log(error)
})
)
}
If your SObjects are in diffent types, you should make sure their fields' names are the same.