• Asish Behera 14
  • NEWBIE
  • 0 Points
  • Member since 2022

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies
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>
Hi all,

in the documentation (https://developer.salesforce.com/docs/component-library/bundle/lightning-datatable/documentation) a way is documented to preselect rows programmatically.
For this reason you should use "selected-rows"-attribut on lightning-datatable.

DOCUMENTATION

The selected-rows attribute enables programmatic selection of rows, which is useful when you want to preselect rows.
<lightning-datatable
    columns={columns}
    data={data}
    key-field="id"
    selected-rows={selectedRows}>
</lightning-datatable>
<lightning-button
    label="Select"
    onclick={handleSelect}>
</lightning-button>

To select a row programmatically, pass in the row key-field value.
// Load data via init handler first
// then handle programmatic selection
handleSelect() {
   const rows = ['a'];
   this.selectedRows = rows;
}

My component looks (simplfied) the following way:
export default class MergeDuplicates extends LightningElement {
    @track preSelectedRows = ['0011x00000KOMiJAAX'];
    
    [...]
}

My html-file (simplified) looks the following way:
<lightning-datatable
       key-field="Id"
       data={data}
       columns={columns}
       onrowselection={handleSelected}
       selected-rows={preSelectedRows}
       is-loading={tableLoadingState}>
</lightning-datatable>

My purpose: Preselect the current account, because the lwc is placed on a lightning record page for account.

My problem: Nothing happens. No checkbox is active.

One of my first ideas was the key-field. I changed it from "id" (lowercase) to "Id" (uppercase) but that don't solve the problem.

I don't know any other simpler way to test the preselect of rows for datatable than my example above.

Any suggestions?