You need to sign in to do that
Don't have an account?
ANITHA BEEMU
Hi can anyone help on this..i created a lightning component to show in tree grid format all the parents and related childs and their related childs and vice versa...bin log its showing but in UI, its not showing.. any help please
Hi can anyone help on this..i created a lightning component to show in tree grid format all the parents and related childs and their related childs and vice versa...bin log its showing but in UI, its not showing.. any help please
here is my component:
<aura:component controller="Test_SubProducts" implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >
<aura:attribute name="gridColumns" type="List"/>
<aura:attribute name="gridData" type="Object"/>
<aura:attribute name="sku" type="string"/>
<aura:handler name="init" value="{!this}" action="{!c.doInit}" />
<!--<lightning:accordion aura:id="accordion" activeSectionName="B">
<lightning:accordionSection name="B" label="Sub Components">-->
<lightning:treeGrid aura:id="subProdTreegridID"
columns="{!v.gridColumns }"
data="{!v.gridData }"
keyField="KeyField"
/>
<!--</lightning:accordionSection>
</lightning:accordion>-->
</aura:component>
my controller:
({
doInit : function(cmp, event, helper){
cmp.set('v.gridColumns',[
{label:'Product Name', fieldName:'productName', type:'text'},
{label:'Product Code', fieldName:'productCode', type:'text'},
]);
var skuVal = cmp.get('v.sku');
var action = cmp.get('c.getRelatedProducts');
action.setParams({sku:skuVal});
action.setCallback(this,function(response){
var state = response.getState();
console.log('**state-->'+state);
if(state === "SUCCESS"){
var data = response.getReturnValue();
console.log('**data-->'+data);
JSON.parse(response.getReturnValue());
var temojson = JSON.parse(JSON.stringify(data).split('items').join('_children'));
console.log('**temojson-->'+temojson);
cmp.set('v.gridData',temojson);
console.log('**gridData-->'+cmp.get('v.gridData'));
}
});
$A.enqueueAction(action);
}
})
my Apex code:
public class Test_SubProducts {
static map<Id,list<Id>> productIdsMap = new map<Id,list<Id>>();
static list<Id> parentProductIds;
static list<Id> childProductIds;
//get the products in Tree.
@AuraEnabled
public static string getRelatedProducts(string sku){
list<Product_Relationships__c> prodRelaList = new list<Product_Relationships__c>();
list<Id> produIds;
map<Id,Product2> productMap;
prodRelaList = [Select Parent_Sku__c,Child_Sku__c from Product_Relationships__c where Parent_Sku__r.ProductCode =: sku];
return JSON.serializePretty(getHeirarchy(prodRelaList));
}
static list<Product_Relationships__c> getProductRelationShip(list<Id> productIds){
return [Select Parent_Sku__c,Child_Sku__c from Product_Relationships__c where Parent_Sku__c IN: productIds];
}
//get the parent and child products ids
static void getProductIds(list<Product_Relationships__c> prodRelaList){
if(prodRelaList != null && !prodRelaList.isEmpty()){
productIdsMap = new map<Id,list<Id>>();
parentProductIds = new list<Id>();
childProductIds = new list<Id>();
for(Product_Relationships__c prodRela: prodRelaList){
if(prodRela.Parent_Sku__c != null){
parentProductIds.add(prodRela.Parent_Sku__c);
if(prodRela.Child_Sku__c != null){
childProductIds.add(prodRela.Child_Sku__c);
if(productIdsMap.containsKey(prodRela.Parent_Sku__c)){
productIdsMap.get(prodRela.Parent_Sku__c).add(prodRela.Child_Sku__c);
}else{
productIdsMap.put(prodRela.Parent_Sku__c,new list<Id>{prodRela.Child_Sku__c});
}
}
}
}//end of for
}
}
static map<Id,Product2> getProducts(set<Id> productsIds){
return new Map<Id,Product2>([Select Id, Name, ProductCode, Description from Product2 where ID IN: productsIds]);
}
static list<ProductsWrapper> getHeirarchy(List<Product_Relationships__c> topProds){
list<ProductsWrapper> prodWrapList = new list<ProductsWrapper>();
if(topProds != null && !topProds.isEmpty()){
getProductIds(topProds);
if(parentProductIds != null && !parentProductIds.isEmpty()){
set<Id> productIds = new set<Id>();
productIds.addAll(parentProductIds);
if(childProductIds != null && !childProductIds.isEmpty()){
productIds.addAll(childProductIds);
}//end of if childproductids check.
map<Id,Product2> prodMap = new map<Id,Product2>();
prodMap = getProducts(productIds);
for(Id prodId: parentProductIds){
ProductsWrapper prodWrapInst = new ProductsWrapper();
prodWrapInst.keyField = prodId;
prodWrapInst.productName = prodMap.get(prodId).Name;
prodWrapInst.productCode = prodMap.get(prodId).ProductCode;
list<Items> level1Prods = new list<Items>();
if(productIdsMap.containsKey(prodId) && productIdsMap.get(prodId) != null){
for(Id childProdId: productIdsMap.get(prodId)){
Items level1Child = new Items();
level1Child.keyField = childProdId;
level1Child.productName = prodMap.get(childProdId).Name;
level1Child.productCode = prodMap.get(childProdId).ProductCode;
level1Prods.add(level1Child);
}
}//end of if prodidmap check.
prodWrapInst.items = level1Prods;
prodWrapList.add(prodWrapInst);
}//end of for parentids.
}//end of if parentproductids check.
}//end of if topproduts check.
return prodWrapList;
}
//Wrapper Class for Parent Products and Child Products.
public Class ProductsWrapper{
@AuraEnabled
public String KeyField {get;set;}
@AuraEnabled
public String productName {get;set;}
@AuraEnabled
public String productCode {get;set;}
@AuraEnabled
public List<Items> items {get;set;}
}
//Wrapper Class for Child Products and Child Products.
public Class Items{
@AuraEnabled
public String KeyField {get;set;}
@AuraEnabled
public String productName {get;set;}
@AuraEnabled
public String productCode {get;set;}
}
}
here is my component:
<aura:component controller="Test_SubProducts" implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >
<aura:attribute name="gridColumns" type="List"/>
<aura:attribute name="gridData" type="Object"/>
<aura:attribute name="sku" type="string"/>
<aura:handler name="init" value="{!this}" action="{!c.doInit}" />
<!--<lightning:accordion aura:id="accordion" activeSectionName="B">
<lightning:accordionSection name="B" label="Sub Components">-->
<lightning:treeGrid aura:id="subProdTreegridID"
columns="{!v.gridColumns }"
data="{!v.gridData }"
keyField="KeyField"
/>
<!--</lightning:accordionSection>
</lightning:accordion>-->
</aura:component>
my controller:
({
doInit : function(cmp, event, helper){
cmp.set('v.gridColumns',[
{label:'Product Name', fieldName:'productName', type:'text'},
{label:'Product Code', fieldName:'productCode', type:'text'},
]);
var skuVal = cmp.get('v.sku');
var action = cmp.get('c.getRelatedProducts');
action.setParams({sku:skuVal});
action.setCallback(this,function(response){
var state = response.getState();
console.log('**state-->'+state);
if(state === "SUCCESS"){
var data = response.getReturnValue();
console.log('**data-->'+data);
JSON.parse(response.getReturnValue());
var temojson = JSON.parse(JSON.stringify(data).split('items').join('_children'));
console.log('**temojson-->'+temojson);
cmp.set('v.gridData',temojson);
console.log('**gridData-->'+cmp.get('v.gridData'));
}
});
$A.enqueueAction(action);
}
})
my Apex code:
public class Test_SubProducts {
static map<Id,list<Id>> productIdsMap = new map<Id,list<Id>>();
static list<Id> parentProductIds;
static list<Id> childProductIds;
//get the products in Tree.
@AuraEnabled
public static string getRelatedProducts(string sku){
list<Product_Relationships__c> prodRelaList = new list<Product_Relationships__c>();
list<Id> produIds;
map<Id,Product2> productMap;
prodRelaList = [Select Parent_Sku__c,Child_Sku__c from Product_Relationships__c where Parent_Sku__r.ProductCode =: sku];
return JSON.serializePretty(getHeirarchy(prodRelaList));
}
static list<Product_Relationships__c> getProductRelationShip(list<Id> productIds){
return [Select Parent_Sku__c,Child_Sku__c from Product_Relationships__c where Parent_Sku__c IN: productIds];
}
//get the parent and child products ids
static void getProductIds(list<Product_Relationships__c> prodRelaList){
if(prodRelaList != null && !prodRelaList.isEmpty()){
productIdsMap = new map<Id,list<Id>>();
parentProductIds = new list<Id>();
childProductIds = new list<Id>();
for(Product_Relationships__c prodRela: prodRelaList){
if(prodRela.Parent_Sku__c != null){
parentProductIds.add(prodRela.Parent_Sku__c);
if(prodRela.Child_Sku__c != null){
childProductIds.add(prodRela.Child_Sku__c);
if(productIdsMap.containsKey(prodRela.Parent_Sku__c)){
productIdsMap.get(prodRela.Parent_Sku__c).add(prodRela.Child_Sku__c);
}else{
productIdsMap.put(prodRela.Parent_Sku__c,new list<Id>{prodRela.Child_Sku__c});
}
}
}
}//end of for
}
}
static map<Id,Product2> getProducts(set<Id> productsIds){
return new Map<Id,Product2>([Select Id, Name, ProductCode, Description from Product2 where ID IN: productsIds]);
}
static list<ProductsWrapper> getHeirarchy(List<Product_Relationships__c> topProds){
list<ProductsWrapper> prodWrapList = new list<ProductsWrapper>();
if(topProds != null && !topProds.isEmpty()){
getProductIds(topProds);
if(parentProductIds != null && !parentProductIds.isEmpty()){
set<Id> productIds = new set<Id>();
productIds.addAll(parentProductIds);
if(childProductIds != null && !childProductIds.isEmpty()){
productIds.addAll(childProductIds);
}//end of if childproductids check.
map<Id,Product2> prodMap = new map<Id,Product2>();
prodMap = getProducts(productIds);
for(Id prodId: parentProductIds){
ProductsWrapper prodWrapInst = new ProductsWrapper();
prodWrapInst.keyField = prodId;
prodWrapInst.productName = prodMap.get(prodId).Name;
prodWrapInst.productCode = prodMap.get(prodId).ProductCode;
list<Items> level1Prods = new list<Items>();
if(productIdsMap.containsKey(prodId) && productIdsMap.get(prodId) != null){
for(Id childProdId: productIdsMap.get(prodId)){
Items level1Child = new Items();
level1Child.keyField = childProdId;
level1Child.productName = prodMap.get(childProdId).Name;
level1Child.productCode = prodMap.get(childProdId).ProductCode;
level1Prods.add(level1Child);
}
}//end of if prodidmap check.
prodWrapInst.items = level1Prods;
prodWrapList.add(prodWrapInst);
}//end of for parentids.
}//end of if parentproductids check.
}//end of if topproduts check.
return prodWrapList;
}
//Wrapper Class for Parent Products and Child Products.
public Class ProductsWrapper{
@AuraEnabled
public String KeyField {get;set;}
@AuraEnabled
public String productName {get;set;}
@AuraEnabled
public String productCode {get;set;}
@AuraEnabled
public List<Items> items {get;set;}
}
//Wrapper Class for Child Products and Child Products.
public Class Items{
@AuraEnabled
public String KeyField {get;set;}
@AuraEnabled
public String productName {get;set;}
@AuraEnabled
public String productCode {get;set;}
}
}
Controller
Example is taken from the documentation
Let me know if this helps