Connect Angular Material by chaining together md-select elements from arrays and array of form inputs

I am encountering a challenge with combining chains in Angular Material. I aim to transition from this linked solution on jsfiddle to using md-select and md-option in Material.

How should it function? It's quite simple. Here's the scenario:

  1. Select Manufacturer in the first md-select.
  2. Select Model in the second md-select.
  3. Retrieve price from the array and display it in a text input.

Array structure:

Manufacturer
-Models
--Model (model name and price)

Another aspect I'd like to achieve is creating an array from these form inputs as follows:

master = [
  {
    "product": {
      "name": "1936 Harley Davidson El Knucklehead",
      "price": 24.23
     }
  }
]

Can anyone assist me with this? Provide guidance or demonstrate how I can implement it. Your help will be greatly appreciated.

Best Regards

Answer №1

Please take a look at the following code snippet featuring Angular Material:

var app = angular.module('app', ['ngMaterial']);

app.controller('SelectController', function ($scope) {
        // Data extracted from KnockoutJs cart example
        $scope.sampleProductCategories = [
          {
            "name": "Classic Cars",
              "products": [
              {
                "name": "1948 Porsche 356-A Roadster",
                  "options":[
                      {"value": "Color",
                       "options":[
                      {"value": "Red"},
                      {"value":"Black"}
                      ],                                  
                   },
                      {"value":"Seats",
                      "options":[
                      {"value": "Leather"},
                      {"value":"Cloth"}
                      ],    
                      
                      },
                      
                     {"value":"Warranty",
                      "options":[
                      {"value": "2 Year"},
                      {"value":"3 Year"}
                      ],    
                      
                      } 
                      
                  ],
                "price": 53.9
              },
              {
                "name": "1948 Porsche Type 356 Roadster",
                "price": 62.16
              },
              {
                "name": "1949 Jaguar XK 120",
                "price": 47.25
              }
            ]
            
          },
          {
            "name": "Motorcycles",
            "products": [
              {
                "name": "1936 Harley Davidson El Knucklehead",
                "price": 24.23
              },
              {
                "name": "1957 Vespa GS150",
                "price": 32.95
              },
              {
                "name": "1960 BSA Gold Star DBD34",
                "price": 37.32
              }
            ]
            
          },
          {
            "name": "Planes",
              "products": [
              {
                "name": "1900s Vintage Bi-Plane",
                "price": 34.25
              },
              {
                "name": "1900s Vintage Tri-Plane",
                "price": 36.23
              },
              {
                "name": "1928 British Royal Navy Airplane",
                "price": 66.74
              },
              {
                "name": "1980s Black Hawk Helicopter",
                "price": 77.27
              },
              {
                "name": "ATA: B757-300",
                "price": 59.33
              }
            ]
            
          }
        ];
    });

angular.bootstrap(document, ['app']);

               
<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/angular_material/1.1.0/angular-material.min.css"/>

<!-- Angular Material requires Angular.js Libraries -->
  <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular.min.js"></script>
  <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular-animate.min.js"></script>
  <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular-aria.min.js"></script>
  <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular-messages.min.js"></script>

  <!-- Angular Material Library -->
  <script src="http://ajax.googleapis.com/ajax/libs/angular_material/1.1.0/angular-material.min.js"></script>
    
<div ng-controller="SelectController">
    <div layout="row">
  <md-input-container style="margin-right: 10px;">
        <label>Catgory</label>
        <md-select ng-model="category">
          <md-option ng-repeat="c in sampleProductCategories" ng-value="c">{{c.name}}</md-option>
        </md-select>
      </md-input-container>
  
  <md-input-container style="margin-right: 10px;">
        <label>Product</label>
        <md-select ng-model="categoryItem">
          <md-option ng-repeat="p in category.products" ng-value="p">{{p.name}}</md-option>
        </md-select>
      </md-input-container>
  
  <md-input-container style="margin-right: 10px;">
        <label>SubChild</label>
        <md-select ng-model="subChild">
          <md-option ng-repeat="o in categoryItem.options" ng-value="o">{{o.value}}</md-option>
        </md-select>
      </md-input-container>
  
  <md-input-container style="margin-right: 10px;">
        <label>Level4</label>
        <md-select ng-model="level4">
          <md-option ng-repeat="o in subChild.options" ng-value="o">{{o.value}}</md-option>
        </md-select>
      </md-input-container>
 
</div>
  <hr />
  category={{category.name}}<br/>
  product={{categoryItem.name}}<br/>
  subChild={{subChild.value}}<br/>
  level4={{level4.value}}<br/>
    
</div>

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

What is the best way to retrieve the current value of a React useState hook within a setInterval function while using Highcharts

