Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angular 5 bootstrap-select: selectpicker is not a function

I wanted to install npm bootstrap-select for angular 5 application So I installed it using npm

npm install bootstrap-select

Then I installed required dependencies:

npm install jquery
npm install popper.js --save

angular-cli.json:

 "apps": [
    {
      "styles": [
        "styles.css"
      ],
      "scripts": [ "../node_modules/jquery/dist/jquery.min.js" ],
    }
  ],

and my package.json is (I removed some packages from a list):

"dependencies": { 
    (...)
    "@ng-bootstrap/ng-bootstrap": "^1.1.0",
    "bootstrap": "^4.1.1",
    "bootstrap-select": "^1.13.2",
    "jquery": "^3.3.1",
    "ng-multiselect-dropdown": "^0.2.1",
    "popper.js": "^1.14.4",
}

In html file, I added code:

<ng-template #modalWindow let-c="close" let-d="dismiss">
  <div class="modal-header">
    <h4 class="modal-title">Options</h4>
    <button type="button" class="close" aria-label="Close" (click)="d('Cross click')">
      <span aria-hidden="true">&times;</span>
    </button>
  </div>

  <div class="modal-body">     
    <select id="myspID" #selectPickerRef class="selectpicker form-control" multiple
        title="Select a number">
      <option>Opt1</option>
      <option>Opt2</option>
      <option>Opt3</option>
    </select>
  </div>

  <div class="modal-footer">
    <button type="button" class="btn btn-light" (click)="c('Close click')">Close</button>
  </div>
</ng-template>

Added select element is not styled, it looks like a simple bordered rectangle with listed options. It is not even a dropdown.

And here is a angular component element:

import { Component, OnInit, ViewChild } from '@angular/core';
import { NgbModal } from '@ng-bootstrap/ng-bootstrap';
import * as $ from 'jquery';

@Component({
  selector: 'app-modal-model-generator',
  templateUrl: './modal-model-generator.component.html',
  styleUrls: ['./modal-model-generator.component.css']
})
export class ModalModelGeneratorComponent implements OnInit {

  closeResult: string;

  @ViewChild('modalWindow')
  modalWindowRef: HTMLElement;

  @ViewChild('selectPickerRef')
  selectPickerEL: any;

  constructor(
    private modalService: NgbModal
  ) {}

  runBootstrapSelects(): void {
    //this.selectPickerEL.selectpicker();
    //$('.selectpicker').selectpicker();
    //$('select').selectpicker();
    $('#myspID').selectpicker();  //<--error is thrown here
  }

  openWindowCustomClass(params) {

    this.modalService.open(this.modalWindowRef, {
      windowClass: 'dark-modal',
      centered: true,
      size: 'lg'
    });
    this.runBootstrapSelects();
  }
}

There is an error:

$(...).selectpicker is not a function

inside runBootstrapSelects() component method.

EDIT:
angular-cli.json and angular component files content have been added.

If I add: @import "~bootstrap-select/dist/css/bootstrap-select.css"; to style.css file:

@import "~bootstrap/dist/css/bootstrap.css";
@import "~bootstrap-select/dist/css/bootstrap-select.css";

then <select> item is not shown on a page

But a file: /node_modules/bootstrap-select/tests/bootstrap4.html which is included to bootstrap-select node_modules folder works. Bootstrap4.html file contains bootstrap-select dropdowns which are shown styled.

like image 825
ael Avatar asked Nov 08 '22 02:11

ael


1 Answers

Yes because it works only when you are calling that plugin class "selectpicker" using jquery conditon.Make sure you are calling that element class name or element name in jquery condition.

// To style only selects with the selectpicker class

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

          (or)

// To style all selects

$('select').selectpicker();

like image 148
Karthik Avatar answered Nov 11 '22 12:11

Karthik