ng filtering with a controller-defined scope

I am currently working on a webpage with AngularJS and I am looking to implement some filters on the site.

Here is the HTML code I have:

<div ng-repeat="data in datas | filter:{area:course} | filter:{subject:subFilter} | filter:{city:cityFilter}">
                        <h5><span class="text-warning">#</span>&nbsp;{{data.intrest}}</h5>
                        <div class="row">
                            <div class="col-xs-3">
                                <h5 class="text-info">Name</h5>
                                <p>{{data.name}}</p>
                            </div>
                            <div class="col-xs-3">
                                <h5 class="text-info">Subject</h5>
                                <p>{{data.subject}}</p>
                            </div>
                            <div class="col-xs-3">
                              <h5 class="text-info">Address</h5>
                                <p>{{data.city}}, {{data.state}}</p>
                            </div>
                            <div class="col-xs-3">
                                <a href="#/view"><button class="btn btn-warning" style="margin-top:10px;">View</button></a>
                            </div>
                        </div>
                        <hr>
                    </div> 

This is my controller file:

app.controller('mainController',['$scope','$http','$routeParams',function($scope,$http,$routeParams){
$http.get('assets/newtab.json').success(function(data){
    $scope.department = data;
    $scope.whichItem = $routeParams.itemId;
    $scope.course = $scope.department[$scope.whichItem].course;
});
$http.get('assets/engineering.json').success(function(data){
    $scope.datas = data;
    $scope.whichItem = $routeParams.itemId;
    $scope.optItem = [
        {title:"Random",value:" "},
        {title:"Question Paper Setting",value:"Question Paper Setting"},
        {title:"Question Paper Passing Board",value:"Question Paper Passing Board"},
        {title:"Question Paper Post Auditing",value:"Question Paper Post Auditing"},
        {title:"Question Paper Evaluation",value:"Question Paper Evaluation"},
        {title:"Member for board of study",value:"Member for board of study"},
        {title:"Member for academic council",value:"Member for academic council"},
        {title:"Result passing board",value:"Result passing board"},
        {title:"Invigilation",value:"Invigilation"},
        {title:"Door Valuation",value:"Door Valuation"},
        {title:"Exam squad",value:"Exam squad"}
    ];
});
}]);

Snippet from engineering.json and newtab.json:

engineering.json

[{"id":"1","name":"Sam","dob":"","age":"21","gender":"","department":"16\/03\/1995","area":"Nuclear Medicine Technology Course","institution":"Park College of Technology ","city":"Tiruppur","state":"Tamil Nadu","intrest":"Question paper evaluation","mobile":"","email":"[email protected]","password":"jaya","subject":"Computer Networks"},{"id":"2","name":"jaya","dob":"","age":"21","gender":"","department":"16\/03\/1995","area":"Nuclear Medicine Technology Course","institution":"Park College of Technology ","city":"","state":"","intrest":"","mobile":"","email":"[email protected]","password":"jaya","subject":""},{"id":"3","name":"jaya","dob":"","age":"21","gender":"","department":"16\/03\/1995","area":"Nuclear Medicine Technology Course","institution":"Park College of Technology ","city":"","state":"","intrest":"","mobile":"","email":"[email protected]","password":"jjjj","subject":""}]

newtab.json

[{"Department":"Arts","course":"B.A. English"}, {"Department":"Arts","course":"B.A. English (Computer Applications)"}, {"Department":"Arts","course":"B.A. Economics"}, {"Department":"Arts","course":"B.A. History"}, {"Department":"Arts","course":"B.A. Political Science"}, {"Department":"Arts","course":" B.A. Tamil  "}, {"Department":"Arts","course":"B.Lit. Tamil  "}]

In my controller, on line 5, I retrieve the value of the clicked item and store it in a variable named "course". I need this variable to properly filter the results in my HTML code. Although I have written filtering syntax in my HTML file, it does not produce the desired result.

Answer №1

Upon further examination, it appears that the .area attribute in engineering.json does not match with the .course attribute in newtab. In each instance of engineering.json, the area is labeled as "Nuclear Medicine Technology Course", resulting in no matches regardless of the department selected from newtab.

You can view a working example on JSFiddle here. I have modified Sam's .area to demonstrate that the filter is functional (and removed extra filters for clarity, although they should still be operative).

An additional issue could be how the data retrieved from $http is typically accessed, as shown below:

