Skip to main content
Shaker Shaikh (Cognizant) ha fatto una domanda in #Sales Cloud

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;              } }

 

I am new for LWC, I am trying to create a search bar on app page for custom object. It gives me data in console but its not showing me on screen.

 

#Sales Cloud

1 risposta
  1. 8 lug 2021, 13:38

    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}

0/9000