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
rohan patel 30rohan patel 30 

lwc data is not visible in datatable :

User-added imagehtml code:

<template>
    <lightning-card title="imprative Demo">
    <div style="height: 300px;">
        <lightning-datatable
                key-field="id"
                data={data}
                columns={columns}>
        </lightning-datatable>
    </div>
</lightning-card>
</template>

js code :

import { LightningElement ,track} from 'lwc';
import getcontacts from '@salesforce/apex/mperativeDemo.getcontacts';
const columns=[
    
    {label: 'record id', fieldname: 'Id'},
    {label:"contact Name" ,fieldname:'Name'},
];

export default class ImperativeDemo extends LightningElement {

@track columns=columns;
@track data=[];
connectedCallback(){
    getcontacts()
    .then(result=>{   //then is a promise in js
        this.data=result;
    })
    .catch(error=>{
        console.log("error occured");
    })
}

}

apex class:

public class mperativeDemo {
    @AuraEnabled
    public static list<contact> getcontacts(){
        list<contact> getlist=[select id,name from contact];
     
        return getlist;
    }

}

 
AnkaiahAnkaiah (Salesforce Developers) 
Hi Rohan,

Refer the below link and modify your code.
https://www.sfdcpoint.com/salesforce/lightning-web-component-lightning-datatable/

Still facing issues let me know will help you with code.

Thanks!!
Hitesh chaudhariHitesh chaudhari
<template>
    <lightning-card title="imprative Demo">
    <div style="height: 300px;">
        <lightning-datatable
                key-field="id"
                data={data}
                columns={columns}>
        </lightning-datatable>
    </div>
</lightning-card>
</template>

js code :

import { LightningElement ,track} from 'lwc';
import getcontacts from '@salesforce/apex/mperativeDemo.getcontacts';
const columns=[
    
    {label: 'record id', fieldname: 'id'},
    {label:"contact Name" ,fieldname:'name'},

];

export default class ImperativeDemo extends LightningElement {

@track columns=columns;
@track data=[];
connectedCallback(){
    getcontacts()
    .then(result=>{   //then is a promise in js
        this.data=result;
    })
    .catch(error=>{
        console.log("error occured");
    })
}

}

apex class:

public class mperativeDemo {
    @AuraEnabled
    public static list<contact> getcontacts(){
        list<contact> getlist=[select id,name from contact];
     
        return getlist;
    }

}