-2

Aura Code:

<aura:iteration items="{!v.O2CSubprocess}"  var="mapKey" indexVar="key">  

    <aura:iteration items="{!mapKey.value}" var="mapValue" indexVar="index">

        <label class="slds-checkbox slds-box" style="width:100%;">
            <ui:inputCheckbox text="{!mapValue}"
                              value="{!mapValue.isChecked}"
                              aura:id="{!index}"

                              change="{!c.selectSubProcessO2C}"/>
            <span class="slds-checkbox--faux" /> &nbsp;
            <span class="slds-form-element__label" style="font-size: 15px;">{!mapValue.subprocessName}</span>
            <br/>
            <aura:if isTrue="{!mapValue.isChecked}">
                <!-- tt<lightning:input name="input2" label="we" value="{!v.O2CWeightage}" class="field"/>-->
                <ui:inputText aura:id="o2cID" label="Weightage:" placeholder="Search By Name"
                              class="field"
                              labelClass="slds-form-element__label"
                              value="{!mapValue.Weightage}"


                              required="false"/>
            </aura:if>

        </label>
    </aura:iteration><br/>
</aura:iteration>

controller js

 selectSubProcessO2C: function(component, event, helper){
    var selectedprocess = event.getSource().get('v.value');
    var getSelectedNumber = component.get("v.selectedCountO2C");
    var selectedSubprocess=component.get("v.CheckboxSelected");
    //var getSelectedNumberAp = component.get("v.selectedCountAP")
    if(selectedprocess==true){
        getSelectedNumber++;
        selectedSubprocess.push(event.getSource().get('v.text'));
    }
    else{
        getSelectedNumber--;  
    }
    component.set("v.selectedCountO2C",getSelectedNumber);
    component.set("v.CheckboxSelected",selectedSubprocess);
    var weightage= component.find("o2cID");

    //var value2=weightage.get('v.value');

    var o2cweightage=100/component.get("v.selectedCountO2C");

    if(Array.isArray(weightage)){
        console.log('weightage==>'+weightage.length);
        component.set("v.value",100/weightage.length);
    }
    else{
        component.set("v.value",100);
    }

},

Requirement: upon selecting 1 checkbox, the value should be 100,if two checkboxes were selected value should be 50 if 4 checkboxes were selected it should be 25.

but, above code is working only when one checkbox is selected, next time giving me an error like weightage.set is not function

  • As per your code, if your map has more than 1 value then it will create multiple ui:inputText with same auraId where aura:id will return array and for which you can't set value. – Chetan Nandre Sep 16 at 5:48
  • Any suggestion,then how can i acheive it? – Vanaja Kasaragadda Sep 16 at 6:48
2

From documentation,

  1. If the local ID is unique, find() returns the component
  2. If there are multiple components with the same local ID, find() returns an array of the components
  3. If there is no matching local ID, find() returns undefined

So, you should first check if its array, and if its array you can get length or else you can directly set to 100.

var weightage= component.find("o2cID");

if(Array.isArray(weightage))
    component.set("v.value",100/weightage.length);
else
    component.set("v.value",100);
  • Can you add your code in question, there could be some syntax error – salesforce-sas Sep 16 at 6:51
  • its component.set only. Your code looks fine, why do you think its not working? problem could be somewhere else, – salesforce-sas Sep 16 at 7:57
0

Adding a for loop,for array helped. Thanks everyone. below is the code that worked.

var weightage=[]; weightage= component.find("o2cID"); var o2cweightage=100/component.get("v.selectedCountO2C"); //weightage.set("v.value",o2cweightage); if(Array.isArray(weightage)){ console.log('weightage==>'+weightage.length); for(var i=0;i

    }
    else{
       weightage.set("v.value",o2cweightage);
    }
  • 1
    Your question is not clear. You should have said in question you wanted to change value of same input – salesforce-sas Sep 16 at 17:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.