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
VSK98VSK98 

Unable to display the data in Lightning:treeGrid

Hi All,

I am unable to display the records using Lightning:treeGrid. The data has passing from apex controller to JS Controller but data not displayed.
Was anything missed over there?

LightningComponent:
 
<aura:component implements="force:appHostable" 
                controller="Nested_JSONController">
 
    
    <aura:attribute name="gridColumns" type="List" />
    <aura:attribute name="gridData" type="Object" />

    
    <aura:handler name="init" value="{!this}" action="{!c.onLoad}"/>
    
    <lightning:treeGrid columns="{! v.gridColumns }"
        data="{! v.gridData }"
        keyField="Id"
        aura:id="mytree"
    />
        

</aura:component>

JS Controller:
({
    onLoad : function(component, event, helper) {
        var columns = [
            {
                type: 'double',
                fieldName: 'totalPrice',
                label: 'totalPrice'
            },
            {
                type: 'datetime',
                fieldName: 'statementDate',
                label: 'statementDate'
            },
            {
                type: 'double',
                fieldName: 'unitPrice',
                label: 'unitPrice'
            },
            {
                type: 'double',
                fieldName: 'quantity',
                label: 'quantity'
            },
            
        ];
        component.set('v.gridColumns', columns);
        var action = component.get("c.Nested_JSON");
        action.setCallback(this, function(response){
            var state = response.getState();
            if ( state === "SUCCESS" ) {
            alert(state);
                var data = response.getReturnValue();
               console.log(data);
                component.set('v.gridData', data);
            }
        });
        $A.enqueueAction(action);
    }

})

Apex Classes:
 
public class InvoiceWrapper_L {
    public class LineItem {
        @AuraEnabled
        public Double unitPrice {get; set;}
        @AuraEnabled
        public Double quantity {get; set;}
        @AuraEnabled
        public String productName {get; set;}

        public Double getLineItemTotal() {
            return this.unitPrice * this.quantity;
        }
    }

    public class Invoice {
        @AuraEnabled
        public Double totalPrice {get; set;}
        @AuraEnabled
        public DateTime statementDate {get; set;}
        @AuraEnabled
        public String contactnumber {get; set;}
        @AuraEnabled
        public List<LineItem> lineItems {get; set;}
        @AuraEnabled
        public Integer invoiceNumber {get; set;}
    }
    @AuraEnabled
    public List<Invoice> invoiceList {get; set;}
}
 
public class Nested_JSONController {
   
    @AuraEnabled
    public static InvoiceWrapper_L Nested_JSON() {
     
    InvoiceWrapper_L wrapper ;
        Http h = new Http();
        HttpRequest request = new HttpRequest();
        request.setEndPoint('https://docsample.herokuapp.com/jsonSample');
        request.setHeader('Content-type', 'application/json');
        request.setMethod('GET');

        HttpResponse response = h.send(request);

        wrapper = (InvoiceWrapper_L) JSON.deserializeStrict(response.getBody(), InvoiceWrapper_L.class);
        system.debug('@@@@@'+wrapper);
        return wrapper;
    }
}

Regards,
VSK98
Raj VakatiRaj Vakati
Change your code as below .. Problem is wit you nested inner class 

 
public class Nested_JSONController {
   
    @AuraEnabled
    public static List<Invoice> Nested_JSON() {
     
    List<Invoice> wrapper ;
        Http h = new Http();
        HttpRequest request = new HttpRequest();
        request.setEndPoint('https://docsample.herokuapp.com/jsonSample');
        request.setHeader('Content-type', 'application/json');
        request.setMethod('GET');

        HttpResponse response = h.send(request);

        wrapper = (List<Invoice>) JSON.deserializeStrict(response.getBody(), List<Invoice>.class);
        system.debug('@@@@@'+wrapper);
        return wrapper;
    }

	public class LineItem {
        @AuraEnabled
        public Double unitPrice {get; set;}
        @AuraEnabled
        public Double quantity {get; set;}
        @AuraEnabled
        public String productName {get; set;}
		@AuraEnabled
        public Double getLineItemTotal() {
            return this.unitPrice * this.quantity;
        }
    }

    public class Invoice {
        @AuraEnabled
        public Double totalPrice {get; set;}
        @AuraEnabled
        public DateTime statementDate {get; set;}
        @AuraEnabled
        public String contactnumber {get; set;}
        @AuraEnabled
        public List<LineItem> lineItems {get; set;}
        @AuraEnabled
        public Integer invoiceNumber {get; set;}
    }
   
}
	
	
	}


 
VSK98VSK98
Hi Raj,

Thanks for your response !!
I have changed the code as you said above. Still, the issue persists.
Class.System.JSON.deserializeStrict: line 19, column 1
Class.Nested_JSONController.Nested_JSON: line 15, column 1
11:07:13.0 (966173433)|FATAL_ERROR|System.JSONException: Malformed JSON: Expected '[' at the beginning of List/Set
Regards,
VSK98
 
Raj VakatiRaj Vakati
Try this code .. from the JSON responce you  are not getting  the tree Grid sort of data . if you want to show the treegrod you need to use the custom logic that will support treegrid data 


Refer this link

https://rajvakati.com/2018/04/15/usage-of-lightningtreegrid/
public class Nested_JSONController {
    
    @AuraEnabled
    public static List<JSON2Apex.InvoiceList> Nested_JSON() {
        Http h = new Http();
        HttpRequest request = new HttpRequest();
        request.setEndPoint('https://docsample.herokuapp.com/jsonSample');
        request.setHeader('Content-type', 'application/json');
        request.setMethod('GET');
        HttpResponse response = h.send(request);
        JSON2Apex cls = JSON2Apex.parse(response.getBody());
        // wrapper = (FinalRes) JSON.deserializeStrict(response.getBody(), FinalRes.class);
        System.debug('aaaaaaaa'+cls.invoiceList);
        return cls.invoiceList;
    }
    
}
 
public class JSON2Apex {
    
    public class LineItems {
        @AuraEnabled
        public Double UnitPrice{get; set;}
        @AuraEnabled
        public Double Quantity{get; set;}
        @AuraEnabled
        public String ProductName{get; set;}
    }
    @AuraEnabled
    public List<InvoiceList> invoiceList{get; set;}
    public class InvoiceList {
        @AuraEnabled
        public Double totalPrice{get; set;}
        @AuraEnabled
        public String statementDate{get; set;}
        @AuraEnabled
        public List<LineItems> lineItems{get; set;}
        @AuraEnabled
        public Integer invoiceNumber{get; set;}
    }
    
    
    public static JSON2Apex parse(String json) {
        return (JSON2Apex) System.JSON.deserialize(json, JSON2Apex.class);
    }
}