1

I have a lwc data-table which is to display the list of records retrieved from APEX Imperative callout. The table displays the data when the number of records retrieved is only 1. However when the number of records retrieved is more than 1 then the table is blank.

My Data Table is:

<div style="height:auto; width:auto">
      <lightning-datatable
         key-field="id"
         data={srvoutput}
         columns={outputcolumns}
         column-width-mode="fixed"
         hide-checkbox-column="true"
         >
      </lightning-datatable>
</div>

My JS Code is:

const inputcol = [
    { label: 'Location', fieldName: 'Location', type: 'text', initialwidth: 100},
    { label: 'Required', fieldName: 'Required', type: 'text', initialwidth: 100},
    { label: 'DataType', fieldName: 'DataType', type: 'text', initialwidth: 150},
    { label: 'Parameter Name', fieldName: 'ParameterName', type: 'text', initialwidth: 100 },
    { label: 'Name', fieldName: 'Name', type: 'text', initialwidth: 100},
    { label: 'Description', fieldName: 'Description', type: 'text', wrapText: true, initialwidth: 450 },
    { label: 'Resource Name', fieldName: 'ResourceName', type: 'text', initialwidth: 100},
];

export default class DetailedTiles extends LightningElement {
 @track srvinput = [];
    @track showtable = false;

//APEX Imperative Callout - triggered by another event
apexmethod({param1: value1, param2:value2})
.then(result=>{
this.srvinput = result;
this.showtable = true;

}

Unable to figure out the inconsistent behaviour. Appreciate help.

1
  • the data variable is different from JS, please add updated code, i.e srvoutput, JS and html code are different.
    – Sarvesh
    Commented Oct 7, 2020 at 13:19

1 Answer 1

1

Each key needs to be unique. Presumably, you're using the Id field from a record, so it should be:

     key-field="Id"

Note that this field name is case sensitive, which is likely why you're having the problem you're experiencing.

3
  • Hi @sfdcfox, if that was the case then the first one also shouldn't be shown , this works only when the there is only one record, of more than one record i can see it in console log but datatable is blank. I changed it to "Id" also tried using an external Id field still it is not showing up if the record count is more than one.
    – Satish
    Commented Oct 8, 2020 at 2:59
  • @Satish The idea behind one showing up but not more than one is because of duplicate keys causing errors, such as if you used the wrong field name. What does your Developer Console (F12) say? You should see an error presumably.
    – sfdcfox
    Commented Oct 8, 2020 at 3:11
  • thanks for your inputs and regret the delay in response. The keys were fine, and there was nothing wrong with the code atall. However the array formation was messed when multiple records were returned. I restructured the array and the above code worked fine.
    – Satish
    Commented Oct 26, 2020 at 5:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .