Filter the options in ng-options according to various controls or ng-models

Scenario:

In my application, I have a backend built on Laravel 5 and a frontend using AngularJS. The purpose of this application is to allow users to select multiple high-value products that are dependent on each other.

A. Initially, the user selects a product from a drop-down menu and adds it to their cart.

B. After adding the first product, the user can click on an "Add" button to dynamically add more products. This action creates a new row with a drop-down menu for selecting additional products. The dynamic addition of rows is achieved through AngularJS by utilizing $compile/$scope functionality to incorporate compiled HTML elements.

C. When a new row is added, the drop-down menu initially shows no pre-selected values for product selection.

D. In order to add a new product to the cart, the user must select it from the drop-down menu.

E. Users are able to continue adding up to 50 products in total.

Refer to the following UI design for visualization:

The specific requirement is that when a new row is added, the corresponding drop-down menu should exclude any products that have already been selected in the drop-down menus above.

If anyone could provide assistance with this matter, it would be greatly appreciated!

Thank you!

Answer №1

Give this a try, hopefully it will be useful.
var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope,$filter) {
  $scope.rep = [];
  $scope.xyz = function(){
    var selectedProducts = $filter('filter')($scope.rep, $scope.sel) ;
    if(selectedProducts.length <= 0){
       $scope.rep.push($scope.sel)
       return false;
    }

  };
  $scope.coll =[
   {id: 1,
   rate : 50,
   base_price : 50,
     product_name : 'ab'
   },
   {id: 2,
   rate : 50,
   base_price : 505,
    product_name : 'abc' 
   },
   {id: 3,
   rate : 520,
   base_price : 500,
     product_name : 'abcd'
   },
   {id: 4,
   rate : 50,
   base_price : 505,
    product_name : 'abcef' 
   },
  ];
});

Here is the HTML code:

 <body ng-controller="MainCtrl">
    <select name="sel" id="sel" ng-model="sel"
    ng-options="x as x.product_name for x in coll track by x.id" ng-change="xyz()">
      <option value="">select product</option>
    </select>
    <table border="0">
      <thead>
        <tr>
          <td>name</td>
          <td>price</td>
        </tr>
      </thead>
      <tbody>
        <tr ng-repeat="x in rep">
          <td>{{x.product_name}}</td>
          <td>{{x.base_price}}</td>
        </tr>
      </tbody>
    </table>
  </body>

For further reference, you can view it here.

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

Adding a default option to my AngularJS select box

