clicking on a specific ion-item button triggers other buttons - angularjs

i used ng-repeat to generate an ion-list. inside each ion-item there's a toggle-behaviored button that changes color when tapped and untapped. however when i tap on the button in one ion-item, the other buttons from other ion-items also gets tapped and changes their colors. how should i go about this?
<ion-list id="listOfCandidates-list6">
<ion-item ng-repeat="item in itemList" class="item-candidates alternaterow item-thumbnail-left" id="listOfCandidates-list-item8">
<img src="img/tRk31wFSmS2hKHXnIAOZ_candidateicon.png">
<br>
<div class="col1">
<h4>Name: {{item.name}}</h4><h4>Candidate No.: {{item.number}}</h4>
</div>
<div class="col2">
<button class="button button-clear icon ion-star button-energized" ng-model="singleTog" ng-click="toggleButton(item.name)" ng-class="singleTog.clicked?'button-energized':'button-dark'" ></button>
<button ng-click="goDetail(item.name,item.number)" class="button btn-small btn-primary">View</button>
</div>
</ion-item>
</ion-list>
and here's my controller.js code:
$scope.favoriteList = []
$scope.singleTog = {}
$scope.toggleButton = function(candidateName)
{
$scope.singleTog.clicked=!$scope.singleTog.clicked
if($scope.singleTog.clicked==true)
{
if($scope.favoriteList.indexOf(candidateName) == -1) //does not exist in array
{
$scope.favoriteList.push(candidateName);
}
}
else
{
if($scope.favoriteList.indexOf(candidateName) != -1) //exists in array
{
var index = $scope.favoriteList.indexOf(candidateName);
$scope.favoriteList.splice(index, 1);
}
}
alert('favorites = ' + $scope.favoriteList);
}

pass the index to the function which you are calling to trigger an event.
<button class="button button-clear icon ion-star button-energized" ng-model="singleTog" ng-click="toggleButton(item.name,$index)" ng-class="singleTog[$index] ?'button-energized':'button-dark'" ></button>
<button ng-click="goDetail(item.name,item.number)" class="button btn-small btn-primary">View</button>
and in your controller
$scope.singleTog =[];
$scope.toggleButton = function(candidateName,index)
{
$scope.singleTog[index]=!$scope.singleTog[index];
if($scope.singleTog[index])
{
if($scope.favoriteList.indexOf(candidateName) == -1) //does not exist in array
{
$scope.favoriteList.push(candidateName);
}
}
else
{
if($scope.favoriteList.indexOf(candidateName) != -1) //exists in array
{
var index = $scope.favoriteList.indexOf(candidateName);
$scope.favoriteList.splice(index, 1);
}
}
alert('favorites = ' + $scope.favoriteList);
}

Related

Ionic scroll with direction y is not working in ionic 1

