1

I am using Angular 6, am trying to add Bootstrap Multiselect in my project. But here am facing some issue bootstrap-multiselect.js not loading in page.

So am tying to add dynamically add this bootstrap-multiselect.js file, but it is not loading please help me on this

index.html

<!doctype html>
<html>
   <head>
      <meta charset="utf-8">
      <title></title>
      <base href="/">
      <meta name="viewport" content="width=device-width, initial-scale=1">
   </head>
   <body>
      <my-app>loading</my-app>
      <!-- Inclusion of sgwt widgets -->
      <noscript>Please enable JavaScript to continue using this application.</noscript>
   </body>
   <!-- Multiselect code -->
   <link rel="stylesheet" href="http://davidstutz.github.io/bootstrap-multiselect/dist/css/bootstrap-multiselect.css" type="text/css" />
   <script type="text/javascript" src="http://davidstutz.github.io/bootstrap-multiselect/docs/js/jquery-2.2.4.min.js"></script>
   <script type="text/javascript" src="http://davidstutz.github.io/bootstrap-multiselect/docs/js/bootstrap.bundle-4.5.2.min.js"></script>
   <script type="text/javascript" src="http://davidstutz.github.io/bootstrap-multiselect/docs/js/prettify.min.js"></script>
   <script type="text/javascript">
      $(document).ready(function() {
            $('#example-getting-started').multiselect({
              includeSelectAllOption: false,
              enableFiltering: true,
              includeFilterClearBtn: false,
              enableCaseInsensitiveFiltering : true,
            });
        });
   </script>
   <!-- Multiselect code -->
</html>

app.component.ts

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

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  ngOnInit() { 
     this.loadScript('http://davidstutz.github.io/bootstrap-multiselect/dist/js/bootstrap-multiselect.js');
   }

   public loadScript(url: string) {
    const body = <HTMLDivElement> document.body;
    const script = document.createElement('script');
    script.innerHTML = '';
    script.src = url;
    script.async = false;
    script.defer = true;
    body.appendChild(script);
  }
}

app.component.html

<!-- Build your select: -->
<select id="example-getting-started" multiple="multiple">
    <option value="cheese">Cheese</option>
    <option value="tomatoes">Tomatoes</option>
    <option value="mozarella">Mozzarella</option>
    <option value="mushrooms">Mushrooms</option>
    <option value="pepperoni">Pepperoni</option>
    <option value="onions">Onions</option>
</select>

Still it is showing like this:

enter image description here

Please help on this am new in angular 6. This question maybe already asked but that is not solve my issue.

Thanks in advance.

Ramesh S
  • 585
  • 3
  • 10
  • 32

1 Answers1

2

I've imported the libraries like you did from that website but only changed jquery.

  <link rel="stylesheet" href="https://davidstutz.github.io/bootstrap-multiselect/docs/css/bootstrap-4.5.2.min.css"
    type="text/css">
  <link rel="stylesheet" href="https://davidstutz.github.io/bootstrap-multiselect/docs/css/bootstrap-example.min.css"
    type="text/css">
  <link rel="stylesheet" href="https://davidstutz.github.io/bootstrap-multiselect/docs/css/prettify.min.css"
    type="text/css">
  <link rel="stylesheet"
    href="https://davidstutz.github.io/bootstrap-multiselect/docs/css/fontawesome-5.15.1-web/all.css" type="text/css">


  <script src="https://code.jquery.com/jquery-2.2.4.min.js"
    integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script>
  <script type="text/javascript"
    src="https://davidstutz.github.io/bootstrap-multiselect/docs/js/bootstrap.bundle-4.5.2.min.js"></script>
  <script type="text/javascript" src="https://davidstutz.github.io/bootstrap-multiselect/docs/js/prettify.min.js">
  </script>

  <link rel="stylesheet" href="https://davidstutz.github.io/bootstrap-multiselect/dist/css/bootstrap-multiselect.css" type="text/css">
  <script src="https://davidstutz.github.io/bootstrap-multiselect/dist/js/bootstrap-multiselect.js"></script>

Here's the stackblitz

Inside an ngb-modal: stackblitz

eko
  • 39,722
  • 10
  • 72
  • 98
  • Thanks for your answer I will implement your code – Ramesh S May 24 '21 at 14:27
  • let me know how it goes – eko May 24 '21 at 14:28
  • Hi am facing `error typeerror $(...).multiselect is not a function` – Ramesh S May 25 '21 at 06:00
  • My folder structure like this https://ibb.co/x3rQ4Pt I added your `ngAfterViewInit()` code in my child component please help me to solve this – Ramesh S May 25 '21 at 06:11
  • Hi, I haven't included the type definition files but added a `declare var $: any;` in my app.component.ts to suppress the type errors. Have you tried that? – eko May 25 '21 at 10:21
  • Yes I added `declare var $: any;` in my child component – Ramesh S May 25 '21 at 11:50
  • umm then that error message shouldn't appear. Can you try `($(...) as any).multiselect` ? – eko May 25 '21 at 12:12
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/232853/discussion-between-ramesh-s-and-eko). – Ramesh S May 25 '21 at 12:25
  • Hi bro now in I put console.log($('#example-getting-started')) it showing some data in console please check this url https://ibb.co/jJt2jq2 – Ramesh S Jun 08 '21 at 12:19
  • No bro before console.log not showing but now it is showing – Ramesh S Jun 08 '21 at 13:25
  • Haha nice! Glad you figured it out! – eko Jun 09 '21 at 13:39
  • Hello bro how to remove `keys` in `value` attribute please check this image https://ibb.co/27qmXkn here automatically `value` attribute added keys. eg: `value="0: 'rc-srvparvvae1478'"` . I just want `value="rc-srvparvvae1478"`. how to change this one. My code https://ibb.co/F8C34Yx – Ramesh S Jun 10 '21 at 12:52
  • Hello, you should really ask a separate question :-) there're other people on Stackoverflow that can help – eko Jun 10 '21 at 19:01
  • hi bro if you are free please check this https://stackoverflow.com/questions/67985275/angular-6-based-on-the-select-multi-select-dropdown-show-and-hide-not-working-pr – Ramesh S Jun 15 '21 at 11:20