What am I doing wrong in below code ?
<template>
<lightning-card title="Search Student Here" icon-name="standard:product">
<div>
<lightning-input type="text" value={searchString} lable="Search Student"
onchange={handleSearch} placeholder="Student Name">
</lightning-input>
</div>
<div if:true={searchResults}>
<lightning-datatable data={searchResults}>
columns={colums}
key-field="id">
</lightning-datatable>
</div>
</lightning-card>
</template>
******** Js.
import { LightningElement,track,wire } from 'lwc';
import getStudents from '@salesforce/apex/SearchStudents.getStudents';
const colums=[
{lable:'id',
fieldName:'id',
type:'text'},
{lable:'Student Name',
fieldName:'Name',
type:'text'},
];
export default class GetStudents extends LightningElement {
@track searchString;
@track colums=colums;
@track searchResults;
handleSearch(event){
this.searchString = event.target.value;
/* eslint-disable no-console */
console.log('this.searchString'+JSON.stringify(this.searchString));
getStudents({studentName:this.searchString})
.then(result=>{
this.searchResults=result;
console.log('this.searchResults'+JSON.stringify(this.searchResults));
})
}
}
********* apex
public class SearchStudents { @Auraenabled(Cacheable = true) public static list<Student__c> getStudents(string studentName){ studentName = '%'+studentName+'%'; list<Student__c> studentLst = [select id,Phone_Number__c,Email__c,name,School__r.name from student__c where NAME LIKE :studentName]; return studentLst; } }
Hi Shaker,
You have a tinnnny typo there that's breaking everything!
Look closely:
<lightning-datatable data={searchResults}>
columns={colums}
key-field="id">
See the closing triangle bracket in the first line? Yeah, that's closing your lightning-datatable before you give it the columns or key-fields!
Try removing that closing bracket right after {searchResults}