1

I have a angular component which has a form with two pulldowns. The first pulldown is a pretty simple. The user can select from a static list of items. The second pulldown is a select2 which should allow the user to selected from a list of results that are pulled from a remote API. I need to be able to change the url the select2 component uses when the user changes their selection in the first pulldown.

Here is the code I have now:

HTML:

   <div class="row">
      <div class="col-md-3">
        <div style="margin-top:10px;text-align: right;font-weight: bold">
          <label> Node </label>
        </div>
      </div>
      <div class="col-md-6">
        <div class="form-group half-margin-bottom">
          <div class="form-group__text ">
            <select name="node" id="node" (ngModelChange)="filtersNodeChange($event)" [(ngModel)]="userInputNode">
              <option *ngFor="let node of nodePullDownValues" [value]="node">{{ node }}</option>
            </select>
          </div>
        </div>
      </div>
    </div>
  </div>
  <div class="col-md-4">
    <div class="row">
      <div class="col-md-3">
        <div style="margin-top:10px;text-align: right;font-weight: bold">
          <label> Filter Key </label>
        </div>
      </div>
      <div class="col-md-6">
        <div class="form-group half-margin-bottom">
          <div class="form-group__text ">
            <select2 [options]="select2Options"></select2>
          </div>
        </div>
      </div>
    </div>
  </div>

TypeScript:

export class MappingDynamicComponent implements OnInit {

   ... omitted for brevity ...
   userInputNode = 'unknown';
   ... omitted for brevity ...

   constructor( ... ) {
      ...
   }

   setSelect2Options () {
     this.select2Options = {
       'width': '100%',
       'minimumInputLength': 3,
       'ajax': {
         'url': "http://dev-03.example.com:5200/api/v1/cm/cm_list/?cm_type=" + this.userInputNode + "&start_date=" + this.startDate,
        'dataType': 'json',
        'data': function (params) {
           var query = { 'starts_with': params.term, 'page': params.page || 1 };
           // Query parameters will be ?search=[term]&page=[page]
           return query;
        },
        'processResults': function (data) {
           var results = [];
           for (var i = 0; i < data.results.length; i++ ) { results.push( { "id": i+1, "text": data.results[i]} ); }
           // Tranforms the top-level key of the response object from 'items' to 'results'
           return { 'results': results };
        }
      }
    };
  }

  ... omitted for brevity ...

  filtersNodeChange(event) {
    this.userInputNode = event;
    this.setSelect2Options();
  }

  ngOnInit() {
    this.errorText = "";
    this.setStartDate();
    this.setNodePullDownValues();

    this.activatedRoute.queryParams.subscribe(params => {
      this.userInputNode = params['node'];
      this.setSelect2Options();
      ... omitted for brevity ...
    });
  }

The problem I am having is the url being used by the select2 component does not change when the user selects new option from the first pulldown. I had hoped that the filtersNodeChange() would change the value of the query parameter cm_list to what user has selected from the first pulldown.

1 Answer 1

1

ok I figured it out. I had to result to a tiny bit of jQuery, $( "#node" ).val(). I changed my url function to this:

this.select2Options = {
  ... 
  'ajax': {
    'url': function(params) {
      var url = "http://dev-03.example.com:5200/api/v1/cm/cm_list/?cm_type=" + $( "#node" ).val() + "&start_date=2019-04-26";
      return url;
    },
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.