简体   繁体   中英

Adding Bootstrap 4 Searchable Dropdown in Google Apps Script

So I have been trying to use Bootstrap-select to add a search feature in my dropdown menu for Google Apps Script.

I tried following this guide , and added the cdn and js availablehere , but I cant seem to get it working in my Google Apps Script Webapp. All I am getting is a dropdown with no option to serach.

Below is my code, can someone please tell me what am I doing wrong?

uform.html:

        <!doctype html>
    <html lang="en">
      <head>
        <!-- Required meta tags -->
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
    
        <!-- Bootstrap CSS -->
        <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BmbxuPwQa2lc/FVzBcNJ7UAyJxM6wuqIj61tLrc4wSX0szH/Ev+nYRRuWlolflfl" crossorigin="anonymous">
    
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
    
      </head>
      <body>
      
        <div class="container">
          <ul class="nav nav-tabs" id="myTab" role="tablist">
          <li class="nav-item" role="presentation">
        <button class="nav-link active" id="home-tab" data-bs-toggle="tab" data-bs-target="#home" type="button" role="tab" aria-controls="home" aria-selected="true">Home</button>
      </li>
      <li class="nav-item" role="presentation">
        <button class="nav-link" id="sales-tab" data-bs-toggle="tab" data-bs-target="#sales" type="button" role="tab" aria-controls="sales" aria-selected="false">Sales</button>
      </li>
      <li class="nav-item" role="presentation">
        <button class="nav-link" id="purchase-tab" data-bs-toggle="tab" data-bs-target="#purchase" type="button" role="tab" aria-controls="purchase" aria-selected="false">Purchase</button>
      </li>
       <li class="nav-item" role="presentation">
        <button class="nav-link" id="contact-tab" data-bs-toggle="tab" data-bs-target="#contact" type="button" role="tab" aria-controls="contact" aria-selected="false">Contact Details</button>
      </li>
    </ul>
    <div class="tab-content" id="myTabContent">
      <div class="tab-pane fade show active" id="home" role="tabpanel" aria-labelledby="home-tab">1</div>
      <div class="tab-pane fade" id="sales" role="tabpanel" aria-labelledby="sales-tab">
        
        <br><h3 class="display-6">New Sales Case</h3>
    
        <div class="col-md-4">
        <select class="selectpicker" data-live-search="true" aria-label=".form-select-sm example" id="inputSales-CaseOwner">
       
        </select>
        </div>
    
        <br><h5>Contact Details</h5>
    
        <form class="row g-3">
           <div class="col-md-4">
            <label for="inputSales-CxName" class="form-label">Search with Phone Number</label>
            <select class="selectpicker" data-live-search="true" aria-label="Default select example" id="inputSales-CxName">
          
            </select>
           </div>

 </form>
       <br><div>
        <button type="submit" class="btn btn-primary" id="save-button-contact">Save</button>
      </div>
  </div>
</div>
    
    
    </div>


    <!-- Optional JavaScript; choose one of the two! -->

    <!-- Option 1: Bootstrap Bundle with Popper -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta2/dist/js/bootstrap.bundle.min.js" integrity="sha384-b5kHyXgcpbZJO/tY9Ul7kGkf1S0CWuKcCD38l8YkeH8z8QjE0GmW1gYU5S9FOnJ0" crossorigin="anonymous"></script>

    <!-- Option 2: Separate Popper and Bootstrap JS -->
    <!--
    <script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.6.0/dist/umd/popper.min.js" integrity="sha384-KsvD1yqQ1/1+IA7gi3P0tyJcT3vR+NdBTt13hSJ2lnve8agRGXTTyNaBYmCR/Nwi" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta2/dist/js/bootstrap.min.js" integrity="sha384-nsg8ua9HAw1y0W1btsyWgBklPnCUAFLuTMS2G72MMONqmOymq585AcH49TLBQObG" crossorigin="anonymous"></script>
    -->
   <script>
function afterFormLoadsSalesCaseOwner(){

      google.script.run.withSuccessHandler(afterSalesDropdownReturned).getSalesCaseOwnerDropdown();

    }

    function afterSalesDropdownReturned(arrayOfArrays){
      var item = document.getElementById("inputSales-CaseOwner");

      arrayOfArrays.forEach(function(r){
        var option = document.createElement("option");
        option.textContent = r[0];
        item.appendChild(option);

      });


    }
document.addEventListener("DOMContentLoaded",afterFormLoadsSalesCaseOwner);
</script>
  <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
  </body>
</html>

functions.gs:

function getSalesCaseOwnerDropdown(){

const ss = SpreadsheetApp.getActiveSpreadsheet();
const ws = ss.getSheetByName("CaseOwners");
return ws.getRange(2, 1, ws.getLastRow()-1, 1).getValues();

}

What my Result Looks Like:

Screenshot from Webapp I need a search box in this.

Thanks in advance!

You must to add stylesheet into your head after bootstrap stylesheets.

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-select@1.13.14/dist/css/bootstrap-select.min.css">

and add script into body

