bootstrap-select dropdown options not getting loaded sometime

笑着哭i 提交于 2019-12-01 14:02:33
Vishwajeet Mishra

Hello samy i know this is too late to answer as your answer is bit correct with {{ refreshSelect() }} calling in view but it goes in infinite loop which breaks the bootstrap-select however i found the proper solution for it may be this can save someones day.

  this.http.get('http://google.com').subscribe(data => {
    console.log(data);
  }, error => {
    console.log(error);
  }, () => {
    setTimeout(() => {
      $('.selectpicker').selectpicker('refresh');
    });
  });

have a look at the third parameter it's the callback in subscribe method which can be used to refresh the selectpicker after the content has been loaded.

I made the bootstrap-select work by below solution, I'm sure there can be a better solution. Just sharing my solution.

1.Added below code to typings.d.ts

interface JQuery {
  addClass(className: string): JQuery;
  attr(attributeName: string, value: string|number): JQuery;
}
interface JQuery {
    selectpicker(options?: any, callback?: Function):any;
}

2.Added below line in my component.ts after all the import statements.

declare var $:any;

3.Added below function in my component.ts

refreshSelect() {
   $('.selectpicker').selectpicker('refresh');
}

4.Finally calling the refreshSelect() method in my template after iterating the select options.

<div *ngIf="observableOptions | async; let loadedOpts; else loadingSelect;">
  <select class="selectpicker" data-live-search="true">
    <option *ngFor="let opt of loadedOpts" value={{opt.id}}> {{opt.value}} </option>
  </select>
  {{refreshSelect()}}
</div>
<ng-template #loadingSelect>Loading User Data dropdown...</ng-template>

I'm sure this 4th step can be done in a better way. The key is to call the $('.selectpicker').selectpicker('refresh'); method after the ngFor iteration to make the bootstrap-select work. Please share if you have a better solution.

Thanks.

Add a delay.

   setTimeout(() => {
      jQuery('.selectpicker').selectpicker('refresh');
    }, 500);

From what I see you're probably need to put an ngIf statement to wait until the options are loaded before you show the drop-down.

It may be populating before you actually have data

You would also benefit from making the get request a promise. That way you can always ensure that it will resolve and give you the response you are looking for i.e. .subscribe()

And On further look you should probably .map and return the result to the subscription of the observable via next() or subscribing to the observable http call

Looks like the issue is with class="selectpicker". When I remove that class, the dropdown works fine, I don't get the bootstrap-select style though.

<div *ngIf="observableOptions | async; let loadedOpts; else loadingSelect;">
  <select class="selectpicker" data-live-search="true">
    <option *ngFor="let opt of loadedOpts" value={{opt.id}}> {{opt.value}} </option>
  </select>
</div>
<ng-template #loadingSelect>Loading User Data dropdown...</ng-template>

You can call:

$('.selectpicker').selectpicker('refresh');

In component.ts by adding the function:

ngAfterViewInit() {

    $('.selectpicker').selectpicker('refresh');
}

that will called after the view got rendered.

Maximo Cajina Jose Maria

If you installed bootstrap-select from nmp, you must place the path of the files in the configuration file angular.json, if the files folder of the bootstrap-select package was not installed with npm, it is enough to place the folder somewhere in the Angular project and specify the path in angular.json file

<div>
    "styles":[<br>`
`
         //the css path of the bootstrap-select package<br>
         "node_modules/bootstrap-select/dist/css/bootstrap-select.min.css"<br>    
      ],<br>
     <br>
    "scripts":[<br>
        //the js path of the bootstrap-select package<br>
         "node_modules/bootstrap-select/dist/css/bootstrap-select.min.css"<br>    
      ]
</div>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!