Guide on inserting data from an array into a Bootstrap dropdown menu with a search feature

Utilizing the bootstrap combobox with search functionality has been successful when using static options, as shown below:

<form role="form">
   <div class="form-group">
      <label>Select options</label>
      <select class="combobox input-large form-control" style="display: none;">
             <option value="" selected="selected">Type Planet name...</option>
             <option value="0">Mercury</option>
             <option value="1">Earth</option>
             <option value="2">Venus</option>
             <option value="3">Mars</option>
             <option value="4">Jupiter</option>
       </select>
   </div>
</form>

My goal is to dynamically load options from an array. I attempted using ng-options and ng-repeat but encountered issues with displaying values in the combo box. Preferably, I would like to use JavaScript.

Attempted Code:

<select class="combobox input-large form-control" style="display: none;"
        ng-model="PlanetData.selectedPlanet">
        option value="" selected="selected">Type for Planet...</option>
        <option ng-repeat="planet in PlanetData.availablePlanets" value="    {{planet.id}}">{{planet.planetName}}</option> 

This code creates a combobox with the text "Type for channel", however the planet data does not appear within the combobox options, instead it appears below it. Seeking guidance on how to properly bind the data so that it displays within the combobox.

Answer №1

The issue arises when the function combobox is called before the options are rendered by ng-repeat or ngOptions.

A simple solution is to invoke $('.combobox').combobox() inside a callback function using $timeout.

$scope.items = [{id: 1, text: 'text1'}, {id: 2, text: 'text2'}];

$timeout(() => 
    $('.combobox').combobox(), 0);

View the plunker demonstration here: https://plnkr.co/edit/j25lasvpHxmMNUjl7EjE?p=preview

I have successfully tested this approach with both ngRepeat and ngOptions.

Answer №2

const planets = ['Venus', 'Neptune'];

$.each(planets, function(idx, val){
  
$('.dropdown').append('<option value="'+idx+'">'+val+'</option>');
  
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form role="form">
   <div class="form-group">
      <label>Choose from the following</label>
      <select class="dropdown input-medium form-control">
           
       </select>
   </div>
</form>

Answer №3

The problem arose due to the ng-options or ng-repeat not rendering properly when the combobox was displayed. Following Rafael's advice, I was able to resolve it by using a timeout function as he recommended.

$timeout(() => $('.combobox').combobox(), 100);

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Displaying identical images repeatedly, alter just one using JQuery with each change

In the design, I have both gray and blue stars displayed multiple times on the page. What I'm trying to achieve is that when a user clicks on a gray star, it should switch to blue, and vice versa. However, the current issue I'm facing is that cli ...

Sticky footer in Bootstrap causing content to overlap

I'm a beginner when it comes to Bootstrap, and I'm attempting to integrate it with Symfony2. I currently have a sticky top bar in place for navigation purposes. However, I'm facing an issue when I try to add a similar sticky footer at the bo ...

Replacing values in an HTML file with MySql query results

----- Problem solved, solution below ----- In my HTML file, I have a dropdown menu for various courses listed as follows: <ul> <li class="dropbtn" id="1"> <a href="">first</a> <ul class="dropdown-content"> ...

Angularjs version 1.2 now includes an additional empty option within the select feature

I am encountering an issue with a dropdown menu that is populated with items from a database. The default selection for this dropdown is supposed to be retrieved from a cookie. However, I am facing two problems: 1. There is an empty option as the first i ...

modify an element using jquery

Hey there! I'm facing an issue where I have an ID of #item_quantity in a span, and I want it to refresh its contents once a button with the ID of #update_cart is clicked. It seems that everything else updates fine on click, but for some reason, the sp ...

Error in AngularJS and TypeScript: Property 'module' is undefined and cannot be read

I'm attempting to incorporate TypeScript into my AngularJS 1.x application. Currently, my application utilizes Webpack as a module loader. I configured it to handle the TypeScript compilation by renaming all *.js source files to *.ts, and managed to ...

Storing HTML content in a MySQL database using the MVC architectural pattern

I currently have a straightforward method that successfully posts a string to my MySQL server and saves the text without any issues. However, when I include a tag within the string, my google console displays an error stating that the page could not be f ...

What could be causing my code to retrieve duplicate items every time the page is loaded?

Currently, I'm diving into the world of React and attempting to fetch product data for a list. The goal is to initially fetch 2 items and then load 2 more items when a button is clicked. However, the issue I'm facing is that it's fetching t ...

Once the PHP page loads, it becomes challenging for me to dynamically change values in JavaScript

Within my code snippet below, I am aiming to modify the initial value using a slider. The slider appears to be functioning correctly; however, it is not updating the values of timeout as indicated beneath the line $('ul.<?php echo $this->session ...

Python.Selenium. Unable to locate current element. Techniques for switching frames

I am trying to collect feedback from a specific page at this URL. After waiting for the page to load, I attempted to locate elements using Google Chrome inspector. However, Selenium is unable to find these elements, and I also could not find them in the p ...

Display content in a div after the page is fully loaded with either a placeholder or animated loading

Hello everyone, this is my first post on here. I am relatively new to PHP, so any help or advice would be greatly appreciated. ...

Leverage the power of AJAX and PHP to securely save comments for future

I have coded a JavaScript function that uses POST and GET methods to send comments from an input field and retrieve them when the page reloads. However, I am unsure of how to handle the data after it is sent in order to save it and access it again later. E ...

Finding the label that is linked to the current DIV or textarea by its "for" property

I am working on a project that involves two elements - a textarea and a div. Each element has a label attached to it using the "for" attribute in HTML. <textarea id="txta_1" class="txta" cols="40" rows="3" onkeyup ...

The Bootstrap tooltip fails to display the visual style of the tooltip, only showing the data

Following the Bootstrap documentation for tooltips, I have tried to make the tooltip function work properly. However, when I hover over the text, the tooltip text Some tooltip text! only shows up with the alt="" function, but not styled as described in the ...

Learn how to eliminate all text characters from a string using jQuery, leaving only the numerical values behind

My webpage features a variety of different products, each with their own values. When trying to calculate the total sum of these products and display it in the shopping cart, I encountered an error displaying NaN. How can I remove this NaN from the strin ...

Obtain a pointer to the timestamp within a embedded Kibana chart on an HTML webpage

The image at the specified link shows a black box labeled @timestamp displaying date and time information. Unfortunately, viewing the image requires a reputation of 10. Link to image: https://www.elastic.co/assets/bltde09cbafb09b7f08/Screen-Shot-2014-09-3 ...

Double the names in a single <input>

Is it possible to assign two different names within a single input tag, like this: <input type='text' name='food' name='user_search_input'>? In my PHP file, I intend to use the input in two separate "if" clauses - one fo ...

Protractor test freezes after attempting to click on an element

I have encountered a challenge while attempting to write a protractor test that selects an item from a custom dropdown menu. The issue arises when trying to click on an element other than the last one in the list, as it hangs and times out. Interestingly, ...

What is the best way to read a .dat file in node.js and convert it into easily readable text or JSON format

I am facing an issue with a .dat file that contains information which is difficult to read. Therefore, my attempt was to open and convert this file into human-readable text or JSON format. Despite writing the code to read the file, I'm uncertain abo ...

What could be causing AngularJS to fail to send a POST request to my Express server?

I am currently running a Node Express server on localhost that serves a page with AngularJS code. Upon pressing a button on the page, an AngularJS controller is triggered to post a JSON back to the server. However, I am facing an issue where the post requ ...