import Error from 'next/error' import React, { useState, useEffect } from 'react' import Highcharts from 'highcharts' import HighchartsReact from 'highcharts-react-official' function generateChart() { const [co ...

Is there a way to extract the content length from the raw DraftJS data?

I have a system where I am storing the data from my DraftJS editor in my database as a JSON string by passing it through convertToRaw(editorState.getCurrentContent()). For example, this is how the stored data looks like in the database: {"blocks": [{"key ...

The addEventListener feature in Bootstrap 5.2.3 seems to be malfunctioning

I have a sample page with a Bootstrap dialog being returned from the server. I want to display it inside a DIV. However, when clicked, I receive this error: Uncaught TypeError: myModal.addEventListener is not a function It points to this line of code: ...

Designing versatile buttons with HTML

When accessing the website on a phone, I have trouble seeing and tapping on these two buttons because they are too far apart. I would like to change it so that once a file is selected, the 'choose file' button will be replaced by the upload butto ...

The function 'toBlob' on 'HTMLCanvasElement' cannot be executed in react-image-crop because tainted canvases are not allowed to be exported

Currently, I am utilizing the react-image-crop npm package for image cropping purposes. Everything works perfectly when I pass a local image as props to the module. However, an issue arises when I try to pass a URL of an image fetched from the backend - th ...

Issue with the Bootstrap carousel jQuery plugin

Hi everyone, I need some help with creating a multiple items bootstrap carousel. I keep getting an error message that says "#Carousel".carousel is not a function TypeError: "#Carousel".carousel is not a function. Can anyone guide me on how to fix this issu ...

displaying and concealing elements with jquery

Is there a way to hide a div if the screen size exceeds 700px, and only show it when the screen size is less than 700px? Below is the jQuery code I'm attempting to use: jQuery(document).ready(function() { if ((screen.width>701)) { $(" ...

What makes arrays declared as "char word[5] = "hello" flexible in comparison to strings declared as a pointer (char *word = "hello")?

My understanding was that: char word[5] = "hello"; is equivalent to char *word = "hello"; due to the fact that arrays always decay into pointers, right? ...

Router-outlet not displaying any content

I'm encountering an issue with using routing inside mat-drawer. Strangely, it results in a blank page. However, when I comment out the router-outlet element, the page displays correctly. Does anyone have insight on why this is happening? P.S. When I ...

Is it possible for the jquery in index.html to retrieve variables stored in the connected controller?

Utilizing jQuery for a dropdown navigation that dynamically adjusts its CSS properties based on the user's login status within the application. The visibility of links in the navigation is determined by the boolean value of the loginStatus variable se ...

Simulate a failed axios get request resulting in an undefined response

I'm having an issue with my Jest test mock for an axios get request, as it's returning undefined as the response. I'm not sure where I might be going wrong? Here is the component with the axios call: import {AgGridReact} from "ag-grid- ...

Including jQuery in an Angular project generated with JHipster

I am facing a challenge with integrating jQuery into my Jhipster Angular project as a newcomer to Jhipster and Angular. My goal is to customize the theme and appearance of the default Jhipster application, so I obtained a theme that uses a combination of ...

What is the best way to reset the state to null when the input field is blank?

After setting some inputs with a state of null, I noticed that when the end-user types something and then deletes it all, the input reverts back to an empty string. How can I adjust the state so that it returns to null? I seem to be encountering an issue ...

What is the process to access array elements in AngularJS?

When coding in HTML, <select class="element-margin-top" ng-model="vm.selectedRole" ng-options="(roleName,enabled) in vm.roleNames"> <option value="">All Roles</option>{{vm.roles[0]}} </select> I am tryin ...

Send a signal to a space, leveraging the distinct characteristics of each socket as input parameters

I am looking to send a function to a group of sockets, with each socket receiving the function along with a specific parameter unique to that particular socket. In my coding scenario, this distinctive variable represents the player's number. As socke ...

Injecting Custom HTML Tag through JavaScript on Page Load in Google Tag Manager

When attempting to inject events into the data layer upon loading DOM pages on my website, I encountered an issue. Since I lack access to the website code and developers are reluctant to make additions, I decided to implement this through a custom HTML tag ...

Verify the presence of a JSON object within the session storage using JavaScript

I'm currently developing a website where some data is stored within the session. In the initial page, I need to verify if the JSON object already exists in the session. Below is the code snippet that's causing issues: var passedTotal = JSON.par ...

Discover the steps to handle parameters received in a $resource GET request

When working in the controller: MyService.get({queryParameter:'MyQueryParameter'}).$promise.then(function(result){ return result; }; Within my service, I include: $resource('/api/path',{ queryParameter: (function manipulate(quer ...

Tips for transferring data from a parent component to a child component in React?

Recently, I've been restructuring a small application that was initially confined to one file by breaking it into its own separate components. Right now, I have a child component called UsersTable which I am displaying within the parent component User ...

Deserialization does not support the use of Type System.Collections.Generic.IDictionary for an array

An issue might arise when trying to call a page method using a manually created JQuery.ajax request. The deserialization process is handled by .NET and not within the user's code. Javascript: MyParam = []; ... $.ajax({ type: 'POST', ...