I tried to display my array using lightning-datatable with the below code. However, I can see the table and overview of datatable but no data is displayed in the table. Please help me to identify my where the mistake was.
JS:
import { LightningElement,track,api } from 'lwc';
const columns = [
{label:'Name',fieldname:'name'},
{label:'Email',fieldname:'email'},
{label:'Phone',fieldname:'phone'}
];
export default class Form extends LightningElement {
@track name;
@track displayAll = false;
@track stu = {};
@track columns = columns;
@track nameList = [
{
Id : 1,
name : 'Sumo',
email : '[email protected]',
phone : 9778787
},
{
Id : 2,
name : 'Sumi',
email : '[email protected]',
phone : 9778788
},
{
Id : 3,
name : 'Asif',
email : '[email protected]',
phone : 9778787
}
];
displayNames(event)
{
this.displayAll = this.displayAll? false :true;
showToast(this,'info','List of Records');
}
}
HTML:
<template>
<lightning-card title="Search,Delete, and Display">
<div class="slds-medium">
<lightning-input label="Name" data-inputname="name" type="text" value={stu.name} onchange={getName}></lightning-input><br/>
<lightning-input label="Email" data-inputname="email" type="text" value={stu.email} onchange={getName}></lightning-input><br/>
<lightning-input label="Phone" data-inputname="phone" type="number" value={stu.phone} onchange={getName}></lightning-input>
</div>
<div>
<lightning-button-group>
<lightning-button label="Display" onclick={displayNames}></lightning-button>
</lightning-button-group>
</div>
<template if:true={displayAll}>
<lightning-datatable columns={columns} data={nameList} key-field="Id"></lightning-datatable>
</template>
</lightning-card>
</template>