1

I'm trying to get the value from checkbox so i can bind it . but i keep get it with the condition ( true or false)

When I click the checkbox, it sets my model like so:

[9: true, 15: true] What I want is a flat array like:

[9, 15] Is there a better to get the values from the checkbox and set them in an array on the model?

enter image description here

    <ion-checkbox ng-repeat="model in models | filter:query" ng-model="order.models[model.id]" >{{model.model_number}} </ion-checkbox>
</ion-item>
current order: {{order}}

    .controller('compareModelsCtrl',['$scope','$http', '$state' ,
function($scope, $http, $state,$location) {
$http.get('js/data.json').success(function(data){
$scope.models = data;
$scope.whichmodel = $state.params.modelId;
//$scope.toggleStar = function(model){
// model.star = !model.star;
//}
$scope.order = {};
Amr Ayoub
  • 73
  • 1
  • 9

2 Answers2

1

Ionic framework doesn't do that by default. You need custom function such as format in the code below. Since, I don't have your data.json file, I mimicked two ion-checkbox.

     <ion-content ng-controller="LoginCtrl">
       <ion-checkbox  ng-model="order[0]" ng-change="format()" >1 </ion-checkbox>
       <ion-checkbox  ng-model="order[1]" ng-change="format()" >2</ion-checkbox>
    current order: {{modifiedOrder}}
  </ion-content>

And implemented following function inside the controller.

angular.module('ionic.example', ['ionic'])
.controller('LoginCtrl', function($scope) {
$scope.order={};
$scope.format=function(){
  $scope.modifiedOrder=[];
  angular.forEach($scope.order, function(value, key) {
    if(value){
      $scope.modifiedOrder.push(parseInt(key));
    }
});
}
});

The order now you had before is modifiedOrder See working code here. https://codepen.io/anon/pen/vXYvPG Goodluck!

Dinesh Devkota
  • 1,417
  • 2
  • 18
  • 45
  • id":"0", "model_number":"BT168562.3", "rotor_drawing_number":4.01591, "material":"17-4 PH", "number_of_lobes":"5/6", "contour_length":51.00, "overall_length":55.00, – Amr Ayoub Sep 03 '16 at 10:44
  • i have like 100 id in data json , how can i place them in ng-model="order[]" – Amr Ayoub Sep 03 '16 at 11:41
  • Thank you , it worked like this {{model.model_number}} current order : {{modifiedOrder}} – Amr Ayoub Sep 03 '16 at 13:28
  • Ok, Great! Happy coding. – Dinesh Devkota Sep 03 '16 at 16:50
  • hello Dinesh , i'm trying to show other values with the id from the datajson along with the id , another info here http://stackoverflow.com/questions/39380678/filter-json-using-checkboxes-with-angularjs , can you help me please – Amr Ayoub Sep 08 '16 at 01:31
  • Hello Dinesh , I have an issue with cordova , if you can help me i appericaited it https://stackoverflow.com/questions/39420391/render-local-pdf-files-on-ionic – Amr Ayoub Sep 09 '16 at 22:12
  • Hello dinesh , hope you're doing great , i'm having small issue with JS , – Amr Ayoub Oct 22 '16 at 18:31
  • http://stackoverflow.com/questions/40195575/pure-javascript-asynchronous – Amr Ayoub Oct 22 '16 at 18:31
1

enter image description here

In Html file,

<ion-content padding>
  <ion-input type="text" placeholder="What's Your groups name?"></ion-input>
  <ion-list>

        <ion-item *ngFor="let friend of friendList">
          <ion-label>{{friend.name}}</ion-label>
          <ion-checkbox [(ngModel)]="friendsSelected[friend.name]"></ion-checkbox>
        </ion-item>  
    </ion-list>
    <button ion-button  (click)="save()">Save</button>
</ion-content>

And in your TS file I have created a variable friendsSelected

 friendsSelected:any={};

   save(){
    var array = [];
    for(var i in this.friendsSelected) {
        console.log(this.friendsSelected[i]);
        if(this.friendsSelected[i] == true) {
            array.push(i);
        }
    }
    console.log(array);
  }
thehennyy
  • 4,020
  • 1
  • 22
  • 31
Abraham Mathew
  • 2,029
  • 3
  • 21
  • 42