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
suri03081988suri03081988 

AURA Lightening Component : Getting issue on the functionality of auto select record check boxes functionality

Hi All,

I am trying to make the record check boxes as checked by default on <lightning:datatable.

please correct me if i am doing wrong in below code

AURA Lightening Component:
<aura:component implements="force:appHostable" access="global" controller="sampleDownload_Class" >
<!-- Handler - Init Method -->
    <aura:handler name="init" value="{!this}" action="{!c.Init}"/>
<aura:attribute name="QuoteIDFromVfPage" type="string" access="global"/>    
    <aura:attribute name="columns" type="List"/>
    <aura:attribute name="data" type="List"/>
    <aura:attribute name="selectedAttachments" type="List"  default="[]"/>
    
<aura:attribute name="preSelectedRows" type="List"/>
<lightning:card title="Generated Documents" >
        <div >
            <lightning:datatable
                                 columns="{! v.columns }"
                                 data="{! v.data}"
                                 keyField="id"                                
                                 onrowselection="{!c.checkboxAttachment}"
                                 selectedRows="{!v.preSelectedRows}"
                                 /> 
        </div>                 
    </lightning:card>  
</aura:component>

Controller JS:

({
    Init : function(component, event, helper) { 
        
        component.set('v.columns', [
            {label: 'Attachment Name', fieldName: 'Name', type: 'text'},
            {label: 'Proposal Name', fieldName: 'parent.name', type: 'text'},
                        
        ]);        
                
        var action = component.get('c.initMethod');
        action.setParams({
            qID : component.get("v.QuoteIDFromVfPage")                        
        });
        action.setCallback(this,function(response){            
            var state = response.getState();
            //alert('successe state is '+state);
            if(state == 'SUCCESS'){
               component.set('v.data', response.getReturnValue());                 
               var selected_rows = helper.getSomeRows(response.getReturnValue(), 3);
               component.set("v.preSelectedRows",selected_rows_mod);

                
            }
        });
        $A.enqueueAction(action);          
     },
})


Apex: Class :::

Public with sharing class sampleDownload_Class {

@AuraEnabled
Public Static List<Attachment> initMethod(String qID){
String currentRecordId = qID;
List<Attachment> attachedFiles = new List<Attachment>();

attachedFiles = [select Id, name, parent.name, parentId from Attachment where parentId =: currentRecordId order By LastModifiedDate DESC limit 50000];
if(attachedFiles.size() > 0)
return attachedFiles;
else
return null;
}
}    

Please help me on this.

Thanks inadvance 
 
mukesh guptamukesh gupta
Hi Suri,

First you need to debeug list of seleted Id's in controller and check it's correct format:-

console.log('selected_rows  === >>>> '+selected_rows ); 

or 

you can follow below url:-

https://gist.github.com/sumugapadman/b31488774df70e50c9f8bf64d3c9ac33


if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh
suri03081988suri03081988
Hi Mukesh,

Thanks for your responce 

i have followed the link which you provided. i have added a alert statement in helper.js
({
    getSomeRows : function(objs, cnt) {
        var selected_records = [];
        
        for(var i=0; i<cnt; i++){
            selected_records.push(objs[i].id);
            alert('===> attch id is '+objs[i].id);
        }
        
        return selected_records;
    }
})

Alert is showing "Undefined"

If i remove the alert message in Helper.JS, then i am getting below error in front end UI for this statement "var selected_rows = helper.getSomeRows(response.getReturnValue(), 3);"

This page has an error. You might just need to refresh it. Error in $A.getCallback() [Cannot read properties of undefined (reading 'id')] Callback failed: apex://sampleDownload_Class/ACTION$initMethod Failing descriptor: {markup://c:sampleDownload_Comp}