1

I am currently working in Servicenow Service portal where it support Angular

I am able to create a Search box using bootstrap as shown below:

 <select class="mdb-select md-form" searchable="Search here..">
  <option value="" disabled selected>Choose your country</option>
  <option value="1">USA</option>
  <option value="2">Germany</option>
  <option value="3">France</option>
  <option value="3">Poland</option>
  <option value="3">Japan</option>
</select>

This gives the correct result. But from the server I am pulling country names which I want set up dynamically upon every update the result may change. I am able to receive a country names as shown below in an array like: Thailand, China, India, Singapore.

How can I set above all the country names in the select box?

halfer
  • 19,824
  • 17
  • 99
  • 186
Abdul Azeez
  • 63
  • 3
  • 9
  • 2
    what do you mean by "upon every update the result may change"? – Nick Parsons Mar 22 '20 at 11:49
  • @Nick Parsons, based on the user we are pulling out the countries result. For every user result of listed countries may change so i want to set the option using a script – Abdul Azeez Mar 22 '20 at 12:23

4 Answers4

1

In your server script, set the global data object property which you can then reference in your HTML.

Server Script:

// perform a query to get your countries array: 
var countries = ["Thailand", "China", "India", "Singapore"];

data.countries = countries.map(function(text, i) {
  return {value: i+1, text: text};
}); 

Then, in your HTML, to iterate the data and create <option> tags dynamically, you can use ng-options in your <select>

HTML Template:

<select class="mdb-select md-form" 
  ng-options="obj.text for obj in data.countries track by obj.value"
  ng-model="c.data.selectedCountry" searchable="Search here..">
    <option value="" disabled selected>Choose your country</option>
</select>

Above, the value selected is reflected to the c.data.selectedCountry variable defined on in your client script.

Client Script:

function() {
  /* widget controller */
  var c = this;
  c.data.selectedCountry = ""; // changes based on value selected in dropdown

  // other code...
}
Nick Parsons
  • 45,728
  • 6
  • 46
  • 64
  • Sir, I just copy pasted but i am getting an empty result, i see only Choose your country field when i click i unable to open that :( – Abdul Azeez Mar 22 '20 at 12:46
  • @AbdulAzeez sorry, see update (I had to add `data` to `countries` in the `ng-options`) – Nick Parsons Mar 22 '20 at 12:53
  • No sir, still it is empty :( if by any chance can you look at my instance ? https://dev51617.service-now.com/sp_config?id=widget_editor&sys_id=915c1a882f37c810209857892799b695&spa=1 admin / 12345 – Abdul Azeez Mar 22 '20 at 12:57
  • @AbdulAzeez I looked at your instance. You're doing `countries.map()` when `countries` is a string. Do `nameArray.map()` instead. I've updated the instance for you so it should be working. I also added change event function which prints the value of the selected value when the dropdown is changed – Nick Parsons Mar 22 '20 at 13:11
  • Thank you Sir.. One last question can you please explain me this line of code: ng-options="obj.text for obj in data.countries track by obj.value" ? What it does, i dont see i defined obj anywhere in the server side – Abdul Azeez Mar 22 '20 at 13:53
  • @AbdulAzeez it is responsible for creating the – Nick Parsons Mar 22 '20 at 13:57
  • 1
    Got it you. Thank you So mcuh – Abdul Azeez Mar 22 '20 at 14:01
  • i got struck up at some place can you please look into it ? https://stackoverflow.com/q/60801221/12309720 – Abdul Azeez Mar 22 '20 at 15:31
  • @AbdulAzeez looks like your figured it out? – Nick Parsons Mar 23 '20 at 00:19
  • 1
    Yes Nick. Thank you :) – Abdul Azeez Mar 23 '20 at 07:17
  • Sir, I again need your help i am sorry to trouble you. Can you please help me on this. https://stackoverflow.com/questions/60825934/how-to-pass-values-from-server-to-html-in-servicenow-service-portal – Abdul Azeez Mar 24 '20 at 06:29
0

Although I am a react guy but as far as I know it can be done in this way. So lets say you have the countries array.

$scope.countries = [{
  id: 1,
  name: 'USA'
}, 
{
  id: 2,
  name: 'France',
}];

<select class="selectpicker" 
 ng-options="country as country.name for country in countries track by 
 country.id" 
 ng-model="selected">
</select>
Muhammad Haseeb
  • 1,269
  • 12
  • 22
0

This is my version of the fix, I understand you need to use a normal select tag, so I have provided a solution without using any custom select plugin, please let me know if this fixes your issue.

HTML Code:

<select class="mdb-select md-form" id="test" searchable="Search here.." multiple #selectBox (change)="onChange($event.target)">
  <option value="" disabled>Choose your country</option>
  <option 
    *ngFor="let item of countries" 
    [value]="item.id" 
    [selected]="userCountries.indexOf(item.id) > -1">{{item.name}}</option>
</select>

Component code:

import { Component, ElementRef } from '@angular/core';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent  {
  userCountries = [
    1,2,3,4
  ];
  countries = [
    {
      name: 'USA',
      id: 1,
    },
    {
      name: 'Germany',
      id: 2,
    },
    {
      name: 'France',
      id: 3,
    },
    {
      name: 'Poland',
      id: 4,
    },
    {
      name: 'Japan',
      id: 5,
    },
  ];

  onChange(selectCtrl) {
    const values = Object.keys(selectCtrl['selectedOptions']).map(key => selectCtrl['selectedOptions'][key]);
    const output = values.map((item: HTMLOptionElement) => item.value);
    this.userCountries = values.map((item: HTMLOptionElement) => +item.value);
    console.log(this.userCountries);
  }
}

Stack blitz example: Stackblitz example

Naren Murali
  • 19,250
  • 3
  • 27
  • 54
-1

You may want to use library bootstrap-select, the demo code as bellow

<select class="selectpicker" data-live-search="true">
  <option data-tokens="ketchup mustard">Hot Dog, Fries and a Soda</option>
  <option data-tokens="mustard">Burger, Shake and a Smile</option>
  <option data-tokens="frosting">Sugar, Spice and all things nice</option>
</select>

You can check more sample here.

Hope this helps!

Van Tho
  • 618
  • 7
  • 20
  • He want to use current library `mdbootstrap`, so you shoud better find a solution with it, before suggesting as new library. – Van Tho Mar 22 '20 at 12:52