You need to sign in to do that
Don't have an account?
Asish Behera 14
Pre select rows programmatically in LWC
Hi All,
I am trying to preselect rows in lighting data tabel, but it is not working out. I could see the value in console but table row is not selected. Any help would be highly apprecaited.
Here is my code.
Java Script code
import { LightningElement,api,wire,track} from 'lwc';
import getAssociatedFacility from '@salesforce/apex/AssignFacilityLWCController.getAssociatedFacility';
import createOpportunuity from '@salesforce/apex/AssignFacilityLWCController.createOpportunuity';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
const columns = [
{ label: 'Facility', fieldName: 'Facility' },
{ label: 'Admission Director', fieldName: 'AdmissionDirector', type: 'text' },
{ label: 'Phone', fieldName: 'Phone', type: 'phone' },
];
export default class AssignFacilityLWCModal extends LightningElement {
@api recordId;
@api accountId;
@api leadName;
@track data;
@track columns = columns;
@track error;
@track preSelectedRows;
@wire(getAssociatedFacility,{sLeadId:'$recordId',sAccountId:'$accountId'})
getAssociatedFacilityWired({ error, data }) {
if (data) {
console.log('data-->',data);
// console.log('data:' + JSON.stringify(data));
let tempRecords = JSON.parse( JSON.stringify( data.assignWrapperList ) );
tempRecords = tempRecords.map( row => {
return { ...row, Facility: row.healthCenter.Signature_Healthcare_Center__r.Facility_Short_Name__c,
AdmissionDirector: row.healthCenter.Signature_Healthcare_Center__r.Admission_Director__r.Name,
Phone: row.healthCenter.Signature_Healthcare_Center__r.Phone__c };
});
this.data = tempRecords;
this.preSelectedRows = data.selectedIdSet; // data.selectedIdset alreasy set in apext controller
//this.preSelectedRows = ["a1d7A000000jZJhQAM"];
console.log('this.preSelectedRows length2-->',this.preSelectedRows.length); // it prints 1
console.log('this.preSelectedRows-->',this.preSelectedRows);
// this.data = data;
console.log(' this.data-->', this.data);
this.error = undefined;
} else if (error) {
this.error = error;
this.data = undefined;
}
}
}
Apex Controller
@AuraEnabled(cacheable=true)
public static AssignFacilityListWrapper getAssociatedFacility(Id sLeadId,Id sAccountId){
List<AssignFacilityWrapper> assignFacilitywrapperList = new List<AssignFacilityWrapper>();
AssignFacilityListWrapper listWrapObj = new AssignFacilityListWrapper();
AssignFacilityWrapper assignFacilityWrap;
set<Id> setOfFacilityIds = new set<Id>();
map<Id,Opportunity> map_FacilityId_oppy = new map<Id,Opportunity>();
for(Opportunity oppy :[select id,Facility__c from Opportunity where Lead__c = :sLeadId]){
map_FacilityId_oppy.put(oppy.Facility__c,oppy);
}
for(Associated_Healthcare_Center__c healthCenter: [SELECT id,Signature_Healthcare_Center__c,Signature_Healthcare_Center__r.phone__c,
Signature_Healthcare_Center__r.Facility_Short_Name__c,
Signature_Healthcare_Center__r.Admission_Director__r.Name
FROM Associated_Healthcare_Center__c
WHERE Account__c =:sAccountId
Order By Signature_Healthcare_Center__r.Facility_Short_Name__c]){
assignFacilityWrap = new AssignFacilityWrapper();
assignFacilityWrap.healthCenter = healthCenter;
if(map_FacilityId_oppy !=null && map_FacilityId_oppy.containsKey(healthCenter.Signature_Healthcare_Center__c)){
assignFacilityWrap.isSelected = true;
setOfFacilityIds.add(healthCenter.Signature_Healthcare_Center__c);
}
assignFacilitywrapperList.add(assignFacilityWrap);
}
listWrapObj.assignWrapperList = assignFacilitywrapperList;
listWrapObj.selectedIdSet = setOfFacilityIds;
system.debug('listWrapObj---'+listWrapObj);
//return assignFacilitywrapperList;
return listWrapObj;
}
public class AssignFacilityWrapper{
@AuraEnabled public boolean isSelected{get;set;}
@AuraEnabled public boolean isExisting{get;set;}
@AuraEnabled public Associated_Healthcare_Center__c healthCenter{get;set;}
public AssignFacilityWrapper(){
isSelected = false;
isExisting = false;
}
}
public class AssignFacilityListWrapper{
@AuraEnabled public List<AssignFacilityWrapper> assignWrapperList{get;set;}
@AuraEnabled public set<Id> selectedIdSet {get; set;}
AssignFacilityListWrapper(){}
}
Template code
<div style="height: 300px;">
<lightning-datatable
key-field="Signature_Healthcare_Center__c"
data={data}
columns={columns}
selected-rows={preSelectedRows}>
</lightning-datatable>
</div>
I am trying to preselect rows in lighting data tabel, but it is not working out. I could see the value in console but table row is not selected. Any help would be highly apprecaited.
Here is my code.
Java Script code
import { LightningElement,api,wire,track} from 'lwc';
import getAssociatedFacility from '@salesforce/apex/AssignFacilityLWCController.getAssociatedFacility';
import createOpportunuity from '@salesforce/apex/AssignFacilityLWCController.createOpportunuity';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
const columns = [
{ label: 'Facility', fieldName: 'Facility' },
{ label: 'Admission Director', fieldName: 'AdmissionDirector', type: 'text' },
{ label: 'Phone', fieldName: 'Phone', type: 'phone' },
];
export default class AssignFacilityLWCModal extends LightningElement {
@api recordId;
@api accountId;
@api leadName;
@track data;
@track columns = columns;
@track error;
@track preSelectedRows;
@wire(getAssociatedFacility,{sLeadId:'$recordId',sAccountId:'$accountId'})
getAssociatedFacilityWired({ error, data }) {
if (data) {
console.log('data-->',data);
// console.log('data:' + JSON.stringify(data));
let tempRecords = JSON.parse( JSON.stringify( data.assignWrapperList ) );
tempRecords = tempRecords.map( row => {
return { ...row, Facility: row.healthCenter.Signature_Healthcare_Center__r.Facility_Short_Name__c,
AdmissionDirector: row.healthCenter.Signature_Healthcare_Center__r.Admission_Director__r.Name,
Phone: row.healthCenter.Signature_Healthcare_Center__r.Phone__c };
});
this.data = tempRecords;
this.preSelectedRows = data.selectedIdSet; // data.selectedIdset alreasy set in apext controller
//this.preSelectedRows = ["a1d7A000000jZJhQAM"];
console.log('this.preSelectedRows length2-->',this.preSelectedRows.length); // it prints 1
console.log('this.preSelectedRows-->',this.preSelectedRows);
// this.data = data;
console.log(' this.data-->', this.data);
this.error = undefined;
} else if (error) {
this.error = error;
this.data = undefined;
}
}
}
Apex Controller
@AuraEnabled(cacheable=true)
public static AssignFacilityListWrapper getAssociatedFacility(Id sLeadId,Id sAccountId){
List<AssignFacilityWrapper> assignFacilitywrapperList = new List<AssignFacilityWrapper>();
AssignFacilityListWrapper listWrapObj = new AssignFacilityListWrapper();
AssignFacilityWrapper assignFacilityWrap;
set<Id> setOfFacilityIds = new set<Id>();
map<Id,Opportunity> map_FacilityId_oppy = new map<Id,Opportunity>();
for(Opportunity oppy :[select id,Facility__c from Opportunity where Lead__c = :sLeadId]){
map_FacilityId_oppy.put(oppy.Facility__c,oppy);
}
for(Associated_Healthcare_Center__c healthCenter: [SELECT id,Signature_Healthcare_Center__c,Signature_Healthcare_Center__r.phone__c,
Signature_Healthcare_Center__r.Facility_Short_Name__c,
Signature_Healthcare_Center__r.Admission_Director__r.Name
FROM Associated_Healthcare_Center__c
WHERE Account__c =:sAccountId
Order By Signature_Healthcare_Center__r.Facility_Short_Name__c]){
assignFacilityWrap = new AssignFacilityWrapper();
assignFacilityWrap.healthCenter = healthCenter;
if(map_FacilityId_oppy !=null && map_FacilityId_oppy.containsKey(healthCenter.Signature_Healthcare_Center__c)){
assignFacilityWrap.isSelected = true;
setOfFacilityIds.add(healthCenter.Signature_Healthcare_Center__c);
}
assignFacilitywrapperList.add(assignFacilityWrap);
}
listWrapObj.assignWrapperList = assignFacilitywrapperList;
listWrapObj.selectedIdSet = setOfFacilityIds;
system.debug('listWrapObj---'+listWrapObj);
//return assignFacilitywrapperList;
return listWrapObj;
}
public class AssignFacilityWrapper{
@AuraEnabled public boolean isSelected{get;set;}
@AuraEnabled public boolean isExisting{get;set;}
@AuraEnabled public Associated_Healthcare_Center__c healthCenter{get;set;}
public AssignFacilityWrapper(){
isSelected = false;
isExisting = false;
}
}
public class AssignFacilityListWrapper{
@AuraEnabled public List<AssignFacilityWrapper> assignWrapperList{get;set;}
@AuraEnabled public set<Id> selectedIdSet {get; set;}
AssignFacilityListWrapper(){}
}
Template code
<div style="height: 300px;">
<lightning-datatable
key-field="Signature_Healthcare_Center__c"
data={data}
columns={columns}
selected-rows={preSelectedRows}>
</lightning-datatable>
</div>