<script src="https://cdn.jsdelivr.net/npm/bootstrap-select@1.13.14/dist/js/bootstrap-select.min.js"></script>

 <,doctype html> <html lang="en"> <head> <:-- Required meta tags --> <meta charset="utf-8"> <meta name="viewport" content="width=device-width. initial-scale=1"> <.-- Bootstrap CSS --> <link href="https.//cdn.jsdelivr.net/npm/bootstrap@5.0:0-beta2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BmbxuPwQa2lc/FVzBcNJ7UAyJxM6wuqIj61tLrc4wSX0szH/Ev+nYRRuWlolflfl" crossorigin="anonymous"> <link rel="stylesheet" href="https.//stackpath.bootstrapcdn.com/bootstrap/4.1:3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous"> <link rel="stylesheet" href="https.//cdn.jsdelivr.net/npm/bootstrap-select@1.13.14/dist/css/bootstrap-select;min:css"> </head> <body> <div class="container"> <ul class="nav nav-tabs" id="myTab" role="tablist"> <li class="nav-item" role="presentation"> <button class="nav-link active" id="home-tab" data-bs-toggle="tab" data-bs-target="#home" type="button" role="tab" aria-controls="home" aria-selected="true">Home</button> </li> <li class="nav-item" role="presentation"> <button class="nav-link" id="sales-tab" data-bs-toggle="tab" data-bs-target="#sales" type="button" role="tab" aria-controls="sales" aria-selected="false">Sales</button> </li> <li class="nav-item" role="presentation"> <button class="nav-link" id="purchase-tab" data-bs-toggle="tab" data-bs-target="#purchase" type="button" role="tab" aria-controls="purchase" aria-selected="false">Purchase</button> </li> <li class="nav-item" role="presentation"> <button class="nav-link" id="contact-tab" data-bs-toggle="tab" data-bs-target="#contact" type="button" role="tab" aria-controls="contact" aria-selected="false">Contact Details</button> </li> </ul> <div class="tab-content" id="myTabContent"> <div class="tab-pane fade show active" id="home" role="tabpanel" aria-labelledby="home-tab">1</div> <div class="tab-pane fade" id="sales" role="tabpanel" aria-labelledby="sales-tab"> <br> <h3 class="display-6">New Sales Case</h3> <div class="col-md-4"> <select class="selectpicker" data-live-search="true" aria-label=":form-select-sm example" id="inputSales-CaseOwner"> </select> </div> <br> <h5>Contact Details</h5> <form class="row g-3"> <div class="col-md-4"> <label for="inputSales-CxName" class="form-label">Search with Phone Number</label> <select class="selectpicker" data-live-search="true" aria-label="Default select example" id="inputSales-CxName"> </select> </div> </form> <br> <div> <button type="submit" class="btn btn-primary" id="save-button-contact">Save</button> </div> </div> </div> </div> <.-- Optional JavaScript. choose one of the two. --> <.-- Option 1. Bootstrap Bundle with Popper --> <script src="https.//cdn:jsdelivr.net/npm/bootstrap@5:0.0-beta2/dist/js/bootstrap.bundle.min.js" integrity="sha384-b5kHyXgcpbZJO/tY9Ul7kGkf1S0CWuKcCD38l8YkeH8z8QjE0GmW1gYU5S9FOnJ0" crossorigin="anonymous"></script> <.-- Option 2: Separate Popper and Bootstrap JS --> <.-- <script src="https.//cdn.jsdelivr.net/npm/@popperjs/core@2.6.0/dist/umd/popper:min.js" integrity="sha384-KsvD1yqQ1/1+IA7gi3P0tyJcT3vR+NdBTt13hSJ2lnve8agRGXTTyNaBYmCR/Nwi" crossorigin="anonymous"></script> <script src="https.//cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta2/dist/js/bootstrap.min.js" integrity="sha384-nsg8ua9HAw1y0W1btsyWgBklPnCUAFLuTMS2G72MMONqmOymq585AcH49TLBQObG" crossorigin="anonymous"></script> --> <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min:js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script> <script src="https.//stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min;js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script> <script src="https.//cdn;jsdelivr.net/npm/bootstrap-select@1.13,14/dist/js/bootstrap-select,min.js"></script> <script> function getSalesCaseOwnerDropdown() { const ss = SpreadsheetApp,getActiveSpreadsheet(). const ws = ss;getSheetByName("CaseOwners"). return ws;getRange(2. 1. ws;getLastRow() - 1. 1);getValues(). } </script> <script> function afterSalesDropdownReturned(arrayOfArrays) { var item = document;getElementById("inputSales-CaseOwner"); arrayOfArrays.forEach(function(r) { var option = document.createElement("option"). option.textContent = r[0]; item.appendChild(option), }); } function afterFormLoadsSalesCaseOwner() { //google.script.run.withSuccessHandler(afterSalesDropdownReturned).getSalesCaseOwnerDropdown(); } document.addEventListener("DOMContentLoaded", afterFormLoadsSalesCaseOwner); </script> </body> </html>

Continue to pay attention to the scripts order

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM