Two selection options controlling filters. The first choice influences the data shown in the second filter

I've been struggling for hours to figure out how to build this functionality. My code seems to be getting close, but I'm having trouble passing my selection from the HTML back to the controller.

Here's my Angular code:

 var Categories = Parse.Object.extend("bizCategories");

        $scope.categories = [
            {category: "Food and Drink"},
            {category: "Healthcare"},
            {category: "Recreation"},
            {category: "Service"},
            {category: "Shops"},
            {category: "Travel"}
        ]

        $scope.setPrimary = function (category) {
            $scope.offer.primary = category;
            $scope.secondaryCategories(category);
        };

        $scope.secondaryCategories = function(category){
            var Secondary = new Parse.Query(Categories);
            Secondary.limit(1000);
            Secondary.find({
                success:function(list){
                    $scope.SecondaryCategories = _.uniq(_.filter(_.map(list, function(item){
                        return{primary: item.get('secondary')
                        }}
                    )),'primary',true)
                    console.log($scope.SecondaryCategories);
                },
                error: function(error){alert("A mistake was made.");
                }
            })
        }

This is how my html looks:

  <select id= 'primary' ng-model="offer.primary" ng-options="XXX.category for XXX in categories"
                ng-click= setPrimary(category)>
            <option value="">Primary Business Category</option>
        </select>

        <select id= 'secondary' ng-model="offer.secondary" ng-disabled="!offer.primary"
                ng-options="XXX.category for XXX in SecondaryCategories" ng-click="setSecondary('XXX.category')">
            <option value="">Secondary Business Category</option>
        </select>

In my html, you'll notice that I used XXX as a placeholder for ng-options. This decision was arbitrary, as I couldn't find a specific requirement in the tutorials. Additionally, I am encountering an issue where the data from the first dropdown is not getting passed to the second dropdown function (category) and is showing up as undefined.

The source of my data:

Answer №1

It appears that you are attempting to update the other dropdown when a certain option is selected. In this case, it would be more appropriate to utilize ng-change instead of ng-click.

Consider trying:


    <select id='primary' ng-model="offer.primary" ng-options="XXX.category for XXX in categories"
            ng-change="setPrimary(category)">
        <option value="">Primary Business Category</option>
    </select>

    <select id='secondary' ng-model="offer.secondary" ng-disabled="!offer.primary"
            ng-options="XXX.category for XXX in SecondaryCategories" ng-change="setSecondary('XXX.category')">
        <option value="">Secondary Business Category</option>
    </select>

Since your ng-model is already linked to offer.primary, you can eliminate the need for $scope.offer.primary = category; as the ng-model should have already been updated.

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

Monitor the fullscreenChange event with angularJs

Utilizing a button to activate fullscreen mode for a DOM element using the fullscreen API is functioning correctly. The challenge arises when exiting fullscreen mode, requiring the listening for the fullscreen change event in order to resize the DOM elemen ...

Enhancing User Experience with Load Indicator during State Changes in React Native

I'm facing an issue where the page for displaying a single item is loading slowly - I need to delay the page from loading until the object has changed. After checking the navigation params through console log, I can see that the id changes when each b ...

Instructions on obtaining a distinct daily array from the weather API array that provides a detailed 5-day weather report every 3 hours

Seeking assistance with my weather app development on React using axios with the openweathermap.org API. Currently stuck on getting data formatted in a specific way from the 5-day forecast it provides, totaling 40 reports over the 5 days. The API response ...

Encountering a "Module not found" error when trying to integrate NextJs 13.4 with React Query and the

I encountered some issues while working on a NextJs application that utilizes App Router. The problem arose when we decided to switch from traditional fetching to using React Query in server components. To address this, I created a file called api.ts withi ...

Utilize localstorage with Redux and Next.js to initialize the state

