NG-CLICK simple function in Angular not passing through variable - javascript

I have a defined a basic controller which has a function called setSector (at the bottom)
function AppCtrl ($scope) {
$scope.sectors =
{
"Sector": [
{
"sectorDesc": "London and South East",
"sectorCode": "LSE",
"SectorPPM": {
"Total": "10329",
"OnTime": "9195",
"Late": "1134",
"CancelVeryLate": "206",
"PPM": {
"rag": "A",
"text": "89"
},
"RollingPPM": {
"trendInd": "-",
"rag": "R",
"text": "83"
}
}
},
{
"sectorDesc": "Long Distance",
"sectorCode": "LD",
"SectorPPM": {
"Total": "1383",
"OnTime": "1159",
"Late": "224",
"CancelVeryLate": "68",
"PPM": {
"rag": "R",
"text": "83"
},
"RollingPPM": {
"trendInd": "+",
"rag": "G",
"text": "92"
}
}
},
{
"sectorDesc": "Regional",
"sectorCode": "REG",
"SectorPPM": {
"Total": "5348",
"OnTime": "4678",
"Late": "670",
"CancelVeryLate": "196",
"PPM": {
"rag": "R",
"text": "87"
},
"RollingPPM": {
"trendInd": "=",
"rag": "R",
"text": "87"
}
}
},
{
"sectorDesc": "Scotland",
"sectorCode": "SCO",
"SectorPPM": {
"Total": "2026",
"OnTime": "1861",
"Late": "165",
"CancelVeryLate": "41",
"PPM": {
"rag": "A",
"text": "91"
},
"RollingPPM": {
"trendInd": "+",
"rag": "G",
"text": "94"
}
}
}
]
}
$scope.currentSector = null;
$scope.setSector = function (sectorCode) {
$scope.currentSector = $scope.sectors[sectorCode];
};
}
which is called from my index file.
<html ng-app>
<head>
<title>PPM Viewer</title>
<script type="text/javascript" src="js/lib/angular.min.js"></script>
<script type="text/javascript" src="js/lib/angular-resource.min.js"></script>
<script type="text/javascript" src="js/controllers/app.js"></script>
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
</head>
<body>
<div class="container" ng-controller="AppCtrl">
<h1>PPM Dashboard</h1>
<ul>
<li ng-repeat="sector in sectors.Sector">
{{sector.sectorCode}} - {{sector.sectorDesc}}
</li>
</ul>
<p>
Current sector : {{currentSector.sectorDesc}}
Current code : {{currentSector.sectorCode}}
</p>
</div>
</body>
When you click on the sectors in the UL list, the name of the clicked on sector is supposed to appear at the bottom, but for some reason the function is not passing back anything.
I am sure it is due to needing to specify the $scope.sectors.Sector somewhere in the function. but nothing I try will work. I am still quite new to angular and scopes, and think that I need to force the scope to go to a deeper level.. just not sure how
Can anyone advise how I can make the function... function! =)
Many thanks

So neater way of doing this would be to use the $index functionality of Angular. Try changing your code to:
HTML
<ul>
<li ng-repeat="sector in sectors.Sector">
{{sector.sectorCode}} - {{sector.sectorDesc}}
</li>
</ul>
JS
$scope.setSector = function (index) {
$scope.currentSector = $scope.sectors.Sector[index];
};
$index in an ng-repeat states the index of an item in an array, which you can use in the javascript to select the correct sector from your array.
http://docs.angularjs.org/api/ng.directive:ngRepeat

You're setting $scope.currentSector, instead of .sectorDesc
So either change your function or template:
Template:
Current sector : {{currentSector}}
Or function:
$scope.currentSector = {};
$scope.setSector = function (sectorCode) {
$scope.currentSector.sectorDesc = $scope.sectors[sectorCode];
};

Related

Filter data using a multiselect. When selecting 2 or 3 options result need to throw only registers where options together exist