view code:
<ion-view class="forms-view" ng-init="loadall()">
<ion-nav-buttons side="left">
<button menu-toggle="left" class="button button-icon icon ion-navicon"></button>
</ion-nav-buttons>
<ion-nav-title>
<span>List</span>
</ion-nav-title>
<ion-content>
<ion-scroll direction="xy" on-scroll="addMoreItem()">
<ion-list ng-controller="card">
<div class="item item-input-inset">
<label class="item-input-wrapper">
<i class="icon ion-search placeholder-icon"></i>
<input type="text" ng-model="input.filterUser" placeholder="Search" style="height:48px;">
</label>
</div>
<ion-item class="item-avatar item-icon-right" ng-repeat="cardlists in cardlist | filter:input.filterUser" ui-sref="app.details({card_id:cardlists.card_id,xyz:0})">
<img src="{{cardlists.avatar}}" >
<h2>{{cardlists.name}}</h2>
<p>{{cardlists.card_id}} </p>
<i class="icon ion-plus-round custom-icon" ng-show="!cardlists.isShow" ng-click="addTo(cardlists.card_id);cardlists.isShow = true"></i>
<i class="icon ion-checkmark-round" ng-show="cardlists.isShow"></i>
</ion-item>
</ion-list>
</ion-scroll>
</ion-content>
</ion-view>
controller code:
controller('boardCtrl', function ($scope, $http, $cordovaSQLite, $localStorage, $state,$rootScope,$ionicPlatform,$ionicPopup,$timeout,$filter,$ionicNavBarDelegate) {
$rootScope.limit_list=10;
$scope.addMoreItem=function()
{
if($scope.cardlist.length >= $rootScope.limit_list)
{
$rootScope.limit_list+=5;
}
$scope.loadall();
}
$scope.loadall=function()
{
$rootScope.cardlist = [];
var query2 = "SELECT * FROM abc LIMIT "+$rootScope.limit_list;
$cordovaSQLite.execute(db, query2, []).then(function (res) {
if (res.rows.length > 0) {
for (var j = 0; j < res.rows.length; j++) {
var name = res.rows.item(j).name;
$rootScope.cardlist.push({
name: name,
age: res.rows.item(j).age,
card_id: res.rows.item(j).card_id,
avatar: "img/avatar5.png"
});
}
}
}
}
}
I want to display a list whenever view is loaded and its working fine.The only change what i want to make is to display only 10 names by default and once i scroll down the page i want to load 10 more and so on.The above code is working perfectly fine but only in x direction i.e horizontal scroll.I want it load more name once i scroll down side i.e vertical scroll.Can any one tell me what is the issue with above code.It will be helpful.

how to refresh the list in AngularJs?

I have to implement the share product functionality with the user after share i have to refresh the list here selectedSharedIntermediaryInAlbum is our array so how to implement this?
//refreshSharedIntermediary for refresh the lsit
$scope.refreshSharedIntermediary = function() {
$scope.selectedSharedIntermediaryInAlbum = $scope.selectedSharedIntermediaryInAlbum;
}
<div class="w3-right">
<button class="btn btn-rose btn-sm" tltle="refresh data" ng-click="refreshSharedIntermediary()">
<i class="glyphicon glyphicon-repeat"></i>
</button>
</div>
You can use $scope.reload function where need to reload the list ... i have used below concept :
<div class="well well-lg" ng-repeat="item in list track by item.id">
<button class="btn btn-default"
ng-click="collapsedFlags[item.id] = !collapsedFlags[item.id]">
{{ item.isCollapsed ? '+' : '-' }}
</button>
<div uib-collapse="collapsedFlags[item.id]" ng-bind="item.value"></div>
</div>
$scope.reload = function () {
$http.get('list.json').then(function (data) {
$scope.list = data && data.data || [];
});
};
$scope.collapsedFlags = {};
$scope.reload();
Working example : https://plnkr.co/edit/87vVb8Jjyz6yFGAjiHBK?p=preview

Add change amount and change the Total in Check Box in Ionic

I created a list in Checkbox, listing products where the user can choose a product. I need add in the product list an option where the user can change the quantity of products selected. How I can do it?
My View:
<ion-view view-title="Bebidas Adicionais" ng-controller="exBebidasCtrl" >
<div class="bar bar-subheader">
<h2 class="title">{{'Sub-Total R$ ' + getTotalSelected()}}</h2>
</div>
<ion-refresher pulling-text="Puxe para atualizar..." on-refresh="doRefresh()"></ion-refresher>
<ion-list class="card list">
<div class="item item-input">
<i class="icon ion-search placeholder-icon"></i>
<input type="search" ng-model="q" placeholder="Procurar" aria-label="filter bebidasextras" />
</div>
</ion-list>
<ion-list>
<div ng-repeat="bebida in bebidasextras">
<ion-checkbox ng-model="bebida.selected" >
<h2>{{bebida.ad_bebida_titulo}}</h2>
<p>R$ {{bebida.ad_bebida_valor}}</p>
</ion-checkbox>
</div>
</ion-list>
<button class="button button-block button-balanced">
<a ng-click="addToCart(bebida.ad_bebida_titulo,bebida.ad_bebida_valor)" class="button button-assertive button-clear icon ion-android-cart"> Continuar Comprando </a>
</button>
</ion-content>
My Controller:
$scope.bebidasextras = [];
var promise = $http.get('http://nhac.esy.es/api_carrinho/lista_bebida_extra.php?json=restaurantes')
.success(function(retorno) {
console.log(retorno);
$scope.bebidasextras = retorno; // não precisa fazer retorno.data
$scope.user = {
bebidasextras: [$scope.bebidasextras[1]]
};
$scope.checkAll = function() {
$scope.user.bebidasextras = angular.copy($scope.bebidasextras);
};
$scope.uncheckAll = function() {
$scope.user.bebidasextras = [];
};
$scope.checkFirst = function() {
$scope.user.bebidasextras = [];
$scope.user.bebidasextras.push($scope.bebidasextras[0]);
};
$scope.setToNull = function() {
$scope.user.bebidasextras = null;
};
$scope.getTotalSelected = function() {
var total = 0;
for(var i = 0; i < $scope.bebidasextras.length; i++){
var bebida = $scope.bebidasextras[i];
total += bebida.selected ? Number(bebida.ad_bebida_valor) : 0;
}
return total;
}
})
.error(function(erro) {
console.log(erro);
});
You can have an input box having a + and - button. Clicking upon which user can change the quantity of product selected.
If you can share some more details probably I would be able to answer in a better way.

Angular $scope not updating in Ionic Framework

I have a ng-repeat list, that has a button that changes the state of the item, when I click the button I run the following:
$scope.fav = function(item) {
var i = $scope.news.indexOf(item);
console.log('index', i);
console.log('$scope.news[i].fav', $scope.news[i].fav);
console.log('$scope.news[i]', $scope.news[i]);
if ($scope.news[i].fav === 0) {
console.log('is 0');
$scope.news[i].fav = 1;
} else {
console.log('its not 0');
$scope.news[i].fav = 0;
}
};
It finds the object without a problem, but
$scope.news[i].fav = 1;
or
$scope.news[i].fav = 0;
Never changes the final value. (Keeps returning 0)
Already tried adding $scope.$apply(); with no avail..
MARKUP:
<ion-list class="list item-text-wrap" can-swipe="true">
<ion-item class="item item-thumbnail-left animated fadeIn" ng-repeat="item in news | filter: {title: sinTildes} | orderBy: '-pubDate'" href="#/feed/{{item.source}}/{{item.link}}/{{item.title}}" ng-click="openNews(item)">
<img class="gm-thumbnail" src="{{item.image}}">
<h2>{{item.title}}</h2>
<div class="gm-bottom-left">
<h4 class="dark">{{item.source}} -
<span am-time-ago="item.pubDate"> </span>
</h4>
</div>
<div class="gm-bottom-right">
<span class="icon ion-ios-eye ng-hide font-medium assertive" ng-show="item.rss"></span>
</div>
<ion-option-button class="button-stable" ng-click="fav(item)">
<span ng-show="item.fav"> <i class="icon ion-ios-star energized animated fadeIn"></i></span>
<span ng-show="!item.fav"> <i class="icon ion-ios-star-outline dark animated fadeIn"></i></span>
</ion-option-button>
</ion-item>
</ion-list>
It is an Ionic issue.
I fixed it using angular.copy like this:
$scope.fav = function(item) {
var i = $scope.news.indexOf(item);
var news = angular.copy($scope.news);
console.log('index', i);
console.log('news[i].fav', news[i].fav);
console.log('news[i]', news[i]);
if (news[i].fav === 0) {
console.log('is 0');
DBnews.fav(news[i].link, 1).then(function() {
news[i].fav = 1;
$scope.news[i] = {};
$scope.news[i] = angular.copy(news[i]);
})
} else {
console.log('its not 0');
DBnews.fav(news[i].link, 0).then(function() {
news[i].fav = 0;
$scope.news[i] = {};
$scope.news[i] = angular.copy(news[i]);
});
}
};

Angular $scope and vars from select box confusion

I have setup a system where I can filter displaying some reporting data on button clicks and drop down list selections.
The filtering works well on the button clicks but I am running into trouble with the drop down list.
Setup is as follows:
... more buttons above with the same as below
<div class="col col-center">
<button class="button button-outline button-positive" ng-click="setMetric('2')">Personal</button>
</div>
<div class="col col-center">
<button class="button button-outline button-positive" ng-click="setMetric('3')">Business</button>
</div>
</div>
<div class="list">
<label class="item item-input item-select">
<div class="input-label">
Area
</div>
<select ng-options="z.id as z.zone for z in zones" ng-model="area"></select>
</label>
</div>
<p>Ref: 1.{{category}}.{{metric}}.0.{{area}}</p> <!-- this updates fine -->
<div ng-show="shouldShow(1.1.1.0.1)" line-chart chart-data="data"></div>
<div ng-show="shouldShow(1.1.1.0.2)" line-chart chart-data="data2"></div>
and the controller:
$scope.category = '1';
$scope.metric = '1';
$scope.area = '1';
$scope.zones = [
{'zone':'National', 'id':'1'},
{'zone':'QLD', 'id':'2'},
{'zone':'All other areas', 'id':'3'},
{'zone':'Affluent', 'id':'4'},
{'zone':'Mass', 'id':'5'}
];
$scope.setCategory = function(category) {
$scope.category = category;
};
$scope.setMetric = function(metric) {
$scope.metric = metric;
};
$scope.setArea = function(area) {
$scope.area = area;
};
$scope.getArea = function() {
return $scope.area;
};
$scope.shouldShow = function () {
console.log("1." + $scope.category + "." + $scope.metric + ".0." + $scope.area); // This does not
switch ("1." + $scope.category + "." + $scope.metric + ".0." + $scope.area) {
case "1.1.1.0.1":
return true;
break;
case "1.1.1.0.2":
return true;
break;
default: return false;
}
};
The problem I am running into is that the area var on the page (see the html comment as part of Ref: 1.{{category}}.{{metric}}.0.{{area}}) does update but the reference to it in the function shouldShow() does not update.
How am i misunderstanding scope and how do I make this work?

Resources