jeudi 2 février 2017

How to add IDs of a checkbox in angular js

Little overview: Hi, i am working on a school project where a user can choose what items he/she can borrow on the school's computer services office. i have completed the application form where the student/faculty can insert their profile information. I am having issues regarding the checkboxes in anularjs.

I need to total/sum up the IDs of the checkboxes so that it can be inserted on the database as a single int not an array. Here's the code and some information.

        $scope.chkItems = [
        {
            id: 1,
            name: 'Laptop',
            value: null,

        }, {
            id: 2,
            name: 'Headset',
            value: null
        }, {
            id: 4,
            name: 'Projector',
            value: null
        }, {
            id: 8,
            name: 'Tablet',
            value: null
        }, {
            id: 16,
            name: 'Speakers',
            value: null
        }];



$scope.save = function () {

                var chkItemsValue = '';

                $scope.chkItems.forEach(function (Item) {

                    if (Item.value) {
                        chkItemValue +=  chkItem.id;
                    }
                })   
                
                }
<md-input-container class="md-block" flex-gt-sm>
<label class="force-input-label">Items</label>
</br>
<div ng-repeat="chkItem in chkItems">
<md-checkbox name="chkItem.name" ng-model="chkItem.value" ng-true-value=1 ng-false-value=0>
</div>
</md-input-container>

I can insert the value on the database but the value is different instead of getting the total the value i get an array like output.

  • Currently I am getting this:

if i check {Laptop, Projector, Speakers} the total would be 1416

  • What i want:

if i check {Laptop, Projector, Speakers} the total would be 21

I am new to angularjs and web development. Your inputs are greatly appreciated!

ThankYOU!




Aucun commentaire:

Enregistrer un commentaire