Utilizing AngularJS for manipulating a multidimensional array with interdependent select boxes

I have a unique collection of product names along with their respective versions stored in a multidimensional array. My goal is to design an interactive interface that allows users to first select a product from a drop-down menu and then choose the version number from a second drop-down menu. The second menu should dynamically display only the version numbers associated with the product selected in the first menu.

Below is a snippet of my multidimensional array:

[Object]0: 
         name: "Product 1"
         versions: [Array]0:
                           number: "1.0"
                           number: "1.5.2"
        1: 
         name: "Product 2"
         versions: [Array]0:
                           number: "0.0"
                           number: "0.5"

Users can opt for multiple products selections, leading me to create an array to store these selections.

The setup of my controller is as follows:

app.controller('mainController', function ($scope) {

    $scope.products = [{id: 1, name: '', versions: []}];

  $scope.packages = [];
  $scope.packages[0] = { id: 1, name: 'Product 1', versions: [{number: 1.0}, {number: 1.5}, {number: 2.0}]};
  $scope.packages[1] = { id: 2, name: 'Product 2', versions: [{number: 0.1}, {number: 0.2}, {number: 0.3}]};

    $scope.addProduct = function(){
    var id = $scope.products.length + 1;
    $scope.products.push({id: id, name: "", version: []});

  };
});

The select boxes implementation using AngularJS looks like this:

    <div  ng-repeat="product in products">
       <label>Product</label>
       <select ng-model="product.product" ng-options="package.name for package in packages" class="form-control"></select>

       <label>Version</label>
       <select ng-model="product.versions" ng-options="version.number for version in product.versions" class="form-control"></select>
</div>
<button ng-click="addProduct()">Add Product</button>

I attempted to link the version select box with the currently selected product by setting up ng-options accordingly, but encountered issues.

You can view my current progress on jsFiddle here: http://jsfiddle.net/rkyu4rjq/

Your suggestions on effectively connecting the version selection with the chosen product would be greatly appreciated.

Thanks in advance!

Answer №1

While I may not be meticulously monitoring which version is selected for each product, I have adjusted the relative select options for you.

If you need further assistance, feel free to check out a solution here. Hopefully, this will help steer you in the right direction!

Answer №2

I overlooked something quite straightforward.

When binding the version selection box, I should have utilized

version.number for version in product.product.versions
instead of
version.number for version in product.versions

For a functional example, check out this jsFiddle: http://jsfiddle.net/rkyu4rjq/2/

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

How to work with multiple selections in React material-ui Autocomplete

I'm currently using Material-ui Autocomplete in multiple selection mode. My goal is to retrieve all the selected values when the form is submitted. Although I found a solution on how to get individual values through the onChange event handler from thi ...

How to update JSON file on server using JavaScript

I am encountering an issue that has been discussed quite frequently. Despite trying various solutions, none have proven effective so far. The problem lies in my JavaScript file where I have some data that I need to append to an existing .json file on my s ...

Utilizing Directives to Embed Attributes

My current challenge involves changing the fill color of attributes in an in-line SVG using Angular and TypeScript. The goal is to have the SVG elements with a "TA" attribute change their fill color based on a user-selected color from a dropdown menu. Howe ...

Discovering the method to retrieve files from the public folder via URL in next.js

I am working on a Next.js project where users have the ability to upload images. These images are stored in the public directory. Is there a way for me to access these files using a URL structure like mydomain.com/public/coverimg/image.jpg? Below is my AP ...

What is the best way to ensure that a link fragment scrolls to the top of the page in Angular?

Having trouble with link fragments in my Angular single-page-app: <a href="/#/search">Search</a> When clicking this link, it takes me to the right page but keeps my scroll position. I want it to scroll to the top of the page so I'm curre ...

Refresh the homepage of a website using React Router by clicking on the site's logo from within the Home component

In most cases, when you click on a website's logo while on the homepage, it typically reloads the page. However, with React, clicking on the logo while already on the Home component does not trigger a reload. Is there a way to work around this issue? ...

Extracting Data from JSON Structures

I am struggling with extracting data from a JSON string that looks like this: var txt= '{“group”: [ {“family1”: { “firstname”:”child1”, “secondname”:”chlid2” }}, {“family2”: { ...

Refreshing pane content within Kendo UI Splitview

I am utilizing the Kendo UI splitview. In my setup, I have configured one pane on the left for navigation and another pane on the right for content display. The left pane contains 4 navigation links structured as follows: <div data-role="pane" id="si ...

Implementing Jquery after async ajax refresh in an asp.net application

My ASP.net page is heavily reliant on jQuery. Within this page, I have a GridView placed inside an UpdatePanel to allow for asynchronous updates. <asp:GridView ID="gvMail" runat="server" GridLines="None" AutoGenerateColumns="false" ...

Nested ui-view is not appearing as expected

I have a query about Angular UI-Router and its ui-views functionality. I am facing an issue where only the ui-view with the name "languages" is displaying, despite declaring three ui-views inside another one. Any assistance in resolving this would be highl ...

Mapping a list with sections can easily be achieved by breaking down the elements

I'm facing an issue with listing array data under sections using .map in React. I know how to use .map to list the entire array, but struggling to list each item under its respective section. Currently, I have to use .map separately for each section. ...

The issue arises when trying to call a JavaScript function that has not been properly

Initially, write code in resources/js/app.js file function button1Clicked(){ console.log('Button 1 is clicked'); } Next, include the following code in testing.blade.php file <!DOCTYPE html> <html> <head> <meta name="cs ...

Ways to invoke a function from a service in AngularJS?

My focus is on learning AngularJS, and I recently came across this particular section. However, I am struggling to comprehend why the batchLog service is being invoked in this manner! Although the batchLog service is designed with only one method named lo ...

Working with JSON data in AngularJS: A guide to loading data onto a scope variable

I am in the process of developing my personal website where I want to be able to update content easily without having to mess with the HTML code. My approach involves loading and updating a JSON file to achieve this, however, I am currently struggling with ...

Gallery Pagination using JQuery is not working properly

I am experimenting with the jquery easy paginate plugin on a separate page of my Blogspot. The images are displaying properly, but the pagination and CSS are not showing up. I need to adjust my CSS to make this jQuery pagination work correctly. Can someone ...

Updates made in the angular controller are not reflecting on the HTML page

I am facing an issue with displaying a "no internet access" button in my app when the user is offline and the required object is not available in the cache. Here is the HTML code: <div class="VideoPlayerController video-player-controller"> <span ...

How to incorporate a JavaScript file into a Handlebars template

I am having trouble receiving calls to my JavaScript file. What could be the issue? Using MVC. Here is the code in view file, file.hbs: <div class="container"> <h2 onClick="test()">Title</h2> {{>list}} </div> <script sr ...

Splitting an array into two equal halves in Swift: A step-by-step guide

I have a single array that I want to split into two separate arrays; the first containing the first half of the elements and the second containing the second half. Here is my attempted code: let totalArray = [20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10] v ...

JavaScript implementation of a carousel slider with responsive design

I have successfully implemented a feature carousel slider using the jquery.featured.carousel.js file along with some CSS. Here is the jsfiddle link to my project: LINK. When running this code on localhost, I noticed that I need to refresh the page every ...

What is the best way to combine the elements within an array with the elements outside of the array in order to calculate their sum?

The goal is to create a function that determines the winner using two input integers. The function should return the first input if it is greater than the second input. function determineWinner(a, b) { let result = [] for (let i = 0; i < 3; i++) ...