Retrieving specific information from a JSON file using Angular

I am looking to sort students by their class and then display them in an HTML format.

Check out the code snippet here: http://plnkr.co/edit/GG9EYmPlIyD4ZM0ehaq6?p=preview

.html code:

<div ng-controller="ClassController">
          <table>
            <thead>
              <tr>
                <th>Name</th>
              </tr>
            </thead>
            <tbody>
              <tr ng-repeat="student in students">
                <td>{{ student.name }}</td>
              </tr>
            </tbody>
          </table>
    </div>

.js code:

var app;

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

app.controller('ClassController', [
  '$scope', '$http', function($scope, $http) {
    $scope.students = [];
    return $http.get('data.json').then(function(result) {
      return angular.forEach(result.data, function(item) {
        return $scope.students.push(item);
      });
    });
  }
]);

You can locate the .json file on this link.

Answer №1

Presented below is the controller:

let app;

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

app.controller('ClassController', [
  '$scope', '$http', function($scope, $http) {
    let filterType = 'apple';
    $scope.students = [];
    return $http.get('data.json').then(function(result) {
      $scope.students = result.data.filter(function(item) {
        return item.classroom.school.name.toLowerCase().indexOf(filterType) > -1;
      })
    });
  }
]);

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

The Express error is thrown when attempting to read the property 'get' of an undefined value

Currently in the process of organizing my routes in a separate directory: server.js: var express = require("express"), parse = require("body-parser"), db = require("mysql"), mailer = r ...

What is the best way to verify a password's strength with Joi so that it includes 2 numbers, 2 special characters, 2 uppercase letters, and 2 lowercase letters?

Is there a way to achieve this using Joi? For instance: Joi.string() .required() .min(8) .max(16) .pattern(/(?=(?:.*[a-z]){2,16}).+/) .pattern(/(?=(?:.*[A-Z]){2,16}).+/) .pattern(/(?=(?:.*[0-9]){2,16}).+/) .pa ...

Ensure that PHP form validations are checked when submitting the form using jQuery

I have created a form in HTML with basic verification. Here is the code: <form class="" action="submit/save" method="POST" enctype="multipart/form-data" id="submit_form"> <input class="form- ...

Angular - Comparing the advantages of using a generic image service versus specialized image services tailored to specific resources such as users, images, and

I'm struggling to grasp a particular issue and could use some assistance. Currently, I am working on an app in Rails utilizing Angular.js as my client-side MVC framework. One of the components I have created is a service that allows Angular to fetch ...

setTimeout executes twice, even if it is cleared beforehand

I have a collection of images stored in the img/ directory named 1-13.jpg. My goal is to iterate through these images using a loop. The #next_container element is designed to pause the loop if it has already started, change the src attribute to the next im ...

Troubleshooting: Issues with AngularJS ng-show and two-way binding functionality

After delving into AngularJS and seeing similar inquiries, I have come across answers that just don't seem to work for me. Instead of jumping into those discussions, I decided to create a new one for myself. My focus is on developing a demo app where ...

Attempting to display a base-64 encoded image in a Next.js application

After following this method, I successfully uploaded my image as a string: const [image, setImage] = useState(""); //------------------^^^^^^^^^^^^^^^^------------------------- const handleImage = (e) => { ...

A guide on utilizing the React Suite range slider in rsuite

Hello there, I recently started working with the UI framework rsuite (React suite) and everything was going smoothly until I tried to use the Range slider component's API. Unfortunately, I am facing some issues with the labels and tooltips not display ...

Attempting to populate the database with information from a JSON array

My code is designed to work in the following way: An HTTP post request (in an AsyncTask class) is sent to a MySQL database using PHP to retrieve JSON encoded data. The JSON encoded data from each row is fetched one by one in the dbCodeHelper class, which ...

AngularJS - Interactive web pages powered by controller scripts

I am experiencing an issue with my AngularJS page where a popup is not displaying correctly. The popup HTML is fetched dynamically from the server using an AJAX request, including a new controller and relevant AngularJS code. The problem arises when the ch ...

How can you extract a JSON object from a JSON array based on a key that is associated with a specific JSON

Is it possible to retrieve a specific JSON object from an array of JSON objects based on a condition? [ { id: 1, name: "larry" }, { id: 2, name: "curly" }, { id: 3, name: "moe" } ] For example, if I want to extract the object where name is equal to " ...

Having trouble utilizing the mongoimport feature in MongoDB

Having trouble importing data (.json) through mongo shell. Here's the error message I received... mongoimport -d ceshi -c datasample user/civycheng/desktop/sample.json 2017-01-18T21:28:17.739+0800 E QUERY [main] SyntaxError: missing ; before s ...

Upon making an Ajax call in F# MVC, the client is receiving an empty object in the Json Result

I am currently working on an F# MVC application and I'm facing an issue with integrating an AJAX call to an endpoint that is supposed to return a list of people, which will then be displayed in a table. type Person(credits:int, name:string, serverDat ...

Trouble updating Express-Session cookie

Hello, I have been working with express.js and have encountered an issue with express-sessions. Here is how my express session is configured in index.js: app.use( session({ secret: 'idegas', resave: false, saveUninitialized: false, cook ...

Creating an overlay button within various containing divs requires setting the position of the button

Tips for overlaying a button on each HTML element with the class WSEDIT. My approach involves using a JavaScript loop to identify elements with the CSS class WSEDIT, dynamically create a button within them, and prepend this button to each element. Below ...

Changing Json Object to Array in JavaScript

My method of converting an Array in php involves using json_encode and sending it to JS. This is the json I need to convert: [{"category_id":4},{"category_id":2},{"category_id":3}] I am aiming to convert it to: ['3','2','4&apos ...

Receiving the result as well as encountering undefined initially during AJAX request

I have a dropdown menu, and when a user selects an option, an AJAX call is made to retrieve and display data based on the selected value. If the dropdown value is 2, it triggers the AJAX request. However, I am encountering two issues: https://i.sstatic.n ...

Even though there is an error in the line of code saying "Error in render: RangeError: Invalid array length", it still manages to perform its intended task

When trying to round a float number and display stars equal to that rating number, the code works as expected. Surprisingly, it also generates an error Error in render: "RangeError: Invalid array length" <p>Rating: <i v-for='n in Math.round( ...

Transmitting the Ctrl+A key combination to an element

My current testing framework is protractor, which I utilize for conducting end-to-end tests in Angular. When it comes to inputting text into an element, I typically use: element(by.model('myModel')).sendKeys('Test'); However, I am cu ...

Execute the function when the element comes into view

Is there a way to create a function that will automatically attach itself to an element when it comes into view? I have multiple elements on my page - element1, element2, and element3. Instead of using a large if statement, I would like to write a functio ...