javascript - How can I show data based on my requirements in angularjs? -


in example based on mobile brand selection mobile models coming checkbox working. based on user brand selection mobile models coming.

for htc brand have models image when user clicks htc models should show images instead of showing

htc 1 x9

desire 820

desire 810s

it should show images check box while submit should pass model name. 1 appreciated

var myapp = angular.module('myapp', []);    myapp.controller('myctrl', function($scope, $http) {    $scope.selectedbrands = [];      $scope.selectbrand = function(selectedphone) {      // if deselect brand      if ($scope.selectedbrands.indexof(selectedphone.brandname) === -1) {        // deselect phones of brand        angular.foreach($scope.phones, function(phone) {          if (phone.brandname === selectedphone.brandname) {            phone.selected = false;          }        });      }    }      $scope.checkselectedphones = function() {      var modelnames = [];      var jsonarr = [];      var submodelarr = [];      var aletrmsg = '';      angular.foreach($scope.phones, function(phone) {        if (phone.selected) {          modelnames.push(phone);          var found = jsonarr.some(function(el) {            if (el.model === phone.brandname) {              el.submodel.push(phone.modelname);              return true;            }          });            if (!found) {            submodelarr.push(phone.modelname);            jsonarr.push({              model: phone.brandname,              brand: 'nokia',              submodel: submodelarr,              city: 'noida',              });            submodelarr = [];          }          }          phone.selected = false;      });      console.log(modelnames.length);      if (modelnames.length == 0) {        alert(modelnames.length ? aletrmsg : 'no phones selected!');      } else {        console.log(jsonarr);      }        $scope.selectedbrands = [];    }      $scope.phones = [{      id: "986745",      brandname: "nokia",      modelname: "lumia 735 ts"    }, {      id: "896785",      brandname: "nokia",      modelname: "nokia asha 230"    }, {      id: "546785",      brandname: "nokia",      modelname: "lumia 510"    }, {      id: "144745",      brandname: "samsung",      modelname: "galaxy trend 840"    }, {      id: "986980",      brandname: "samsung",      modelname: "galaxy a5"    }, {      id: "586980",      brandname: "samsung",      modelname: "galaxy note 4 duos"    }, {      id: "986980",      brandname: "samsung",      modelname: "galaxy a5"    }, {      id: "586980",      brandname: "samsung",      modelname: "galaxy note duos"    }, {      id: "232980",      brandname: "htc",      modelname: "htc 1 x9",      image:"http://cdn.bgr.com/2015/03/bgr-htc-one-m9-1.jpg",    }, {      id: "456798",      brandname: "htc",      modelname: "desire m9",      image:"https://vtcdn.com/sites/default/files/images/2014/6/10/img-1402379949-1.jpg",    }, {      id: "656798",      brandname: "htc",      modelname: "desire 810s",      image:"https://encrypted-tbn2.gstatic.com/images?q=tbn:and9gcqinolh4peebd7h8f5mm9sgdc14oqah91i4xqhjzl3llug1pkov",    }];    });    myapp.filter('unique', function() {    return function(collection, keyname) {      var output = [],        keys = [];        angular.foreach(collection, function(item) {        var key = item[keyname];        if (keys.indexof(key) === -1) {          keys.push(key);          output.push(item);        }      });        return output;    };  });
<div ng-controller="myctrl">    <button ng-click="checkselectedphones()">      check selected phones    </button>      <div ng-repeat="phone in phones | unique:'brandname'">      <label>        <input type="checkbox" ng-true-value="'{{phone.brandname}}'" ng-false-value="''" ng-model="selectedbrands[$index]" ng-change="selectbrand(phone)"> {{phone.brandname}}      </label>    </div>      <br>      <div ng-repeat="brand in selectedbrands track $index" ng-if="brand">      {{brand}}      <div ng-repeat="phone in phones" ng-if="phone.brandname === brand">        <label>          <input type="checkbox" ng-model="phone.selected"> {{phone.modelname}}          </label>      </div>    </div>  </div>

demo

i've made fiddle based on code. here's link: https://jsfiddle.net/0lggc5pn/

only htc phones have image. i've done add code:

<img ng-if="phone.image" src="{{phone.image}}" width="20" height="20"> <span ng-if="!phone.image">{{phone.modelname}}</span> 

so if there image available, image displayed. otherwise display model name.

i've removed method called selectbrand(), didn't seem necessary.


Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

java - Copying object fields -

c++ - Clear the memory after returning a vector in a function -