Inline editing of Checkbox list

Angular implementation of xeditable.

by rodhartzell

HTML

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.min.js"></script>
<script src="http://vitalets.github.io/angular-xeditable/dist/js/xeditable.js"></script>
<link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css">
<link rel="stylesheet" href="http://vitalets.github.io/angular-xeditable/dist/css/xeditable.css">
<script src="http://vitalets.github.io/angular-xeditable/libs/checklist-model.js"></script>
<h4>Angular-xeditable Checklist (Bootstrap 3)</h4>
<div ng-app="app" ng-controller="Ctrl">
    <a editable-checklist="customer.DiscoveredVia" 
       e-ng-options="s.demographicType as s.demographicDesc for s in demographicTypes"
       onbeforesave="updateDemog(demographicTypes, $data)">
        {{ showDemog() }}
    </a>
</div>

CSS

div[ng-app] { margin: 50px; }

JavaScript

var app = angular.module("app", ["xeditable", "checklist-model"]);

app.run(function(editableOptions) {
  editableOptions.theme = 'bs3';
});

app.controller('Ctrl', function($scope, $filter) {
  $scope.customer = {
    DiscoveredVia: [{
      'demographicType': 'EmailMail',
      'deomgraphicDesc': 'Email/Mail'
    }, {
      'demographicType': 'TradeAssoc',
      'demogrpahicDesc': 'Trade Publication'
    }]
  };

  $scope.demographicTypes = [{
    'demographicType': 'EmailMail',
    'demographicDesc': 'Email/Mail'
  }, {
    'demographicType': 'TradeAssoc',
    'demographicDesc': 'Trade Association'
  }, {
    'demographicType': 'FMCSA',
    'demographicDesc': 'FMCSA'
  }, {
    'demographicType': 'Print',
    'demographicDesc': 'Print Publication'
  }];

  $scope.showDemog = function() {
    var selected = [];
    angular.forEach($scope.demographicTypes, function(s) {
      angular.forEach($scope.customer.DiscoveredVia, function(v) {
        if (v.demographicType.indexOf(s.demographicType) >= 0) {
          selected.push(s.demographicDesc);
        }
      });
    });
    return selected.length ? selected.join(', ') : 'Not set';
  };

  $scope.updateDemog = function(feedSource, option) {
    $scope.customer.DiscoveredVia = [];
    angular.forEach(option, function(v) {
      var feedObj = $filter('filter')($scope.demographicTypes, {
        demographicType: v
      });
      if (feedObj.length) {
        $scope.customer.DiscoveredVia.push(feedObj[0]);
      }
    });
    return $scope.customer.DiscoveredVia.length ? false : 'Not set';
  };
});