thank you in advance for reading me. So I have been working in a filte. Right now my filter works, however doesn't do what I want. The current status is. When I select 2 options or more. I get all the values inside the data that contains either optionA oder optionB.
See my example data below:
{
"_uid": "1",
"body": [
{
"_uid": "2",
"name": "John",
"image": {
"id": 6807178,
"filename": "https://",
"copyright": "",
"fieldtype": "asset",
"is_external_url": false
},
"gewerk": "Project Owner",
"skill": ["vuejs", "react", "symfony"],
"component": "person",
},
{
"_uid": "3",
"name": "Jean",
"image": {
"id": 6807182,
"filename": "https://",
"copyright": "",
"fieldtype": "asset",
"is_external_url": false
},
"gewerk": "UI",
"skill": ["svelte"],
"component": "person",
},
{
"_uid": "4",
"name": "Martha",
"gewerk": "Frontend",
"skill": ["vuejs", "react"],
"component": "person",
},
{
"_uid": "5",
"name": "Tom",
"gewerk": "UI",
"skill": ["svelte", "angular", "vuejs"],
"component": "person",
}
],
}
With that being says when I filter using this example combi(screenshot). I get Martha, Tom and John as a result. When what I actually want is to have only Tom as a result. because only Tom have both criterias together inside his skills data.
This is my current computed function:
filterPersonSkill() {
return this.getComponentPerson.filter((e) =>
e.skill.map((skill) => this.multiValue.includes(skill)).includes(true)
);
}
At the beginning I used includes instead of map and that worked half. Because I was getting the result only if I selected in the same order(in the multiselect) as the array skills was appearing. Example below
filterPersonSkill() {
return this.getComponentPerson.filter((e) =>
e.skill.includes(...this.multiValue)
);
}
Thank in advance for the advice and reading me.
I think, it will be much simpler, if you add checkbox for the user to use "exact" filtering, i.e. results which include only selected tags.
With such a checkbox you can do something like this:
// your vue component
export default {
data() {
return {
exactMatch: true,
}
},
methods: {
filterPersonSkillExactMatch() {
const result = [];
for (const p of this.getComponentPerson) {
if (p.skill.length === this.multiValue.length
&& this.multiValue.every(val => p.skill.includes(val))) {
result.push(p)
}
}
return result
}
// somewhere in your code (either computed prop or method):
filteredPersons() {
if (exactMatch) {
return this.filterPersonSkillExactMatch()
}
return this.filterPerson()
}
}
}

interate through nested json file using ng-repeat not working

So I have a json file with nested elements. This is my json file:
[
{
"qid": "1",
"contester": "0",
"answer": "0",
"question": "What are you most likely to do after getting into an argument? ",
"images": [
{
"qid": "1",
"imageid": "AB100",
"imgname": "doghug_q1",
"imgpath": "Images\/Q1\/doghug_q1.jpg"
},
{
"qid": "1",
"imageid": "AB101",
"imgname": "eq_q1.jpg",
"imgpath": "Images\/Q1\/eat_q1.jpg"
},
{
"qid": "1",
"imageid": "AB102",
"imgname": "headache_q1",
"imgpath": "Images\/Q1\/headache_q1.jpg"
},
{
"qid": "1",
"imageid": "AB106",
"imgname": "scream_q1.jpg",
"imgpath": "Images\/Q1\/scream_q1.jpg"
},
{
"qid": "1",
"imageid": "AB107",
"imgname": "shopping_q1",
"imgpath": "Images\/Q1\/shopping_q1.jpg"
},
{
"qid": "1",
"imageid": "AB108",
"imgname": "walkAlone_q1",
"imgpath": "Images\/Q1\/walkAlone_q1.jpg"
}
]
},
{
"qid": "2",
"contester": "0",
"answer": "0",
"question": "Which game would you rather play?",
"images": [
{
"qid": "2",
"imageid": "AB105",
"imgname": "charades_q2.jpg",
"imgpath": "Images\/Q2\/charades_q2.jpg"
},
{
"qid": "2",
"imageid": "AB109",
"imgname": "playingCards_q2.jpg",
"imgpath": "Images\/Q2\/playingCards_q2.jpg"
},
{
"qid": "2",
"imageid": "AB110",
"imgname": "chess_q2",
"imgpath": "Images\/Q2\/chess_q2.jpg"
},
{
"qid": "2",
"imageid": "AB111",
"imgname": "twister_q2",
"imgpath": "Images\/Q2\/twister_q2.jpg"
}
]
}
]
And this is my controller that i used to access the json :
var app= angular.module('myApp', []);
app.controller('ListCtrl', ['$scope', '$http',
function($scope, $http) {
$http.get('results.json').success(function(data) {
$scope.questions = data; // get data from json
angular.forEach($scope.questions, function(item){
console.log(item.images);
})
});
});
}
]);
And then my html code to display the questions and each questions list of images using the ng-repeat :
<body ng-app="myApp" >
<div ng-controller="ListCtrl">
<ul>
<li ng-repeat="question in questions"> {{question.qid}} </li>
</ul>
</div>
</body>
As of now iam trying to display the questions id from the json file as a list however the output im getting is a:
{{question.qid}}
as the output in my html page.. Can someone please help me. I dont know what im doing wrong.
your code has some missing things.
Here is working example of your code
http://plnkr.co/edit/FvD26TYZ9v8TbgUBUzN2?p=preview
firstly,
var app = angular.module('myApp', []); //variable app is missing.
secondly, data you mention has missing closing square brace.
First define app
var app = angular.module('myApp', []);
Then initiate your $scope.questions on your controller like
$scope.questions = [];
Also you might need to parse your data with
$scope.questions = JSON.parse(data);
You haven's define the app so you should change the first line of your code to this and check your closing tags on your js code...:
var app = angular.module('myApp', []);
app.controller('ListCtrl', ['$scope', '$http',
function($scope, $http) {
$scope.questions = [];
$http.get('results.json').success(function(data) {
$scope.questions = data; // get data from json
angular.forEach($scope.questions, function(item){
console.log(item.images);
})
});
}
]);
Complete working code for your case :
HTML
<!DOCTYPE html>
<html ng-app="plunker">
<head>
<meta charset="utf-8" />
<title>AngularJS Plunker</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.8/angular.js"></script>
<script src="app.js"></script>
</head>
<body>
<div ng-controller="ListCtrl">
<ul>
<li ng-repeat="question in questions"> {{question.qid}} </li>
</ul>
</div>
</body>
</html>
JS
var app = angular.module('plunker',[]);
app.controller('ListCtrl',function ($scope,$http) {
$http.get('data.json').success(function(data) {
$scope.questions = data; // get data from json
});
});
Working Plunker
It is working !! Try once !!

Use Angular with HTML5

I'm taking tutorial in CodeSchool about angular and I try to create my own experiment in html5 and angular new version in vs2012.
I try to test angular repeat but the problem is HTML5 can't render my angular.
When I try to run my page, the repeater only show the angular statement {{item.name}}:{{item.quantity}} not the result. Why? -"show code below"
Angular script :
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular.min.js"></script>
application.js :
<script type="text/javascript" src="Scripts/application.js"></script>
$(function () {
var app = angular.module('zaskarCorp', []);
app.controller('kirito', function ($scope) {
$scope.items = [{
"name": "dual sword",
"quantity": 2
}, {
"name": "gun",
"quantity": 1
}, {
"name": "laser sword",
"quantity": 1
}];
});
});
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" data-ng-app="zaskarCorp">
<head>
<title></title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular.min.js"></script>
<script type="text/javascript" src="Scripts/application.js"></script>
</head>
<body data-ng-controller="kirito">
<ul>
<li data-ng-repeat="item in items">
<span>{{item.name}}:{{item.quantity}}</span>
</li>
</ul>
</body>
</html>
As you can see I add data- in my angular because I use html5. -"I hate warnings"
Your code has a number of typos and errors:
funtion instead of function
agular instead of angular
no jQuery included, but you call $(function(){...})
Here's a plunker with your code working.
Also, for future reference, if you remove .min from the angular source, it makes it easier to debug.
Please add this directive to your body tag:
data-ng-app="zaskarCorp"
Also, you have some misspellings in your javascript code.
Without using jQLite your code will look like this:
var app = angular.module('zaskarCorp', []);
app.controller('kirito', function ($scope) {
$scope.items = [
{
"name": "dual sword",
"quantity": 2
},
{
"name": "gun",
"quantity": 1
},
{
"name": "laser sword",
"quantity": 1
}];
});
Full code:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" data-ng-app="zaskarCorp">
<head>
<title></title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular.min.js"></script>
<script>
var app = angular.module('zaskarCorp', []);
app.controller('kirito', function($scope) {
$scope.items = [{
"name": "dual sword",
"quantity": 2
}, {
"name": "gun",
"quantity": 1
}, {
"name": "laser sword",
"quantity": 1
}];
});
</script>
</head>
<body data-ng-controller="kirito">
<ul>
<li data-ng-repeat="item in items">
<span>{{item.name}}:{{item.quantity}}</span>
</li>
</ul>
</body>
</html>

Why is my select ignoring my ng-model and ng-selected

I have a select box that will not select the option that is bound to ng-model or ng-select, instead it selects the hard coded value initially but when I select another it just overriders my selection with the last item in the list. I have stripped it down to it bare minimum and still cannot see
this is my html page:
<!doctype html>
<html lang="en">
<head></head>
<body>
<div ng-app="myapp">
<div ng-controller="myController">
<select ng-model="freezeCalendar_model" ng-options="freezeCalender.calendarID as freezeCalender.name for freezeCalender in freezeCalendarList"></select>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.1.4/angular.min.js"></script>
<script src="script.js"></script>
</body>
</html>
and this is my script.js:
var app = angular.module("myapp", ["controllers"]);
var controllers = angular.module("controllers", []);
controllers.controller('myController', function ($scope) {
$scope.freezeCalendar_model = 162;
$scope.freezeCalendarList = [{
"id": 104,
"name": "ABC"
}, {
"id": 202,
"name": "123"
}, {
"id": 121,
"name": "xyz"
}, {
"id": 224,
"name": "hello"
}, {
"id": 162,
"name": "bam"
}, {
"id": 164,
"name": "boom"
}];
});
I also Have a plnkr here
This is frustrating me a lot because I have a lot of selects working fine and I cannot understand what is wrong with this particular one.
Help would be greatly appreciated
You're not referencing the id correctly, it appears. Works fine for me after making that change.
Change:
freezeCalender.calendarID as freezeCalender.name
To:
freezeCalender.id as freezeCalender.name

Query JSON node value with another node's value

New to JSON so I'll do my best here. I have a JSON object called HUDS. Below are 2 sample nodes (by the way, can I call these nodes in JSON like in XML?).
var HUDS = [
{
"DISTRICT": "100",
"BIOS": "BROWN",
"AREA_KM": "3663.158164",
"AREA_MI": "1414.347616",
"NAME": "100",
"REG": "1",
"ACRES": "905182",
"EMU_Name": "Purcell",
"Shape_Leng": "299746.4938",
"Shape_Area": "3663158164",
},
{
"DISTRICT": "101",
"BIOS": "THIER",
"AREA_KM": "1507.774765",
"AREA_MI": "582.152762",
"NAME": "101",
"REG": "1",
"ACRES": "372578",
"EMU_Name": "Salish",
"Shape_Leng": "229150.0655",
"Shape_Area": "1507774766",
}
]
I have a drop down form that will be used to specify a value specific to the "BIOS" field in my JSON. For example the user could select BROWN from the dropdown menu.
I'd then like to create a var that can be used to fill in a div. For example when the user selects BROWN I'd like my div to fill with the value from "EMU_Name"
I know this is wrong but maybe it conveys what I am going for
function dropDownAction(){
var tempBios=document.BIOSForm.BIOS.value;
var tempEmuValue=HUDS.BIOS==tempBios.EMU_Name;
document.getElementById("mydiv").innerHTML=tempEmuValue;
}
Try:
<form name="BIOSForm">
<select name="BIOS" onchange="dropdownaction();">
<option value="BROWN">BROWN</option>
<option value="THEIR">THEIR</option>
</select>
</form>
<div id="emuname">
</div>
<script type='text/javascript'>
var HUDS = [
{
"DISTRICT": "100",
"BIOS": "BROWN",
"AREA_KM": "3663.158164",
"AREA_MI": "1414.347616",
"NAME": "100",
"REG": "1",
"ACRES": "905182",
"EMU_Name": "Purcell",
"Shape_Leng": "299746.4938",
"Shape_Area": "3663158164",
},
{
"DISTRICT": "101",
"BIOS": "THIER",
"AREA_KM": "1507.774765",
"AREA_MI": "582.152762",
"NAME": "101",
"REG": "1",
"ACRES": "372578",
"EMU_Name": "Salish",
"Shape_Leng": "229150.0655",
"Shape_Area": "1507774766",
}
]
function dropdownaction(){
for(var x=0;x<HUDS.length;x++){
var tempBios = document.BIOSForm.BIOS.value;
if(tempBios == HUDS[x].BIOS){
document.getElementById("emuname").innerHTML = HUDS[x].EMU_Name;
break;
}
}
}
</script>

Categories

Resources