Having an issue when attempting to populate the initial state from local storage in order to create a store. I encountered the following error message: ReferenceError: localStorage is not defined Here is my store configuration: const store = createStore(r ...

No styles are appearing on a specific element after running a specific jQuery function on that element within a Vue page

I recently integrated JQuery-AsRange (https://github.com/thecreation/jquery-asRange) into my vue.js project. Everything functions as expected within the .vue page, however, I am facing an issue with css styling not being applied. The css styles should be ...

Generate a flexible JSON array in VB.NET

Looking to generate a flexible array that can be converted into a JSON array for visualization with Morris charts. The usual approach in VB.NET is as follows: Dim xArray(2) xArray(0) = New With {Key .TradingDay = "Day1", .Seller1 = 1500, .Seller2 = 160 ...

The access to Angular.js has been denied with a 403 Forbidden error

I'm seeking assistance with a specific issue I am facing. For an upcoming job interview, I have been tasked with cloning and understanding a project - Repository HUBTaxi-Server. I am struggling as this project is larger than what I am used to working ...

The Vue3 counterpart of vNode.computedInstance

I'm currently in the process of upgrading my app from Vue2 to Vue3, but I've hit a roadblock when it comes to dealing with forms. For my form elements, such as FormInput, FormTextArea, and FormCheckbox, I have been using Single File Components ( ...

Incorporating a JavaScript npm module within a TypeScript webpack application

I am interested in incorporating the cesium-navigation JavaScript package into my project. The package can be installed via npm and node. However, my project utilizes webpack and TypeScript instead of plain JavaScript. Unfortunately, the package is not fou ...

Create a unique filter in an ng-repeat directive that allows users to personalize the data displayed

Is there a way to create a custom filter that hides the inventory column for Color Pencil Special on October 2nd, 2017 in the view? The inventory for Color Pencil Special is dependent on the inventory of regular Color Pencils, which are stored somewhere e ...

Utilize JQuery to modify hover state when focused and restrict tab navigation to specific areas on the keyboard

I'm currently working with a JQgrid in one of my projects (Check out the JFiddle here). and I have some specific requirements: 1.) I want the save & cancel button to become highlighted when a user tabs to it, similar to how it behaves on mouse ov ...

Remove click event listeners from a specific element within a group of selected elements using D3

In my D3 class, I have a group of selectors that I want to remove the click event from: d3.selectAll('.selectors').on('click',function(){ //Remove the click event from the currently clicked element. }); I'm encountering tw ...

Validation of user input using jQuery

Trying to ensure that the form inputs are not empty has been a challenge. Despite using jQuery for input validation, it seems that the inputs are not being properly checked. Strangely, the submit button still allows the form to be submitted: <form id=" ...

Eliminating the unwanted line breaks that are automatically inserted when integrating JavaScript with HTML code

Hey everyone, I'm new to web development and could really use some assistance. I am currently working on a website that shows the weather forecast for the next four days, using SimpleWeather.js. In my JavaScript, I want to display the High & Low temp ...

By utilizing the window.history.back() function, it takes me four clicks to navigate back one page

I find it peculiar that my back button is using the JS method window.history.back() to return to the previous page. However, I have noticed a strange behavior with this button. When I click on it before the entire document loads, it functions as expected a ...

Issue with Component not displaying properly upon refreshing

I'm currently using react.js and I have a button with an onClick event. My goal is to reload the page after clicking the button and then display a specific component on the page. However, I've encountered an issue where the component doesn't ...

Is it possible to modify the color of a span element within a select dropdown?

Is there a way to change the color of a span to red within a select option in js fiddle? I am trying to modify the color of <span class="myError">This is a required field.</span> to red https://i.sstatic.net/nRF2c.png select{ color: green; ...

Tips for passing a URL variable into an Ajax script to prefill a form input in a search field

I have a website with a search feature that dynamically queries a database as you type in the search field, similar to Google's search suggestions. This functionality is achieved through AJAX. As the results appear on the page while you enter your se ...

Frontend Navigation with Role-Based Display

I am currently working on a project with a REST API served by Spring Boot, using JWT tokens generated on the backend server. These tokens are then passed to the frontend application built with AngularJS and HTML5. My goal now is to customize the navigation ...