React const SnazzyContext = React.createContext(); function App() { return ( <SnazzyContext.Provider value={getList()}> <MenuSideController /> </SnazzyContext.Provider> ); } function getList() { // Function to fetch ...

Unlocking protection: Confirming password strength and security with password indicator and regular expressions for special characters in angular through directive

I have developed an app for password validation using an AngularJS directive. The requirements for the password include at least one special character, one capital letter, one number, and a minimum length of 8 characters. Additionally, I have included a pa ...

Guide on dynamically accessing child records of a Firebase array in Angularfire

I'm in the process of setting up an accordion using angularfire. I've managed to fetch the main categories ("A1", "D1", "R1") for display, but I'm struggling to retrieve the child elements for each selected accordion tab. For example, if I s ...

How to effectively utilize TypeScript in a team environment using both Atom and VSCode?

Our team utilizes TypeScript with both Atom and VSCode as our editors, but we are facing challenges with the tsconfig.json file. VSCode is not recognizing the typings, causing the namespace for 'ng' (for Angular 1.x) to be unknown in VSCode. Wh ...

What is the best way to combine a hyperlink with a string in an Angular form?

Currently I am in the process of learning angular and experimenting with creating a list of websites that can be clicked on, similar to what you would find in a bookmark app. This is based on a todo example. https://github.com/LightYear9/ToDoList In orde ...

Is it necessary for me to generate a directive in order to leverage $asyncValidators?

Here is the code snippet that I put together: <input class="inputField" id="registerUserName" name="registerUserName" ng-model="aus.registerUserName" ng-model.$asyncvalidators.unique="aus.isUsernameAvailable"; ng-model-options="{ de ...

Is there a method available to incorporate a scroller into an nvd3 chart?

I am encountering an issue with my nvd3 chart. When I have a large amount of data that exceeds the width of the chart container, there is no scroll bar present and I'm struggling to figure out how to add one. I attempted to include overflow:scroll wi ...

The $q.all() function in angular seems to struggle with resolving properly

Having trouble with 3 $http calls in a factory. Creating 4 promises: var promise = $q.defer(), PBdeferred = $q.defer(), Rdeferred = $q.defer(), Pdeferred = $q.defer(); Making the first call to the API: $http.get('/pendingBills').then(fu ...

Having trouble inserting a new row into the AngularJS table?

Having trouble adding a new row to the table in angularjs when clicking the add button. Here is the link to the Plunkr example -https://plnkr.co/edit/s7XAaG4JbvbTTxiZ0V2z?p=preview The HTML code snippet is as follows: <html> <head> <script ...

JavaScript code to use Angular expressions in an HTML document

I am facing an issue with using Angular expressions inside JavaScript within an ng-repeat loop on my HTML page (note that the app and controller have been declared elsewhere): <div ng-repeat="eleRubrique in scopeComplet"> <script type="text ...

UI-Router fails to navigate when a parameter is included

Working with UI-Router. I have an initial controller vehicuels.controller.js: 'use strict'; angular.module('autoPrivilegeApp') .controller('VehiculesCtrl', function ($scope,$http, $state) { $scope.awesomeThings = []; ...

What is the best way to programmatically activate a selectbox click within a function's scope?

I am currently developing a mobile app with phonegap, jQuery Mobile, and AngularJS. I am trying to activate a click event once I have clicked on another icon. To achieve this, I am calling a scope function where I have attempted using $('#id').c ...

Connect ngOptions to an array beyond the current scope

Can the ngOptions be bound to a value that is not within the $scope? I have enums that will be generated as JavaScript code. These enums are not currently part of "the angular domain", but I want to bind an ngOptions to one of the arrays without manually ...

Adding a character to an AngularJS textbox

I am attempting to add the "|" Pipe symbol to a textbox when a button is clicked, using this function. $scope.appendPipe = function(){ var $textBox = $( '#synonyms' ); $textBox.val($textBox.val()+'|'); //textBox ...

What is the best way to change a variable in an AngularJS view?

My Request In my application, I have implemented 3 views, each with its own controller. The first view is the home screen, and from there the user can navigate to view 2 by clicking on a div element. On view 2, the user can then move to view 3 by clicking ...

Error: The 'controller' argument is not recognized as a function after adding ngCookies

I'm having trouble importing ngCookies in my controller. When I tried to add ngCookies, I received the following error: Argument 'HomeCtrl' is not a function, got undefined This is how I added ngCookies in my Controller.js file: angular.m ...

Steps for clearing input field with type=date in Protractor:

I am currently working with protractor version 4.0.4 and I'm encountering an issue where I cannot clear a date input field. It seems like Chrome is introducing some extra controls that are causing interference. You can find further details about Chro ...

Tips on safeguarding your templates while working with SailsJS and AngularJS

I am currently working on a web application using SailsJS and AngularJS. My project requires session management to track user login status. I came across this helpful tutorial and implemented an index.ejs view located in the /view folder. Within the index. ...

Combining ng-repeat and ng-model in a controller

Hello everyone, I'm a newbie here and also new to AngularJS. I am currently working on creating a form using ng-repeat, but I'm struggling to understand some of the angular concepts. Here is the JS in my controller: $scope.myCurrentAssets = { ...

Utilize the ng.IFilterService interface within a TypeScript project

I am facing an issue with a .ts file that contains the following code: module App.Filters { export class SplitRangeFilter implements ng.IFilterService { static $inject = ['$filter']; public static factory(): Function { ...