You need to sign in to do that
Don't have an account?
bharath kumar 52
Not able to get data from multiple rows in lightning component
Hi All,
I am trying to build a search component which takes multiple criteria so i am adding fields, criteria and the search string. When i try to add more than 1 criteria in my lightning component i am getting this error
"This page has an error. You might just need to refresh it. Action failed: c:FilterComponent$controller$searchProducts [Cannot read property 'get' of undefined] Failing descriptor: {c:FilterComponent$controller$searchProducts}"
Please help me fix this. I also tried checking if its an array or not but that doesn't work in my favour.
I am trying to build a search component which takes multiple criteria so i am adding fields, criteria and the search string. When i try to add more than 1 criteria in my lightning component i am getting this error
"This page has an error. You might just need to refresh it. Action failed: c:FilterComponent$controller$searchProducts [Cannot read property 'get' of undefined] Failing descriptor: {c:FilterComponent$controller$searchProducts}"
Please help me fix this. I also tried checking if its an array or not but that doesn't work in my favour.
Component : <aura:component controller="SearchFilterController" Implements="force:appHostable,flexipage:availableForRecordHome,force:hasRecordId"> <aura:handler name="init" value="{!this}" action="{!c.doInit}"/> <aura:attribute name="prodWrapper" type="list"/> <aura:attribute type="Object" name="testAttribute" /> <aura:attribute name="accountList" type="Account[]"/> <aura:attribute name="productFields" type="list"/> <aura:attribute name="criteriaType" type="list"/> <lightning:card> <div class="slds-m-around--xx-large"> <div class="slds-float_right slds-p-bottom_small"> <h1 class="slds-page-header__title">Add Row <lightning:buttonIcon iconName="utility:add" size="large" variant="bare" alternativeText="Add" onclick="{!c.addRow}"/> </h1> </div> <div class="container-fluid"> <table class="slds-table slds-table_bordered slds-table_cell-buffer"> <thead> <tr class="slds-text-title_caps"> <th scope="col"> <div class="slds-truncate">Sr. No</div> </th> <th scope="col"> <div class="slds-truncate" title="Field Name">Field Name</div> </th> <th scope="col"> <div class="slds-truncate" title="Criteria">Criteria</div> </th> <th scope="col"> <div class="slds-truncate" title="Value">Value</div> </th> <th scope="col"> <div class="slds-truncate" title="Action">Action</div> </th> </tr> </thead> <tbody> <aura:iteration items="{!v.prodWrapper}" var="acc" indexVar="index"> <!--{!v.prodWrapper.size} --> <tr> <td> {!'index'+index} </td> <td> <!--<lightning:input name="fieldName" type="text" maxlength="50" value="{!v.testAttribute.fieldNames}" /> {!index} --> <lightning:select aura:id="PicklistId" label="Select a field" name="fieldName" onchange="{c.changeHandler}" > <option value="" text="- None -" /> <aura:iteration items="{!v.productFields}" var="field"> <option value="{!field}" text="{!field}" /> </aura:iteration> </lightning:select> </td> <td> <!-- <lightning:input name="criteria" type="string" maxlength="30" value="{!acc.criteriaType}" /> --> <lightning:select aura:id="PicklistId2" label="Select a criteria" name="criteria" > <option value="" text="- None -" /> <aura:iteration items="{!v.criteriaType}" var="per"> <option value="{!per}" text="{!per}" /> </aura:iteration> </lightning:select> </td> <td> <lightning:input name="searchFieldValue" type="text" value="{!acc.searchStr}" /> </td> <td> <a onclick="{!c.removeRecord}" data-record="{!index}"> <lightning:icon iconName="utility:delete" size="small" alternativeText="Delete"/> <span class="slds-assistive-text">Delete</span> </a> </td> </tr> </aura:iteration> </tbody> </table> <div class="slds-align_absolute-center slds-p-top_small"> <lightning:button variant="brand" label="Submit" title="Brand action" onclick="{!c.searchProducts}" /> </div> </div> </div> </lightning:card> </aura:component> ================== JS Controller : ({ doInit:function(component, event, helper) { var action = component.get("c.displayFieldsAndCriteria"); action.setCallback(this, function(response) { //get response status var state = response.getState(); if (state === "SUCCESS") { //set empty account list var respValue=JSON.parse(response.getReturnValue()); component.set("v.testAttribute",respValue); //var testValue = component.set("v.testAttribute",response.getReturnValue()); var respVal =response.getReturnValue(); //console.log('respVal >>> '+respVal); console.log('v.testAttribute value >>>>'+respValue); console.log('criteriaType value >>>>'+respValue.criteriaType); console.log('searchStr value >>>>'+respValue.searchStr); // console.log('fieldNames value >>>>'+JSON.stringify(respValue.fieldNames)); //console.log('respValue.fieldNames typeof >>>> '+typeof(respValue.fieldNames)); var result=respValue.fieldNames; console.log('result stringified >>>>>> '+result.typeof); component.set("v.productFields",respValue.fieldNames); component.set("v.criteriaType",respValue.criteriaType); /*var criteriaTypeVar= component.get("v.criteriaType"); console.log('criteriaTypeVar >>>> '+criteriaTypeVar[0]); var productFieldsVar= component.get("v.productFields"); console.log('productFields >>>> '+productFieldsVar);*/ alert('data received from class'); } }); $A.enqueueAction(action); }, addRow: function(component, event, helper) { //get the List from component var pwList = component.get("v.prodWrapper"); //Add New criteria pwList.push({ 'fieldNames': '', 'criteriaType': '', 'searchStr': '' }); component.set("v.prodWrapper", pwList); }, removeRecord: function(component, event, helper) { var criteriaList = component.get("v.prodWrapper"); var selectedItem = event.currentTarget; //Get the selected item index var index = selectedItem.dataset.record; criteriaList.splice(index, 1); component.set("v.prodWrapper", criteriaList); }, changeHandler:function(component, event, helper){ var count=event.target.id; console.log('input id >>>> '+count); }, searchProducts: function(component, event, helper) { var a= component.get("v.prodWrapper"); console.log(a); var fName=component.find("PicklistId").get("v.value"); for(var i=0;i<a.length;i++){ console.log('fName >>>>'+ fName.length); a[i].fieldNames=fName; console.log('a value >>>> '+JSON.stringify(a)); } /*var b= component.find("PicklistId2").get("v.value"); var c= component.find("PicklistId").get("v.value");*/ // use compo.find.get for picklist fields and while setting parameters create a json string // send the created json string to server /*console.log('typeof c>> '+typeof(c) +' '+JSON.stringify(c)); console.log('typeof b >> '+typeof(b) +' '+JSON.stringify(b)); console.log('typeof a >> '+typeof(a) +' '+JSON.stringify(a));*/ var action = component.get("c.generateQuery"); action.setParams({ "prodWrapList": JSON.stringify(component.get("v.prodWrapper")) }); action.setCallback(this, function(response) { //get response status var state = response.getState(); if (state === "SUCCESS") { //set empty account list component.set("v.prodWrapper", []); alert('data sent to generateQuery'); } }); $A.enqueueAction(action); } })
try below code now it's working fine.I made some changes in your cmp and js file.
I hope above answer will help you. friend don't forget to mark it as best answer because I invested my much time over it.
Thank you
All Answers
I need ApexCtrl and if you have helper then plz share with us. Plz share whole code. we will try to fix it.
thank you
Hi Veer,
Below is the Apex controller. I haven't added any code to the helper js for now so the code must be good to go. Thanks again
try below code now it's working fine.I made some changes in your cmp and js file.
I hope above answer will help you. friend don't forget to mark it as best answer because I invested my much time over it.
Thank you
Hi Veer,
Firstly, thanks a ton for fixing it. While you were busy checking my code i also took a few less travelled roads to fix this. Just as an FYI i used the dynamic aura id generated with an index variable and tried to get it but it dint work for me. So, i took a last chance(although it's not a best practice) and tried the below and it also works. But i definitely admit that yours is the best answer my friend. Meanwhile, please have a look at my code(hack : D ) below. You can also checkout the limitation on why dynamic aura id doesn't work . Here's the link https://trailblazer.salesforce.com/ideaView?id=0873A000000E8fBQAS