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

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.

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.

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 controller value seeming to revert to original value

After originally setting my controller weddingPrice value a change caused by a function does not seem to change the variable. Its probably a simple error- can anyone help?
When sendWeddingQuoteInfo() is called it seems to send the original weddingPrice, not the one which has been updated when the return journey toggle has been checked.
What should happen is that the wedding price should be set at the start through the local setup function. Then if the return journey toggle is switched to on, the returnJourneyChange() should be fired, changing the global weddingPrice. When the Submit Quote button is pressed this should take the updated weddingPrice and send it to the next page. Currently this does not happen- no idea why.
//wedding.html
<ion-view view-title="Wedding Pax Num" can-swipe-back="true">
<ion-content ng-controller="WeddingPaxCtrl">
<div class="card">
<div class="item centerText" style="background-color: brown;">
<h2>CALCULATE WEDDING</h2>
</div>
</div>
<div class="padding20Top padding20Bottom">
<h2 class="centerText">Select number of Passengers</h2>
<input ng-model="passengerNo" class="col col-50 col-offset-25 quoteTFieldWithListItems" type="textfield">
<h6 class="centerText">PASSENGERS</h6>
</div>
<ion-toggle ng-model="returnJourney.checked" ng-change="returnJourneyChange()">Return Journey
</ion-toggle>
<ion-toggle ng-show="returnJourney.checked" ng-model="midnightReturn.checked" ng-change="midnightReturn()">Return Journey After Midnight</ion-toggle>
<div>
<h6 class="logText centerText"> {{ getCostBreakDown() }}</h6>
</div>
</ion-content>
<div class="endOfPageButton">
<a href="#/app/home/tester">
<button class="button button-full button-clear" ng-click="sendWeddingQuoteInfo()">Submit Quote</button>
</a>
</div>
</ion-view>
//controller.js
app.controller('WeddingPaxCtrl', function($scope, $stateParams, PassData, WhichQuote, CostOfMarriage, StoreQuoteData, WeddingData, DataThrow) {
var item = WeddingData.getWeddingDataObject();
$scope.passengerNo = 49;
$scope.returnJourney = { checked: false };
$scope.midnightReturn = { checked: false };
var weddingPrice;
$scope.returnJourney;
$scope.midnightReturn;
setup();
var sendInfo;
function setup() {
console.log("setup called");
weddingPrice = CostOfMarriage.getPrice(item[0], $scope.passengerNo, $scope.returnJourney.checked, $scope.midnightReturn.checked);
}
$scope.returnJourneyChange = function() {
console.log("return j called");
weddingPrice = 1000;
console.log("wedding price is now" + weddingPrice);
}
$scope.midnightReturn = function() {
}
$scope.getCostBreakDown = function() {
}
$scope.sendWeddingQuoteInfo = function() {
// var weddingPrice = $scope.weddingPrice;
console.log("WeddingPrice is " + weddingPrice + weddingPrice);
var sendInfo = ["Wedding Hire", item[0], $scope.passengerNo, "Extra", weddingPrice];
StoreQuoteData.setQuoteData(sendInfo);
WhichQuote.setInfoLabel("Wedding");
}
})
I think your ng-controller attribute is not at the right place. So the scope of your submit button is different.
I've moved the controller to ion-view element then the click is working as expected.
Please have a look at the demo below or here at jsfiddle.
(I've commented a lot of your code just to make the demo work.)
var app = angular.module('demoApp', ['ionic']);
app.controller('WeddingPaxCtrl', function($scope, $stateParams) { //, WeddingData, DataThrow) {
//var item = WeddingData.getWeddingDataObject();
$scope.passengerNo = 49;
$scope.returnJourney = {
checked: false
};
$scope.midnightReturn = {
checked: false
};
var weddingPrice;
$scope.returnJourney;
$scope.midnightReturn;
setup();
var sendInfo;
function setup() {
console.log("setup called");
weddingPrice = 100; //CostOfMarriage.getPrice(item[0], $scope.passengerNo, $scope.returnJourney.checked, $scope.midnightReturn.checked);
}
$scope.returnJourneyChange = function() {
console.log("return j called");
weddingPrice = 1000;
console.log("wedding price is now" + weddingPrice);
}
$scope.midnightReturn = function() {
}
$scope.getCostBreakDown = function() {
}
$scope.sendWeddingQuoteInfo = function() {
// var weddingPrice = $scope.weddingPrice;
console.log("WeddingPrice is " + weddingPrice + weddingPrice);
//var sendInfo = ["Wedding Hire", item[0], $scope.passengerNo, "Extra", weddingPrice];
//StoreQuoteData.setQuoteData(sendInfo);
//WhichQuote.setInfoLabel("Wedding");
}
})
<script src="http://code.ionicframework.com/nightly/js/ionic.bundle.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.2.15/angular-ui-router.js"></script>
<link href="http://code.ionicframework.com/nightly/css/ionic.css" rel="stylesheet" />
<ion-view ng-app="demoApp" view-title="Wedding Pax Num" can-swipe-back="true" ng-controller="WeddingPaxCtrl">
<ion-content>
<div class="card">
<div class="item centerText" style="background-color: brown;">
<h2>CALCULATE WEDDING</h2>
</div>
</div>
<div class="padding20Top padding20Bottom">
<h2 class="centerText">Select number of Passengers</h2>
<input ng-model="passengerNo" class="col col-50 col-offset-25 quoteTFieldWithListItems" type="textfield">
<h6 class="centerText">PASSENGERS</h6>
</div>
<ion-toggle ng-model="returnJourney.checked" ng-change="returnJourneyChange()">Return Journey
</ion-toggle>
<ion-toggle ng-show="returnJourney.checked" ng-model="midnightReturn.checked" ng-change="midnightReturn()">Return Journey After Midnight</ion-toggle>
<div>
<h6 class="logText centerText"> {{ getCostBreakDown() }}</h6>
</div>
</ion-content>
<div class="endOfPageButton">
<!---<a href="#/app/home/tester">-->
<button class="button button-full button-clear" ng-click="sendWeddingQuoteInfo()">Submit Quote</button>
<!--</a>-->
</div>
</ion-view>

$scope.passengerNo returns undefined

I'm having problems with receiving the value from an angular expression.
What should happen is when the endOfPageButton is clicked it should pass through the passengerNo and the title. The title is being passed through fine however the passengerNo is always undefined.
Can you tell me how I can get this to update as the passengerNo should be controlled by a textfield and I though the updated value should be automatically injected?
Thanks!
//mainquote.html
<ion-view view-title="{{title}}" can-swipe-back="true">
<ion-content ng-controller="QuoteItemCtrl">
<div class="card">
<div class="item centerText" style="background-color: brown;">
<h6 class="headerTextPadding">REGION</h6>
<h2>{{title}}</h2>
</div>
</div>
<div class="padding40Top padding20Bottom">
<h2 class="centerText">Select number of Passengers</h2>
<input ng-model="passengerNo" class="col col-50 col-offset-25 quoteTField" type="textfield">
<h6 class="centerText">PASSENGERS</h6>
</div>
<div>
<h6 class="logText centerText">SCHOOLS > {{title}} > {{getCost()}} > # {{multiplier}}</h6>
</div>
</ion-content>
<div class="endOfPageButton">
<a href="#/app/home/finalquote">
<button class="button button-full button-clear" ng-click="sendQuoteInfo()">Submit Quote</button>
</a>
</div>
</ion-view>
//controller.js
app.controller('QuoteItemCtrl', function($scope, $rootScope, $stateParams, MakeSchoolQuote, DataThrow) {
var item = DataThrow.getDataObject();
$scope.passengerNo = 49;
$scope.title = item;
$scope.multiplier = MakeSchoolQuote.getMultipler(item);
$scope.getCost = function() {
$scope.pax = $scope.passengerNo;
var cost = $scope.multiplier * $scope.passengerNo
console.log("passenger numbers are" + $scope.pax);
return cost;
}
$scope.sendQuoteInfo = function() {
var data = [$scope.title, $scope.passengerNo, "N/A"];
var sendData = DataThrow.storeDataObject(data);
}
})

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