2

i am using a custom lookup component and i am unable to set predefined value on it , i am unsure of what part of my component is the value attribute to set it , i have checked below post but doesnt help me out.pls let me know

How do we pre-populate force:inputField in Lightning Component?

<aura:attribute name="selectedRecord" type="Object" default="{}" description="Use,for store SELECTED sObject Record"/>
   <aura:attribute name="listOfSearchRecords" type="List" description="Use,for store the list of search records which returns from apex class"/>
   <aura:attribute name="SearchKeyWord" type="string"/>
   <aura:attribute name="ObjectString" type="string"/>
   <aura:attribute name="AutonumInteger" type="Integer"/>
   <aura:attribute name="Message" type="String" default="Search Result.."/>

   <aura:handler name="oSelectedObjectEvent" event="c:SelectedObjectEvent" action="{!c.handleComponentEvent}"/>
   <aura:handler event="aura:waiting" action="{!c.showSpinner}"/>
   <aura:handler event="aura:doneWaiting" action="{!c.hideSpinner}"/>



      <div aura:id="searchRes" class="slds-form-element slds-lookup slds-is-close" data-select="single">
         <label class="slds-form-element__label" for="lookup-348"> {!v.ObjectString} Name </label>

         <!--This part is for display search bar for lookup-->  

            <div class="slds-input-has-icon slds-input-has-icon--right">
               <c:svg class="slds-input__icon slds-show" xlinkHref="{!$Resource.SLDS222 + '/assets/icons/utility-sprite/svg/symbols.svg#search'}" />
               <!-- This markup is for when an record is selected -->
               <div aura:id="lookup-pill" class="slds-pill-container slds-hide">
                  <span class="slds-pill">
                      <aura:if isTrue="{!v.ObjectString != 'Case'}">
                      <span class="slds-pill__label">
                       {!v.selectedRecord.Name}  
                      </span>
                      </aura:if>
                      <aura:if isTrue="{!v.ObjectString == 'Case'}">
                      <span class="slds-pill__label">
                       {!v.selectedRecord.caseNumber}  
                      </span>
                      </aura:if>
                      <button class="slds-button slds-button--icon slds-pill__remove" onclick="{!c.clear}">
                        <c:svg class="slds-button__icon" xlinkHref="{!$Resource.SLDS222 + '/assets/icons/utility-sprite/svg/symbols.svg#close'}" />
                        <span class="slds-assistive-text">Remove</span>
                     </button>
                  </span>
               </div>

              <div aura:id="lookupField" class="slds-show">
               <ui:inputText updateOn="keyup" keyup="{!c.keyPressController}" class="slds-lookup__search-input slds-input " value="{!v.SearchKeyWord}" placeholder="search.."/>

              </div>   
            </div>


          <!--This part is for Display typehead lookup result List-->  

          <div class="slds-lookup__menu slds" id="lookup-348">
            <div class="slds-lookup__item--label slds-text-body--small">{!v.Message}</div>
            <center> <ui:spinner aura:id="spinner"/> </center>

            <ul class="slds-lookup__list" role="listbox">
               <aura:iteration items="{!v.listOfSearchRecords}" var="singleRec">
                  <c:customLookupResult Sobject="{!singleRec}" />
               </aura:iteration>

            </ul>
         </div>

      </div>
   <!--</div>-->
</aura:component>

1 Answer 1

1

You need to define a method to initialise a value if one can be found.

You may want to override the render/rerender object and call your method there or you could try attaching it to an attribute change handler - or finally, you could expose the initData routine via an aura:method and call it manually.

Your class would be something like this (note this is untested and can't really be tested as you have not provided all the required code - for example, you have not define what your Apex controller search method looks like):

initData: function(cmp) {
  var action = cmp.get('c.getCurrentValue');
  var self = this;
  action.setParams({
    'type': cmp.get('v.ObjectString'),
    'value': cmp.get('v.recordId'),  
    // you don't seem to have a record id attribute, 
    //so you will need to add it and store it when you find a record
  });

  action.setCallback(this, function(a) {
    if (a.error && a.error.length) {
      return $A.error('Unexpected error: ' + a.error[0].message);
    }
    var result = a.getReturnValue();
    cmp.set("v.SearchKeyWord", result);

    if (null != result) {
      // Show the Lookup pill
      var lookupPill = cmp.find("lookup-pill");
      $A.util.removeClass(lookupPill, 'slds-hide');

      // Lookup Div has selection
      var inputElement = cmp.find('searchRes');
      $A.util.addClass(inputElement, 'slds-has-selection');
    }
  });
  $A.enqueueAction(action);
}

You must log in to answer this question.

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