I have a working code that gives the sum of the total item in a json file after ticking one or all of the checkboxes
Here is my controller
$scope.testFood = [{ name: item1, price: 400 }, { name: item2, price: 800 }, { name: item3, price: 1000 }];
$scope.selectedItems = [];
$scope.value = function (isSelected, item) {
if (isSelected == true) {
$scope.selectedItems.push(item);
} else {
// console.log(item.name);
angular.forEach($scope.selectedItems, function (itemRmv, $index) {
if (itemRmv.itemid == item.itemid) {
$scope.selectedItems.splice($index, 1);
}
})
}
console.log($scope.selectedItems);
}
The filter:
.filter('getprice', function () {
return function (value, property) {
var total = 0;
angular.forEach(value, function (val, index) {
total = total + Number(val.price)
});
return total.toFixed(2);
}
});
The html template:
<ul class="list" ng-repeat ="item in testFood">
<li class="item item-checkbox">
<label class="checkbox">
<input type="checkbox" ng-model="item.isSelected" ng-change="value(item.isSelected,item)">
</label>
{{item.package_name}} <p>N {{item.price}}</p>
</li> X
<label class="item item-input">
<input type="number" value="0" min="0" step="1">
</label>
</ul>
Calling {{selectedItems | getprice}} gives the total price.
I want the quantity for each product to be calculated along side the total price, how do I do that?
Thanks.
Related
How we can get the value of checkbox an input to an array?
Here is my code, I've tried this:
<cbComp
v-for="name in names"
v-bind:key="name"
v-model="selected_name[name]">
<span> {{ name }} </span>
</cbComp>
Name: {{ selected_name }}
data () {
return {
names: ['Michael', 'Millie', 'Brown'],
selected_name: []
}
},
This was the cbComp template:
<template>
<label class="checkbox">
<input
:id="cb"
type="checkbox"
:value="modelValue"
v-model="computedValue"
>
<label
:for="cb">
<slot/>
</label>
</label>
</template>
This is the javascript:
export default {
data: {
updateValue: this.value
},
props: {
value: undefined,
modelValue: {
type: undefined,
default: false
}
},
computed: {
computedValue: {
get () {
return this.updateValue
},
set (value) {
this.updateValue = value
this.$emit('input', value)
}
}
},
watch: {
value (value) {
this.updateValue = value
}
}
}
It turns out there is no error, and the selected name won't appear on {{ selected_name }}
You simply need to bind your v-model to the selected_name property. Here is a bare minimum example for you to work off of:
<html>
<div id="app">
<p>Names: {{ selected_name.join(', ') }}</p>
<label for="maxCheckbox">Max</label>
<input type="checkbox" value="Max" v-model="selected_name" id="maxCheckbox">
<label for="markCheckbox">Mark</label>
<input type="checkbox" value="Mark" v-model="selected_name" id="markCheckbox">
<label for="johnCheckbox">John</label>
<input type="checkbox" value="John" v-model="selected_name" id="johnCheckbox">
</div>
<script src="https://unpkg.com/vue#2"></script>
<script>
new Vue({
el: '#app',
data() {
return {
selected_name: []
}
}
});
</script>
</html>
As a side note, I would probably rename your selected_name to be more clear like selected_names or more commonly you'll find selectedNames camelCase.
i'm developing an app that should have a form with select boxes that are populated dinamically with JSON arrays.
My problem is that the controller is correctly executed but the select options are not populated from the controller, there are empty.
This id my code:
<script>
angular.module('ionicApp', [
])
.controller('TimesController', function () {
console.log("JavaScript Function");
var data = <?php echo $json; ?>;
console.log(data);
var result = {
events: [],
events2: [],
events3: [],
schedules: [],
schedules2: [],
schedules3: []
};
var events = data;
//console.log("events");
//console.log(events);
var events2 = data;
var events3 = data;
var dates = [];
var sedi = [];
var professionisti = [];
console.log("qua3");
for (var i = 0; i < events.length; i++) {
if (dates.indexOf(events[i].day) === -1) {
var date = events[i].day;
dates.push(date);
result.events.push({
date: date
});
}
if (sedi.indexOf(events[i].sede) === -1) {
var sede = events[i].sede;
sedi.push(sede);
result.events2.push({
sede: sede
});
}
if (professionisti.indexOf(events[i].professionista) === -1) {
var professionista = events[i].professionista;
professionisti.push(professionista);
result.events3.push({
professionista: professionista
});
}
var a = 0;
var found = false;
while (a < result.schedules2.length) {
if (events[i].sede === result.schedules2[a].sede && events[i].professionista === result.schedules2[a].professionista) {
found = true;
}
a++;
}
if (found == false) {
result.schedules2.push({
sede: events[i].sede,
professionista: events[i].professionista
});
}
var a = 0;
var found = false;
while (a < result.schedules3.length) {
if (events[i].sede === result.schedules3[a].sede && events[i].professionista === result.schedules3[a].professionista && events[i].day === result.schedules3[a].date) {
found = true;
}
a++;
}
if (found == false) {
console.log("OLEE");
result.schedules3.push({
sede: events[i].sede,
professionista: events[i].professionista,
date: events[i].day
});
}
result.schedules.push({
sede: events[i].sede,
professionista: events[i].professionista,
date: events[i].day,
time: events[i].time
//console.log("qua4");
});
}
console.log(result);
})
</script>
<div ng-app="ionicApp" ng-controller="TimesController as ctrl">
<div align="center">
<p style="font-size: 22px;">Inserisci appuntamento</p>
<br>
<div class="list" align="center">
<div class="input-label">
</div>
<select style="width: 75%;" ng-options="event as event.sede for event in ctrl.data.events2" ng-model="ctrl.form.sede">
<option value="" disabled>Seleziona sede</option>
</select>
</div><br>
<div class="list" align="center">
<div class="input-label">
</div>
<select style="width: 75%;" ng-options="schedule as schedule.professionista for schedule in ctrl.data.schedules2| filter: { sede: ctrl.form.sede.sede}" ng-model="ctrl.form.professionista" ng-disabled="!ctrl.form.sede">
<option value="" disabled>Seleziona professionista</option>
</select>
</div><br>
<div class="list" align="center">
<div class="input-label">
</div>
<select style="width: 75%;" ng-options="schedule as schedule.date for schedule in ctrl.data.schedules3| filter: { professionista: ctrl.form.professionista.professionista, sede: ctrl.form.sede.sede}" ng-model="ctrl.form.giorno" ng-disabled="!ctrl.form.professionista">
<option value="" disabled>Seleziona giorno</option>
</select>
</div><br>
<div class="list" align="center">
<div class="input-label">
</div>
<select style="width: 75%;" ng-options="schedule as schedule.time for schedule in ctrl.data.schedules| filter: { date: ctrl.form.giorno.date, professionista: ctrl.form.professionista.professionista, sede: ctrl.form.sede.sede}" ng-model="ctrl.form.ora" ng-disabled="!ctrl.form.giorno">
<option value="" disabled>Seleziona orario</option>
</select>
</div><br>
</div>
</div>
Can someone help me to solve it?
Thank's
angular.module('app', []).controller('MyCtrl', function ($scope) {
$scope.list = [{
id: 27,
name: "loruth water point",
latitude: 4.453488123,
longitude: 35.36021409
}, {
id: 28,
name: "kibish",
latitude: 5.286289986,
longitude: 35.82917452
}, {
id: 30,
name: "Ekalale",
latitude: 4.434588531,
longitude: 35.72135923
}, {
id: 34,
name: "karubangorok",
latitude: 4.506236007,
longitude: 35.4201746
}, {
id: 35,
name: "nakitoe kakumon",
latitude: 4.214576564,
longitude: 35.35912495
}, {
id: 36,
name: "kaikor mission",
latitude: 4.516645656,
longitude: 35.42262991
}];
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app">
<div ng-controller="MyCtrl">
<h2>List:</h2>
<p>
<select ng-model="selectedItem" ng-options="item.name for item in list">
<option value="">-- choose --</option>
</select>
</p>
<p>
<h2>Selected:</h2>
{{selectedItem.name}}
</p>
</div>
</div>
Check this example once.
I am doing a project using MEAN Stack and still learning the parts of AngularJS.
I have a form which I get filled from the user. It contains a series of checkboxes which selects the students and stores them in an array. For each students, I intend to take 2 values(correct answers, incorrect answers) for that student. How do I model these two inputs to two variables and get the values in the controller?
The problem is that if one time, I select 3 students, there will be 3 pairs of correct and incorrect answers. If other time I have different students selected, how do I map the answers to those students?
View.html:
<label ng-repeat="studentA1 in studentsA1">
<input
type="checkbox"
ng-model="selectedStudentsA1"
ng-checked="existA1(studentA1)"
value="studentA1"
ng-click="toggleSelectionA1(studentA1)">
{{studentA1}}
</label>
{{selectedStudentsA1}}
<div class="col-md-3">
<h6>Correct Answers</h6>
<input type="number" min="0" name="correctAnswers" ng-model="correctAnswers" required>
</div>
<div class="col-md-3">
<h6>Incorrect Answers</h6>
<input type="number" name="incorrectAnswers" ng-model="incorrectAnswers" min="0" required>
</div>
Controller.js:
$scope.studentsA1 = ['Akhilesh', 'Prathamesh', 'Mandar', 'Sunmay'];
$scope.selectedStudentsA1 = [];
$scope.existA1 = function(item) {
return $scope.selectedStudentsA1.indexOf(item) > -1;
};
$scope.toggleSelectionA1 = function(item) {
var idx = $scope.selectedStudentsA1.indexOf(item);
if (idx > -1) {
$scope.selectedStudentsA1.splice(idx, 1);
} else {
$scope.selectedStudentsA1.push(item);
}
};
$scope.checkAllA1 = function() {
if ($scope.selectAllA1) {
angular.forEach($scope.studentsA1, function(item) {
var idx = $scope.selectedStudentsA1.indexOf(item);
if(idx >= 0) {
return true;
} else {
$scope.selectedStudentsA1.push(item);
}
});
} else {
$scope.selectedStudentsA1 = [];
}
};
Check if this helps you
I have made a fiddle for you
If you are looking more please comment them
In View Side
<div ng-app ng-controller="mainController">
<table>
<tr>
<th>
<input type="checkbox" ng-model="selectAll" ng-click="selectAllCheckBox(selectAll)">
</th>
<th>Name</th>
<th>Correct</th>
<th>Wrong</th>
</tr>
<tr ng-repeat="studentA1 in studentsA1">
<td>
<input type="checkbox" ng-model="studentA1.selected">
</td>
<td> {{studentA1.Name}}</td>
<td>
<input type="number" ng-model="studentA1.Correct" />
</td>
<td>
<input type="number" ng-model="studentA1.Wrong" />
</td>\
</tr>
</table>
<br> {{selectedStudentsA1}}
<a ng-click="selectedStudents()">Submit</a>
</div>
In Controller Side
function mainController($scope) {
$scope.studentsA1 = [{
selected: false,
Name: 'Akhilesh',
Correct: '',
Wrong: ''
}, {
selected: false,
Name: 'Prathamesh',
Correct: '',
Wrong: ''
}, {
selected: false,
Name: 'Mandar',
Correct: '',
Wrong: ''
}, {
selected: false,
Name: 'Sunmay',
Correct: '',
Wrong: ''
}];
$scope.selectedStudents = function() {
$scope.selectedStudentsA1 = $scope.studentsA1.filter(i => i.selected == true);
$scope.correctAnswers = $scope.selectedStudentsA1.length;
$scope.incorrectAnswers = $scope.studentsA1.length - $scope.selectedStudentsA1.length;
}
$scope.selectAllCheckBox = function(value) {
$scope.studentsA1.forEach(function(item) {
item.selected = value;
});
}
}
https://jsfiddle.net/athulnair/9r8sbwx4/
Hi I am new to angularjs I have a problem in calculation the sum of the field my code is like this
<div ng-repeat="(key,item) in expenses_data | groupBy: 'category_name'">
<h4 ng-cloak><i class="fa fa-plus-circle"></i> {{ key }}</h4>
<div class="form-group" ng-repeat="expn_head in item">
<label class="col-sm-3 control-label">{{ expn_head.name }}</label>
<div class="col-sm-2">
<input type="text" class="form-control input-sm" ng-model="expenses.expenditure_head[expn_head.id]">
</div>
</div>
</div>
How do I sum up all the expenses.expenditure_head value entered and put it in the new text field. Is there any function like this:
<input type=text ng-value="{{ sum() }}">
and js
$scope.sum = function() {
var total = 0;
for (var i = 0; i < $scope.expenses.expenditure_head; i++) {
var myValue = $expenses.expenditure_head[i];
total += myValue;
}
return total;
}
json
[
{
"id":23,
"name":"Agency Commission",
"expenditure_category_id":1,
"category_name":"Main"
},
{
"id":22,
"name":"Bonus to Local Staff",
"expenditure_category_id":1,
"category_name":"Main"
},
{
"id":48,
"name":"Advance for Expenses",
"expenditure_category_id":2,
"category_name":"Other Dept's Budget Exp"
},
{
"id":49,
"name":"Agency TDS",
"expenditure_category_id":2,
"category_name":"Other Dept's Budget Exp"
}
]
Can anyone show me the right direction? I have trying this for a day.
The right way is to do so inside the controller. Create a function call it getSum()
$scope.getSum = function(){
var total = 0;
for(var i = 0; i < $scope.someVar; i++){
var myValue = $someVar[i];
sum += myValue;
}
return sum;
};
Then inside your code you do something like
<td>Total: {{ getSum() }}</td>
How to make the button functional, and the button has default value, for example button A-B has a range for a to b of first letter to be filter. Thanks, Sorry for my last post, it was hard to understand. =)
var app = angular.module('app', []);
app.filter('startsWithLetter', function () {
return function (items, letter) {
var filtered = [];
var letterMatch = new RegExp(letter, 'i');
for (var i = 0; i < items.length; i++) {
var item = items[i];
if (letterMatch.test(item.name.substring(0, 1))) {
filtered.push(item);
}
}
return filtered;
};
});
app.controller('PersonCtrl', function () {
this.friends = [{
name: 'Andrew'
}, {
name: 'Baldo'
}, {
name: 'Carlo'
}, {
name: 'Delo'
}, {
name: 'Emman'
}, {
name: 'Ferman'
}];
});
</style>
<script src="//code.angularjs.org/1.3.0-beta.7/angular.js"></script>
<style>
<div ng-app="app">
<div ng-controller="PersonCtrl as person">
<input type="text" ng-model="letter" placeholder="Enter a letter to filter">
<button>A-B</button>
<button>C-D</button>
<button>E-F</button>
<ul>
<li ng-repeat="friend in person.friends | startsWithLetter:letter">
{{ friend }}
</li>
</ul>
</div>
</div>
Is this what you meant? Did you want clicking each button to filter by those 2 letters?
All I changed was set the markup for the buttons to be:
<button ng-click="letter='[AB]'">A-B</button>
<button ng-click="letter='[CD]'">C-D</button>
<button ng-click="letter='[EF]'">E-F</button>
var app = angular.module('app', []);
app.filter('startsWithLetter', function () {
return function (items, letter) {
var filtered = [];
var letterMatch = new RegExp(letter, 'i');
for (var i = 0; i < items.length; i++) {
var item = items[i];
if (letterMatch.test(item.name.substring(0, 1))) {
filtered.push(item);
}
}
return filtered;
};
});
app.controller('PersonCtrl', function () {
this.friends = [{
name: 'Andrew'
}, {
name: 'Baldo'
}, {
name: 'Carlo'
}, {
name: 'Delo'
}, {
name: 'Emman'
}, {
name: 'Ferman'
}];
});
</style>
<script src="//code.angularjs.org/1.3.0-beta.7/angular.js"></script>
<style>
<div ng-app="app">
<div ng-controller="PersonCtrl as person">
<input type="text" ng-model="letter" placeholder="Enter a letter to filter">
<button ng-click="letter='[AB]'">A-B</button>
<button ng-click="letter='[CD]'">C-D</button>
<button ng-click="letter='[EF]'">E-F</button>
<ul>
<li ng-repeat="friend in person.friends | startsWithLetter:letter">
{{ friend }}
</li>
</ul>
</div>
</div>