$http.get(url).then(function(result) {
    $scope.variable = result.data;
}

This is due to the fact that the object returned by the $http call stores the data within the .data attribute. This is something worth investigating as well.

EDIT: As mentioned by @mzulch in the comments, your $http syntax is accurate for angular versions earlier than 1.5.

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

Updating the text of a Mat-Label dynamically without the need to reload the page

In my application, there is a mat-label that shows the Customer End Date. The end date is fetched initially through a GET request to an API. Let's say the end date is 16-05-2099, which is displayed as it is. There is also a delete button implemented f ...

Retrieve the values and IDs for every span element within the form

I have been attempting to retrieve all span elements within a form and make them editable input text fields. Once you click away, they should revert back to span elements. I will provide a live example in the attached fiddle. I tried my hand at it, but the ...

Having trouble with installing Angular JS on my computer

On my machine, I have successfully installed node.js version v0.12.0. However, when attempting to run sudo npm install, I encountered the following errors: npm ERR! install Couldn't read dependencies npm ERR! Darwin 14.0.0 npm ERR! argv "node" "/usr/ ...

What causes a component to not update when it is connected to an Array using v-model?

Array1 https://i.stack.imgur.com/cY0XR.jpg Array are both connected to the same Array variable using v-model. However, when changes are made to Array1, Array2 does not update. Why is this happening? Process: Upon examining the logs, it can be observed th ...

Prevent request timeouts in Express.js

I am currently experimenting with methods to terminate client requests that are prolonging excessively, thus depleting the server's resources. After reviewing various sources (referenced below), I attempted a solution similar to the one suggested here ...

Encountering issues due to overriding JSON.stringify

Recently, I developed a customized function for JSON.stringify implementation as shown below: JSON.stringify = function (item, replacer, space) { return JSON.stringify(item, replacer, space); } This modification led to multiple errors in AngularJS w ...

Jest came across a surprising token that it wasn't expecting while working with React and TypeScript in conjunction with Jest and React-testing-L

An unexpected token was encountered by Jest while using React and TypeScript along with Jest and React-testing-Library. Error occurred at: E:\Git\node_modules@mui\x-date-pickers\internals\demo\index.js:1 ({"Object." ...

Presenting charts using flot library

Having Trouble: My x-axis ticks are not displaying correctly on the graph - only the first tick is showing at the far right and no bars are being plotted. I believe I might be overlooking something obvious. Desired Fix Needed: Display all x-axis ticks and ...

Conceal a request URL within JavaScript through the use of Laravel/Ajax

I have an idea that I'm not sure is great or even feasible, but I really need to make it work. I am attempting to obfuscate a URL that needs to be used in a Javascript function as a parameter. This is what I currently have: <script> myFunction ...

Is there a way to eliminate duplicates from an array in JavaScript by utilizing a set and placing each element in an li tag? Here is the code

const numbers = [" 4", "9", "16"," 4", "9", "16"," 4", "9", "16"] <ul> {(<li> {[...new Set(numbers)]} </li>)} </ul> const numbers = [" 4", "9", "16"," 4", "9", "16"," ...

Hovering over a table cell triggers a popup in Angular

Inserted a class into <td><span class="only-show-on-hover"></span></td> CSS code for the class td span.only-show-on-hover { visibility: hidden; } td:hover span.only-show-on-hover { visibility: visible; } Code for dialog box < ...

Inquiries regarding scopes, node.js, and express

I struggle with understanding scopes and similar concepts in various programming languages. Currently, I am working on an express application where I take user input, query an arbitrary API, and display the results in the console. To interact with the REST ...

After the update, Material UI is causing a TypeError by throwing an error stating that it cannot read the property 'muiName' of an

After updating from "material-ui": "^1.0.0-beta.38" to "@material-ui/core": "^1.3.0", I made changes to imports, ran npm install, removed node_modules and even deleted package-lock.json. However, I continue to encounter the cryptic error message TypeError: ...

The table width is malfunctioning on Firefox when viewed in HTML

I have been puzzled by the fact that I am unable to adjust the width of the table th in Firefox browser to the smallest value. However, in Chrome browser, this functionality works perfectly. I simply want to divide the values of my td into three rows as sh ...

I. Discovering the Step-by-Step Guide on Retrieving User Information from Facebook Upon Generating App

I have set up a Facebook login for user registration on my website. However, I am only able to retrieve the profile name and user ID from Facebook. How can I access the email and other user information? Here is the data I am currently receiving from Faceb ...

Toggle visibility of cards using bootstrap

I've been attempting to show and hide a selection of cards in bootstrap, but I'm having trouble figuring it out. All the cards share the class "card," and my goal is to hide them all when a specific button is clicked. Below is my current code: ...

Using Highmaps in a VueJs application involves passing a state to the mapOptions for customization

I'm currently struggling with passing a vuex state to mapOptions in vuejs components. Here is the code snippet: <template> <div> <highcharts :constructor-type="'mapChart'" :options="mapOptions" class="map">&l ...

Building a stylish table using Material UI in ReactJS

I've been working on creating a dashboard using the Material UI minimized table. You can check out the documentation here: Material UI Tables However, I've encountered an issue that has me stuck for the past 5 hours. While I was able to successf ...

Assistance required for implementing JSON post functionality on EpiServer CMS versions above 8.0.0

We are currently implementing JSON posting to a site monitoring API across multiple EpiServer sites. The process works smoothly on our EpiServer CMS version 8.0.0 sites, but we encountered issues with versions 8.8.1 and above. Here is our successful worki ...

What benefits does sending data in JSON offer when using JQuery Ajax?

I created a form that sends the input values using ajax: var data = { id : $("#tabs").tabs('option', 'selected'), title : $('#name').val(), csrf_token : $("input[name=csrf_token]").val() }; Wh ...