Click here to Skip to main content
15,918,889 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,
I want to show checkboxes in a dropdown html control. I am binding data using angularjs.
This type of control is not available in html. So I have created a div and applied styling to it to look like a dropdown.
Using ng-repeat, I am able to create checkboxes inside it.
When a user checks a checkbox, its text is stored in database on button click.

What I want it, when page loads, the respective checkboxes should get checked whose values we stored in database.

Here is the HTML:
HTML
<dl class="dropdown"> 
  <dt>
      <a href="#">
	<span class="hida">Select</span>    
	<p class="multiSel"></p>  
      </a>
  </dt>
  <dd>
      <div class="mutliSelect">
	<ul>
	   <li class="floatLeft widthFull" data-ng-repeat="code in MultipleRhythm">
		<label class="floatLeft"> <input type="checkbox" class="floatLeft multiCheckbox" value="{{code.Name}}" />{{code.Name}}</label> 
	   </li>
        </ul>
      </div>
   </dd>	
</dl>


Here is js:
JavaScript
$scope.MultipleRhythm = $scope.ManagerCollection["Multiple Rhythm"]["Multiple Rhythm"];

This gives me a collection to loop through using ng-repeat
JavaScript
$scope.SelectedMultipleRhythm = $filter('GetMultiSelectedOptions')($scope.MultipleRhythm, $scope.ManagerCollection["SelectedMultipleRhythm"]);

This gives values selected by user which come from db. This is comma separated.

If user has selected 3 values, I want to show those three checkboxes checked.

What I have tried:

I tried using ng-if, ng checked that if the code.Name == SelectedMultipleRhythm, then show checkbox checked. But no luck.
Posted
Updated 28-May-16 21:29pm

1 solution

use ng-checked[^] directive

try this

HTML
<!DOCTYPE html>
<html lang="en-US">
<head>
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-filter/0.5.8/angular-filter.js"></script>
    <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
    <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
    <script>

        var allItems = [{ Name: 'a' }, { Name: 'b' }, { Name: 'c' }, { Name: 'd' }, { Name: 'e' }, ]
        var selectedItems = 'a,c,e';

        var app = angular.module('myapp', []);
        app.controller('myctrl', myfun);
        function myfun($scope) {
            $scope.MultipleRhythm = allItems;
            $scope.isChecked = function (item) {
                var items = selectedItems.split(',');
                return items.indexOf(item) != -1;
            }
        }
    </script>


</head>
<body ng-controller="myctrl" ng-app="myapp">

    <dl class="dropdown">
        <dt>
            <a href="#">
                <span class="hida">Select</span>
                <p class="multiSel"></p>
            </a>
        </dt>
        <dd>
            <div class="mutliSelect">
                <ul>
                    <li class="floatLeft widthFull" data-ng-repeat="code in MultipleRhythm">
                        <label class="floatLeft"> <input type="checkbox" ng-checked="isChecked(code.Name)" class="floatLeft multiCheckbox" />{{code.Name}}</label>
                    </li>
                </ul>
            </div>
        </dd>
    </dl>

</body>
</html>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900