0

i am trying to set value in dropdown from query string but unable to do so , below is the code

private SelectReferralFromQueryString(name) {
   name = this.GetParameterValues('Country');

  if (name != false) {

      name = decodeURIComponent(name);
      //here name comes correct as Keith T. What should I do from here?
  }

}
private GetParameterValues(param) {
  var url = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
  for (var i = 0; i < url.length; i++) {
      var urlparam = url[i].split('=');
      if (urlparam[0] == param) {
          return urlparam[1];
      }
      else
          return false;
  }
}



<Dropdown
        placeholder="Select an option"
        selectedKey={this.SelectReferralFromQueryString(name)}
        label="TRAVELLING TO"
        options={this.state.ddlCountry}
        styles={dropdownStyles}
        onChange={(_, optionSelected) => {this.DataBind(optionSelected.key,'TravelTo');
        
        }}
      />
0

The SelectReferralFromQueryString method does not require the name parameter,you could remove it.

My test code:

<Dropdown
        placeholder="Select an option"
        label="Basic uncontrolled example"
        options={options}
        styles={dropdownStyles}
        onChange={(event,options,index)=>console.log(event,options,index)}
        
        selectedKey={this.state.names}
      />
private  SelectReferralFromQueryString(){
     var names=[];
     var name;
    name = this.GetParameterValues('Country');
 
   if (name != false) {
 
       name = decodeURIComponent(name);
       //create an array to save country name
       names.unshift(name);
       //save the array to state
       this.setState({names})
   }
 
 }
 private GetParameterValues(param) {
   var url = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
   for (var i = 0; i < url.length; i++) {
       var urlparam = url[i].split('=');
       if (urlparam[0] == param) {
           return urlparam[1];
       }
       else
           return false;
   }
 }

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.