Prevent selection duplication in adjacent select by disabling the option in AngularJS ng-options

In my table, I have 2 select options that change dynamically.

<tr>
     <th>Start time</th>
     <th>End time</th>
</tr>
<tr ng-repeat="s in config.time">
     <td>
       <select ng-model="s.start_time" class="form-control" ng-options="option for option in config.hrs"></select>
     </td>
     <td>
       <select ng-model="s.end_time" class="form-control" ng-options="option for option in config.hrs"></select>
     </td>
</tr>

The values in config.hrs are also dynamic and contain hours in an array.

hrs = ["00:00","00:30","01:00",..,"23:00","23:30"]

I am trying to prevent the selection of the same hour for start and end times. Additionally, if a new row is added, previously selected hours should not be enabled.

I attempted to disable like this:

<select ng-model="s.end_time" class="form-control" ng-options="option disable when s.start_time for option in config.hrs"></select>
but it disabled all end time options.

Image 1 shows the current output. https://i.sstatic.net/rYNgI.png

Image 2 displays the expected output. https://i.sstatic.net/2ELep.png

Answer №1

It seems unlikely that your current setup will allow for this functionality.

Suggestion #1

You could consider maintaining an array of objects, such as

[{ time: '08:00', selected: false }, ...]
. This way, you can show only those options where selected === false, though it may require using a simple ng-repeat instead of ng-options on <option>...</option>.

Suggestion #2

Another approach could be to remove the selected time from an array upon selection, and then store it in a separate variable.

Answer №2

If you're looking for a solution, consider incorporating the following snippet of code:

    <tr>
       <input type="text" ng-model="item.name" class="form-control">
    </tr>

Implementing this may provide insight and assist in resolving your problem.

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

Issue detected: [ng:areq] The parameter 'Ctrl' is not recognized as a function

I am new to using angularJs and I am currently following this guide on asp.net-angularjs. However, I encountered an issue which is detailed below: entryCtrl.js (function (app) { 'use strict'; app.controller('entryCtrl', entryCtrl) ...

Toggle switch with active state

I'm currently using Ionic 2 alongside Angular 2 beta 11. Is there a way to turn off the toggle switch? The Ionic documentation suggests using the checked attribute, but I've tried that and also attempted ng-checked with no luck. Any advice on t ...

How can the backgroundImage css in react admin <Login /> be replaced using Material-UI?

I have been refering to the following resources: Learn about customizing login page background in React-Admin: Explore themes customization in Material-UI: https://material-ui.com/customization/components/ Instead of using a preset background, I want to ...

Angular Directive for dragging and dropping elements within an iFrame

Looking for controls to enable Drag & Drop functionality across an iFrame? The reason for needing this capability within an iframe is due to building a CMS where any "absolute" css changes on the editing page will also affect the main site. I have develop ...

Is there a way to hide an image once my scrollable div has reached a specific height?

I'm currently working on a project that involves a scrollable div with arrows above and below. My goal is to have the arrow above only appear after the div has reached a certain height. Below is the code I've been using... $("#scrolldivup").hid ...

How can I launch five separate instances of Chrome on a Selenium grid, each with a different URL?

I have a list of URLs saved in a JSON file, structured like this: { "urls": [ "http://www.google.com/", "http://www.stackoverflow.com" ] } Currently, these URLs are being opened sequentially by the Selenium WebDriver JavaScript manager on a ...

What strategies can I employ to address this historical issue?

Encountered TypeError: (0 , history_history__WEBPACK_IMPORTED_MODULE_6_.default) is not a function This issue arises in my history.js file import { createBrowserHistory } from 'history'; export default createBrowserHistory({ forceRefresh: tr ...

What is the process for creating a dynamic URL, such as example.com/profile/CroatiaGM?

I am in the process of developing a user control panel, and I have encountered some difficulties with creating a dynamic profile link. For instance, how can I achieve something like example.com/profile/CroatiaGM? Given that my website is primarily coded ...

Creating an error message display function using HTML and JavaScript when incorrect input is detected

I am having trouble creating an HTML5 and JS program that will show an error message if the user input is empty. Despite my efforts, I can't seem to get the "Error Message" to display. As a beginner, the video tutorial I'm following doesn't ...

AngularJS routing that is tailored to form input

I am trying to create a dynamic html page where the user can input form variables. My goal is to use AngularJS routing to navigate to different URLs based on the form inputs. Does anyone have advice or guidance on how I can achieve this functionality? ...

Tips for implementing a conditional statement within an export default clause

I am completely new to web programming and I'm attempting to create a question-answer feature using Vue.js. export default { // Trying to implement an if statement (if character == "Past", then do these steps) "steps": [ { ...

Embracing Asynchronous Header Management in Ember.js

I am facing an issue where I need to asynchronously insert the headers into the adapter. The token function is responsible for checking if the token has expired, refreshing it via Ajax if needed, and then returning the new token. However, it appears that ...

Guide to changing the border color in a Material-UI TextField component styled with an outline design

To complete the task, utilize the Material UI outlined input field (TextField component from "@material-ui/core": "^4.12.2",) and apply a custom blue color style to it. Here is the outcome of my work: https://i.sstatic.net/lw1vC.png C ...

Using JavaScript, import the variable module object from a specific module

Is there a way to import a module object from a module if I am unsure of the specific objects I will need beforehand? How can I utilize a variable in place of fixed module names? import { dynamicVariable } from './module' The variable represents ...

React-Redux-Saga: Only plain objects are allowed for actions. Consider using custom middleware for handling asynchronous actions

Struggling to integrate redux-saga into my react app, I keep encountering this error: Actions must be plain objects. Use custom middleware for async actions. The error appears at: 15 | changeText = event => { > 16 | this.props.chan ...

The user interface design transforms as a PDF file is being generated through html2pdf

I am experiencing an unusual problem while using html2pdf to convert an HTML page to a PDF file and download it. The conversion process is successful and the PDF file is downloaded without any issues. However, when I click on a button to generate the file, ...

Encountering a Problem with Chart.js Library During Online Tutorial on YouTube

Currently, I am immersed in a YouTube tutorial that guides me through the process of creating an expense tracker application. However, as I diligently follow the steps outlined in the tutorial, I encounter a hiccup along the way. Instead of witnessing the ...

Getting the value of dynamically generated buttons when they are clicked in PHP - a simple guide

In my PHP code, I have successfully created dynamic buttons. However, I am facing an issue where I need to retrieve the value of a specific button when it is clicked, and populate all the information in a form. Below is the code snippet for handling the b ...

Is there a way to implement an onclick event for every iframe within a document using jquery?

I have a webpage containing two iframes that can be switched using a selector. My goal is to implement an onclick event that will trigger a URL for specific <rect> elements within the iframes. After reading a helpful post on accessing iframe childr ...

Adjusting the size of a Vue component on the fly

I'm struggling to find a way to dynamically adjust the width of a component. The component I am working with is called 'vue-burger-menu' -> https://github.com/mbj36/vue-burger-menu. To set the width, you need to assign a number to the p ...