0

I have a data table, where I'm showing Pricebook entries with other values using the wrapper. I need to show some rows preselected, but it seems to be not working.

Component:

    <aura:attribute name="selectedRows" type="Object"/>
    <aura:attribute name="data" type="Object"/>
    <aura:attribute name="columns" type="List"/>

<aura:handler name="init" value="{!this}" action="{!c.doInit}" />

        <div style="height: 600px; width:1200px;">
                                    
                                    <lightning:datatable
                                                         columns="{! v.columns }"
                                                         data="{! v.data }"
                                                         keyField="id"
                                                         onrowselection="{! c.updateSelectedText }"
                                                         selectedRows="{! v.selectedRows }"
                                                         />
                                </div>

Controller:

doInit : function(component, event, helper) {
        let selectedIds = ['01u4S000000oZIxQAM','01t4S000000M0rwQAC'];
        component.set("v.selectedRows",selectedIds);
        
        component.set('v.columns', [
            {label: 'Product name', fieldName: 'ProductName', type: 'text',minColumnWidth: 70},
            {label: 'Product Code', fieldName: 'ProductCode', type: 'text'},
            {label: 'List Price', fieldName: 'UnitPrice', type: 'text'},
            {label: 'Product Description', fieldName: 'ProductDescription', type: 'text'},
            {label: 'Product Family', fieldName: 'ProductFamily', type: 'text'}
        ]);

            var action = component.get("c.getPriceBookEntry");
            action.setParams({ pricebookId : component.get("v.selectedPriceBookId") });
            action.setCallback(this, function(a) {
                var state = a.getState();
                if (state === "SUCCESS"){
                    var rows = a.getReturnValue();
                    //var rows = response.getReturnValue();
                    for (var i = 0; i < rows.length; i++) {
                        var row = rows[i];
                        if (row.priceBook.Product2){
                            row.ProductName = row.priceBook.Product2.Name;
                            row.ProductDescription = row.priceBook.Product2.Description;
                            row.ProductFamily = row.priceBook.Product2.Family;
                            row.UnitPrice = row.priceBook.UnitPrice;
                            row.ProductCode = row.priceBook.ProductCode;
                            row.Required = true;
                            row.deleted = false;
                            row.heading = '';
                            row.id = row.priceBook.Id;
                        } 
                    }
                    component.set('v.data', rows);
                } 
            });
            $A.enqueueAction(action);
        
    },

Not sure what I'm doing wrong, but I can't see the rows preselected .

2
  • Selected rows is of type Object when it should be of type List. Not sure if that is the cause, but it can't help to try. May 18, 2021 at 6:39
  • As Javascript is case-sensitive and you name your Id field one time as id: accs.push({name: account.Name, id: account.Id}); and another time as Id: selected.push(account.Id); maybe setting all ids as Id should fix your problem
    – Saroj Bera
    May 20, 2021 at 15:57

1 Answer 1

0

Everything looks right here except, I can think of the sequencing of setting variables. Can you try this?

if (state === "SUCCESS"){
                        var rows = a.getReturnValue();
                        //var rows = response.getReturnValue();
                        for (var i = 0; i < rows.length; i++) {
                            var row = rows[i];
                            if (row.priceBook.Product2){
                                row.ProductName = row.priceBook.Product2.Name;
                                row.ProductDescription = row.priceBook.Product2.Description;
                                row.ProductFamily = row.priceBook.Product2.Family;
                                row.UnitPrice = row.priceBook.UnitPrice;
                                row.ProductCode = row.priceBook.ProductCode;
                                row.Required = true;
                                row.deleted = false;
                                row.heading = '';
                                row.id = row.priceBook.Id;
                            } 
                        }
                        component.set('v.data', rows);
    
                       let selectedIds = ['01u4S000000oZIxQAM','01t4S000000M0rwQAC'];
                       component.set("v.selectedRows",selectedIds);
                    } 

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .