How to show glyphicon eye based on value in Angularjs? - angularjs

I have glyphicon-eye for all fields in the form. If user click on glyphicon-eye-open then it will change to glyphicon-eye-close and I push that specific field name into an array.
In my JSON response I am getting the hidden field values but how can I use that value and call exact glyphicon-eye.
JSON response :
{
"response": {
"status": {
"code": "0",
"message": "Success"
},
"service": {
"servicetype": "4",
"functiontype": "1005"
},
"data": {
"session_id": "372",
"roles": [
{
"hiddenfields": [
{
"fname": "firstname",
"fblink": "fblink",
"country": "country",
"martialStatus": "martialStatus"
}
]
}
]
}
}
}
Controller :
$scope.user = {
fname: "firstname",
lname: "lastname",
dob: "dob",
gender: "gender",
country: "country",
state: "state",
city: "city",
pincode: "pincode",
martialStatus: "martialStatus",
emailId: "emailId",
mobile: "mobile",
qualification: "qualification",
fblink: "fblink"
};
$scope.allow = {};
$scope.users = [];
$scope.push = function(){
$scope.users = [];
var user = {},
allow = $scope.allow;
console.log(allow);
Object.keys(allow).forEach(function(key){
allow[key] ? user[key] = $scope.user[key] : null;
});
$scope.users.push(user);
}
HTML :
<a class="menu-toggle" class="btn btn-default" ng-model="allow.fname"><i class="glyphicon" ng-class="{'glyphicon-eye-open':allow.fname, 'glyphicon-eye-close':!allow.fname}" ng-click="push(allow.fname = allow.fname?false:true)"></i></a>
If field value is in array then I need to show glyphicon-eye-close.

You can use ng-class like below.
<div class="form-group" ng-repeat="x in allow" >
<button class="btn btn-default"><span class="glyphicon" ng-class="{ 'glyphicon-eye-open': x.fname==0 , 'glyphicon-eye-close': x.fname==1}"></span> {{x.name}}</button>
</div>
function myCtrl($scope) {
$scope.allow=[{
'fname':1,
'name':'Anil'
},{
'fname':0,
'name':'Sunil'
},{
'fname':1,
'name':'Manil'
}]
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div ng-app ng-controller="myCtrl" class="col-md-12">
<div class="form-group" ng-repeat="x in allow" >
<button class="btn btn-default"><span class="glyphicon" ng-class="{ 'glyphicon-eye-open': x.fname==0 , 'glyphicon-eye-close': x.fname==1}"></span> {{x.name}}</button>
</div>
</div>

Some times the ! doesnot works inside ng-clss. You please replace that by
<a class="menu-toggle" class="btn btn-default" ng-model="allow.fname">
<i class="glyphicon"
ng-class="{'glyphicon-eye-open':allow.fname.length > 0, 'glyphicon-eye-close':allow.fname.length == 0}"
ng-click="push(allow.fname = allow.fname?false:true)">
</i>
</a>

I am now able to glyphicon based on values by assigning the response to my model as below,
$scope.allow = response.data.roles[0].hiddenfields[0];

Related

How to use ng-repeat on this JSON data

I am trying to call hotel_name using ng-repeat. How do I do it.
This is the code. But nothing is being displayed.
<div ng-repeat="x in hotels">
<p ng-repeat="y in x.data">{{y.hotel_name}}</p>
</div>
This is the data format.
{
status: 200,
message: "Packages Found",
data: [
{
hotel_id: "40",
company_id: "2",
user_id: "17",
hotel_name: "Zen International",
hotel_description: "Good Hotel"
}
]
}
Your hotels seems an object. You don't need to iterate it.
Convert this
<div ng-repeat="x in hotels">
<p ng-repeat="y in x.data">{{y.hotel_name}}</p>
</div>
to this
<div>
<p ng-repeat="y in hotels.data">{{y.hotel_name}}</p>
</div>
You can us one ng-repeat
<div>
<p ng-repeat="y in hotels.data">{{y.hotel_name}}</p>
</div>
DEMO
var demoApp = angular.module('demoApp', []);
var controllers = {};
controllers.SimpleController = function ($scope) {
$scope.hotels = {
status: 200,
message: "Packages Found",
data: [
{
hotel_id: "40",
company_id: "2",
user_id: "17",
hotel_name: "Zen International",
hotel_description: "Good Hotel"
}
]
};
};
demoApp.controller(controllers);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.5/angular.min.js"></script>
<div ng-app="demoApp" ng-controller="SimpleController">
<div>
<p ng-repeat="y in hotels.data">{{y.hotel_name}}</p>
</div>
</div>
Bind Datalist with $scope in controller like
angular.module('YourApp', [])
.controller('YourCtrl', function($scope) {
$scope.dataList={
status: 200,
message: "Packages Found",
data: [
{
hotel_id: "40",
company_id: "2",
user_id: "17",
hotel_name: "Zen International",
hotel_description: "Good Hotel"
}
]
}
});
and bind dataListwith view like
<div ng-app="YourApp" ng-controller="YourCtrl">
<div>
<p ng-repeat="item in dataList.data">{{item.hotel_name}}</p>
</div>
</div>

Angularjs: Use ng-repeat="(key, value) in myObj" to show data from json

I am creating one notification app which will send notification through email and SMS. Which will be set to true at starting (cant change json data)
I have created toggle button which will change status of each email and sms to true and false. when changing status to true and false, need to send json data again in below format:
PUT : http://example.url
{
"category": "Fruit",
"method": "EMAIL",
"enabled": true
}
HTML:
<div class="notification-inner-wrap" ng-repeat="settings in pgxNotification.preferences | orderBy:'order'" >
<p class="notification-heading">{{settings.code}}</p>
<div ng-repeat="(key, value) in pgxNotification.preferences">
<div class="notification-methods">
<span>{{settings.methods[0]}}</span>
<div class="notification-on-off-icon">
<i class="fa fa-toggle-on active" ng-if="status == true" ng-click="status = !status"></i>
<i class="fa fa-toggle-on fa-rotate-180 inactive" ng-if="status == false" ng-click="status = !status"></i>
</div>
<pre>{{ status }}</pre>
</div>
<div class="notification-methods">
<span>{{settings.methods[1]}}</span>
<div class="notification-on-off-icon">
<i class="fa fa-toggle-on active" ng-if="status == true" ng-click="status = !status"></i>
<i class="fa fa-toggle-on fa-rotate-180 inactive" ng-if="status == false" ng-click="status = !status"></i>
</div>
<pre>{{ status }}</pre>
</div>
</div>
</div>
JS:
app.controller('MainCtrl', function($scope) {
$scope.status = true;
$scope.changeStatus = function(status){
$scope.status = !$scope.status;
//var method = $scope.pgxNotification.methods[index];
}
var response = {
"status" : true,
"exception" : null,
"data": {
"methods": ["SMS","EMAIL","PUSH"],
"preferences": [
{
"code": "Fruit",
"name": "Fruit content",
"methods": ["SMS", "EMAIL"]
},
{
"code": "Vegetable",
"name": "Vegetable content",
"methods": ["SMS", "EMAIL"]
},
{
"code": "Car",
"name": "Car content",
"methods": ["SMS", "EMAIL"]
},
{
"code": "bike",
"name": "bike content",
"methods": ["SMS", "EMAIL"]
}
]
}
};
$scope.pgxNotification = response.data;
});
Here is the plunkr:
https://plnkr.co/edit/pIBjao0ujuAzyJGZSPfU?p=preview
Right now the switch button is not working, and need to figure out on-switch how to send json response in above format.
somewhere I read that need to use ng-repeat="(key, value) in pgxNotification.preferences" to set key value and then send json response.
You are using status variable at controller scope level. Hence whatever last update occur on status from any control will be final view value. Hence you are not getting expected output.
Here is the updated plunkr

How can I have a ng-model in btn-group

How can I have a ng-model in a btn-group? It's working like radio buttons, and I need to control the answer every time I click in one specific button.
Here is my code:
<div class="btn-group">
<button type="button" class="btn btn-sm btn-primary"
ng-click="icc.model.investigacao.dadoClinicoDoencaPreExCollection.statusDiabetes = 1"
ng-class="icc.model.investigacao.dadoClinicoDoencaPreExCollection.statusDiabetes == 1 ? [''] : ['btn-info']">
Yes
</button>
<button type="button" class="btn btn-sm btn-primary"
ng-click="icc.model.investigacao.dadoClinicoDoencaPreExCollection.statusDiabetes = 2"
ng-class="icc.model.investigacao.dadoClinicoDoencaPreExCollection.statusDiabetes == 2 ? [''] : ['btn-info']">
No
</button>
<button type="button" class="btn btn-sm btn-primary"
ng-click="icc.model.investigacao.dadoClinicoDoencaPreExCollection.statusDiabetes = 9"
ng-class="icc.model.investigacao.dadoClinicoDoencaPreExCollection.statusDiabetes == 9 ? [''] : ['btn-info']">
Ignored
</button>
</div>
In the end I need to validate these custom "radio buttons". How can I do that?
You can use Bootstrap to style your radio buttons as toggle buttons. This will allow you to keep the input type for use of ng-model and form validation, without using your ng-click method above.
Here is a simple demo in a fiddle.
Controller:
var app=angular.module('myApp', []);
app.controller('MyCtrl', function MyCtrl($scope) {
$scope.people = [{
name: "John"
}, {
name: "Paul"
}, {
name: "George"
}, {
name: "Ringo"
}];
$scope.choices = {};
});
HTML:
<form name="myForm" ng-app="myApp" ng-controller="MyCtrl">
<p>Favorite Beatle</p>
<div class="btn-group-vertical" data-toggle="buttons">
<label class="btn btn-primary" ng-repeat="person in people">
<input type="radio" name="options" id="option1" autocomplete="off" ng-model="choices.SelectedPerson" value="{{person.name}}" required> {{person.name}}
</label>
</div>
<p><tt>myForm.$invalid: {{myForm.$invalid}}</tt></p>
You chose: <strong>{{choices.SelectedPerson}}</strong>
<button ng-disabled="myForm.$invalid">Submit</button>
You have very similar buttons code. I advise you to generate them from the model. And you will no problem to link them to controller.
Here is code example:
in controller:
$scope.buttonChecked = 1;
$scope.buttons = [
{
text: 'Yes',
value: 1
}, {
text: 'NO',
value: 2
}, {
text: 'Ignored',
value: 9
}
];
And the view:
<div class="btn-group">
<button ng-repeat="button in buttons" type="button" class="btn btn-sm btn-primary"
ng-click="buttonChecked = button.value"
ng-class="buttonChecked == button.value ? [''] : ['btn-info']">
{{button.text}}
</button>
</div>
I am not sure how do you want represent the changes, but if you will explain that more detail I will edit an answer, hope it will help you!
Updated
I had a situation like that one and I solve it like so:
I used the li to position the buttons and are the "real" clickable element the <button>... are just for looks :D
<ul>
<li ng-repeat="item in buttons" ng-show=item.view ng-click="goToNgRepeat(item.op);" ng-style="{'display':'inline-block'}">
<button class="menuBtn" ng-style="{'width':item.size, 'height':menuButtonHeight}"><b>{{ item.tx }}</b></button>
</li>
</ul>
and solve the which one was pressed with ng-click calling a function and passing a value, the value is defined in the button array:
$scope.buttons = [
{ "tx": "Alm Extn", "op": 0, "view": true, "viewCont": false, "file": "TXT/extn.htm", "size": topicButtonsPos.menuButtonW },
{ "tx": "DS", "op": 1, "view": true, "viewCont": false, "file": "TXT/Ds.htm", "size": topicButtonsPos.menuButtonW },
{ "tx": "CDnt", "op": 2, "view": true, "viewCont": false, "file": "TXT/C.htm", "size": topicButtonsPos.menuButtonW },
{ "tx": "CDnt", "op": 3, "view": true, "viewCont": false, "file": "TXT/CA.htm", "size": topicButtonsPos.menuButtonW },
...
];
This array content op which I use as button value.
Hope it helps.

How to make filter to be viewed in html

On my home page I added a button named 'city'. On clicking it opens a popup listing the city names. On clicking the city 'paris' the data with city name must be displayed.
homepage
<ion-header-bar class="bar bar-subheader">
<button class="button button-flat button-positive"ng-click="citypopup();" style="padding-right: 63px;
padding-left: 18px;">
<l >City </l>
<l class="icon ion-chevron-down"></l>
</button>
</ion-header-bar>
<ion-content>
<div class='card ' ng-repeat="item in list | filter:test.searchCity ">
<div class="list " >
<div class='item' style="padding-top:0px;" > {{item.id}}
<l class="item-icon-right" style="padding-left:30%"> {{item.date}} </l>
</div>
<div class='item' style="padding-top:0px;" >{{item.status }}
<l class="item-icon-right" style="text-align:right;">{{item.QCstatus}}</l>
<i class="icon ion-chevron-right"></i>
</div>
<b class='item '> {{item.Factory}} </b>
<l class='item '>{{item.city }}</l>
</div>
popup page
<ion-list>
<ul class="list" ng-model="test.searchCity" ng-repeat="ci in cityList | orderBy:'city' " >
<li class="item" ng-click="test.searchCity">{{ci.city}} </li>
</ul>
</ion-list>
javascript
$scope.test = {
searchCity: null,
};
$scope.cityList = [
{
"city": "Chennai"
}, {
"city": "Banglore"
}, {
"city": "Delhi"
}
];
$scope.list = [
{
id: "#001",
date:"DEC 04 2016",
status: "Successful",
QCstatus: "Approve",
Factory: "ARUN ICE CREAM",
city: "paris"
},
{
id: "#002",
date: "JAN 11 2016",
status: "Successful",
QCstatus: "Approve",
Factory: "Floxtronics",
city: "Delhi"
},
{
id: "#003",
date: "Feb 14 2016",
status: "Bug fixing",
QCstatus: "Approve",
Factory: "Aachi",
city: "Chennai"
}
];
$scope.$watch('test.searchCity', function () {
$scope.test.searchCity = null;
console.log('test.searchCity')
});
$scope.citypopup = function() {
var myPopup = $ionicPopup.show({
scope: $scope,
templateUrl: 'templates/cityTemplate.html',
buttons: [
{ text: 'Cancel',
type: 'button-positive' },
]
});
}
What I need is when I click 'city' button, my city popup appears and when I click on a city nothing happens! Could someone help me?
No need to create another popup page, you can open popup content in citypopup() function
$scope.citypopup = function() {
$scope.popup = $ionicPopup.show({
template:"<ion-list><ul class='list' ng-model='test.searchCity' ng-repeat='ci in cityList | orderBy:city ' ><li class='item' ng-click='searchCity(ci.city)'>{{ci.city}} </li> </ul></ion-list>" ,
title: 'City',
scope: $scope
});
}
$scope.searchCity = function(city){
$scope.test.searchCity = city;
$scope.popup.close();
}
You never assign test.searchCity.
In you popup page code you use ng-click="test.searchCity", where you probably should use ng-click="test.searchCity = ci.city".
Unrelated to your problem (but a matter of correct use of patterns): No need for ngModel in a ul element (it's only for form elements), and ng-repeat makes more sense when used on lis rather than uls.
In conclusion:
<ion-list>
<ul class="list">
<li class="item" ng-repeat="ci in cityList | orderBy:'city' " ng-click="test.searchCity = ci.city">{{ci.city}} </li>
</ul>
</ion-list>

binding data in ng-repeat angularjs

*This is my html file where i want to repeat chapters which is a array that looks like
My code gives binds the selected checked boxes only (Index values) to true. But i need the entire list of chapters and their i.d's to be retrieved on submit.
Cannot figure out how to iterate it on nested loops *
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<span ng-repeat="chapter in chapters">
<label><input type="checkbox" name="checkbox" value="{{chapter}} ng-model="escConfigForm.chapters[$index]" >{{chapter.name}}</label><br>
</span>
<input type="submit" id="save" value="Save" />
$scope.chapters = chapterService.getChapters($scope.isbn);
$scope.submit = function(escConfigForm) {
var postData = {
content_items: JSON.stringify({
"#context" : [],
"#graph" : [ {
"#type" : "ContentItemPlacement",
"placementOf" : {
"#type" : "LtiLink",
"text" : escConfigForm.examName,
"mediaType" : "application/vnd.ims.lti.v1.launch+json",
"title" : "Exam Study Center",
"custom" : {
"dueDate" : escConfigForm.examDueDate,
"targetScore" : escConfigForm.examTargetScore,
"chapters" : escConfigForm.chapters
},
"activityType" : "other",
"activityRefId" : $scope.escId
}
} ]
}),
data: $scope.data
};
postForm($scope.postUrl, postData);
var configData = {
"activityRefId" : $scope.escId,
"dueDate" : escConfigForm.examDueDate,
"targetScore" : escConfigForm.examTargetScore,
"chapters" : escConfigForm.chapters
};
console.log($scope.chapters);
JSON file:
[{"name":"Chapter 1: Negative Messages","id":"832115"},{"name":"Chapter 2: Physics","id":"832115"},...]
I would recommend maintaining a list of the selected objects in the controller.
using this post as referenece: How do I bind to list of checkbox values with AngularJS?
I created this fiddle: http://jsfiddle.net/ruwk5r0v/7/
<div ng-app="formExample">
<div ng-controller="ExampleController"> <span ng-repeat="chapter in chapters" ng-click="checkboxChange($index)" ng-checked="selection.indexOf($scope.chapters[$index]) > -1">
<input type="checkbox" name="checkbox" value="{{$index}}" />
{{chapter.name}}
<br>
</span>
<br>
<input type="submit" ng-click="submitForm()" id="save" value="Save" />
<div> <span ng-repeat="chapter in selection">
<span>
{{chapter.name}}
</span>
<br>
</div>
and the js:
angular.module('formExample', []).controller('ExampleController', ['$scope', function ($scope) {
$scope.chapters = [{
"name": "Chapter 1: Negative Messages",
"id": "832115"
}, {
"name": "Chapter 2: Physics",
"id": "832115"
}];
$scope.submitForm = function () {
console.log(selection);
}
$scope.selection = []
$scope.checkboxChange = function(index){
var chapter = $scope.chapters[index];
var idx = $scope.selection.indexOf(chapter);
if (idx > -1){
$scope.selection.splice(idx, 1);
} else {
$scope.selection.push(chapter);
}
}
}]);
here you can very easily implement your submit function, just use the new selection object.
This really should be moved into a directive, but don't have time to write that right now :P
Here I created a controller for the snippet, and added some data to $scope.chapters object, and it is displaying correctly. The values disappear when selected, but that is another issue.
angular.module('myApp', [])
.controller('myCtrl', ['$scope',
function($scope) {
$scope.chapters = [{
name: 'Chapter 1: Negative Messages',
id: "1",
isSelected: false
}, {
name: 'Chapter 2: Physics',
id: "2",
isSelected: false
}];
$scope.submitItems = function(chapters) {
alert(JSON.stringify(chapters));
}
}
]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp">
<div ng-controller="myCtrl">
<span ng-repeat="chapter in chapters">
<input type="checkbox" name="checkbox" value="{{chapter}}"
ng-model="chapter.isSelected" />
{{chapter.name}}
</span>
<br>
<form ng-submit="submitItems(chapters)">
<input ng-model="chapters" type="submit" id="save" value="Save" />
</form>
</div>
</div>
Edit: Updated the code to reflect the OP needs.

Resources