Dynamically insert a disabled option in ng-options

I'm struggling to create a division in my select/option dropdown menu. The dropdown structure I am aiming for is as follows:

  • NO IMAGE
  • CUSTOM IMAGE


  • rest of data....

Despite my efforts, the code doesn't seem to be working as expected. Can anyone provide assistance?

controller

$scope.teamListWithOptions.push({ name: "NO IMAGE" }, { name: "CUSTOM IMAGE" }, { name: "____________", notSelectable: true });

//this just adds the rest of the data
for (var a = 0; a < data.length; a++) {
  $scope.teamListWithOptions.push(data[a]);
}

html

<select class="form-control" ng-model="contentTeam1Selected" ng-change="selectContentTeam1(contentTeam1Selected)" 
                                ng-options="team as team.name for team in teamListWithOptions" ng-disabled="team.notSelectable">
                        </select>

Answer №1

For those utilizing a more recent version of Angular (>= 1.4 possibly), incorporating the disable when syntax within the ng-options attribute is an option. Here's an example:

ng-option="p as p.name disable when p.show == false for p in people"

An illustrative fiddle can be found here: http://jsfiddle.net/wwu071so/1/

Answer №2

You may consider using the orderBy function to group items within an optgroup in order to achieve your desired effect.

Check out this example on jsFiddle

<select
        ng-options="p as p.name group by p.show for p in people | orderBy:'show'"
        ng-model="selectedPerson"></select>

$scope.people = [
    { id: 1, name: 'Image' },
    { id: 2, name: 'Custom'},
    { id: 3, name: 'John', show: ' ' },
    { id: 4, name: 'Ben', show: ' ' }
];
$scope.selectedPerson = $scope.people[3];

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

Creating a Node.js application using Express requires careful consideration of file structure and communication methods

Struggling to pass login form credentials to existing JavaScript files for logic and info retrieval. Login page contains a form with POST method. main.js handles the login: main.js module.exports = { returnSessionToken: function(success, error) { ...

AngularJS - Array binding issue not updating in directive

I am currently developing a directive that has the ability to display a series of controls. Each individual control is implemented as a directive named fsFilter. In the controller managing the parent element, I establish a binding between the filters arra ...

At what point does Angular's link function execute?

It appears that this script only runs once before the page is displayed. However, is there any scenario in which it runs after the page has been rendered? I decided to experiment with different scenarios using this plnkr: angular .module('app&apos ...

React: Resolving issue of state becoming outdated after fetching data through a network request in the useEffect hook. Stale state no

As I work on updating and referencing the state field hasError within the initialization function of my component, it's crucial for me to control whether a redirect occurs after successful initialization or if an error is displayed. Here's a sum ...

Modify the output information in a VueJS dropdown menu

I have a VueJS multiselect component that is displaying data from a JSON file using axios. Unfortunately, I am unable to modify the data directly. However, I would like to edit the text displayed and hide any blank entries. For example, I want 'BL&ap ...

Show 1,000 as "one thousand" using AngularJS

I recently started working with AGS and I am looking for guidance on how to effectively perform the following task: Displaying 1k as 1000 1.2k as 1200 1l as 100000 and so forth for any dynamic input number. Any assistance would be greatly appreciated. Tha ...

Which option offers superior performance: using attributes through HTML or jQuery?

At times, I find myself needing to include special classes, divs, and attributes in my HTML code for jQuery scripts to function properly (the site's "no-JavaScript" version doesn't require these elements). You probably understand what I mean. I& ...

Is there a way to access an SD card by clicking on an HTML link using JavaScript?

UPDATE: I am seeking a way to embed an HTML file with JavaScript or jQuery that can directly access the contents of the SD card while being opened in a browser. Currently, I have posted code for accessing it through an activity, but I want to be able to d ...

The ngResource ($resource) method is displaying a 404 error when trying to send a

It has been a considerable amount of time since I last utilized $resource to handle my service calls. Interestingly, all my requests are successfully reaching my REST end-points at /api/profile and /api/profile/:id. However, I am encountering a 404 error ...

What exactly is the significance of the error message "The JSX element type 'Header' does not have any construct or call signatures"?

I am encountering an error in my code, specifically with the Header and List elements. The error message keeps stating that the JSX element type Header does not have any construct... What could be causing this issue? import React, { useEffect, useState } ...

PHP causing syntax error when echoing HTML due to quotes being echoed along with the HTML code

I'm currently working with knockout.js combined with PHP and I have encountered an issue while trying to execute a GET request that returns an HTML/knockout form. The purpose behind this approach is to simplify the process for others using the form, a ...

Persistent change issue with JQuery CSS function

Looking for some help with a button-bar div that I want to display only after the user clicks on a "settings" button. Currently, I have set the button-bar div to have a display:none property in my css file. However, when the settings button is clicked, t ...

The resize function triggers twice as many events with each window resize

While working on my website, I encountered a navigation issue when resizing the browser from desktop to mobile size. Initially, the mobile menu worked on load, as did the desktop navigation. However, after running a script with $(window).on('resize&ap ...

What is the best way to access document identifiers in a Firestore Database?

I am completely new to working with Angular, Firestore, and Firebase. Currently, I have a component set up that successfully retrieves all my hero documents. I was able to display their names in a list. However, my next goal is to retrieve the unique ID f ...

Tips on including variables within quotation marks in JavaScript

I'm currently working on a JavaScript project using Pug. My goal is to use Express and MongoDB to develop a CRUD application that generates an edit button for each post, triggering a specific event when clicked. However, instead of the desired resul ...

Attempting to display a randomly selected element from an array upon clicking a button, utilizing axios and a local JSON file. Is there something crucial that I am overlooking?

I have figured out how to display the entire array in a random order but I am struggling to render just one element of the array. Another issue I am facing is showing the complete JSON object instead of only the quote text. Below is the HTML code: <te ...

Expand and collapse button for viewing more content

I need help figuring out how to display a list of 10 items on my website. I want to show only three items initially, with a "View More" button that, when clicked, will reveal the remaining items. The button should then change to "View Less" so users can ea ...

Issue with jQuery .hover() not functioning as expected

The issue I'm encountering is just as described in the title. The code functions correctly on all divs except for one! $(".complete-wrapper-1").hide(); var panelHH = $(".file-select-wrapper").innerHeight; $(".files-button").hover(function(){ $(" ...

How the Material-UI Tooltip zIndex takes precedence over the MenuItem in the Select component

My issue is quite straightforward. I have a <Tooltip> component wrapping a <Select> component, and when I click the Select, the tooltip appears above the MenuItems like this: Expected behavior: https://i.sstatic.net/VOVmf.png Unexpected beha ...

The select2 does not show the selected information

My select2 is not selecting the value when in edit mode. You can view my data here. Upon clicking the edit data button, it should select "settings" as the parent's value, but unfortunately, it's not working. You can see the issue here. Modal Sc ...