3

I'm in need of your help. I just starting working with AngularJS and I have encountered a problem with code that is written in Boostrap see snippet.

There is a form and below it I need to add something a few options simillar to radio buttons/check box.

EXAMPLE: So first you pick one or more items from the existing form, and then below it you can choose one of those 2-3 checkboxes. After selecting those, it all sums up in the Total Cost.

View image >>> https://s18.postimg.org/chgy451ix/Screenshot_1.png

var myApp = angular.module("MyApp", []);
myApp.controller("MyCtrl", function($scope){
 $scope.menuCard = [
			{item: "Egg and Cheese", cost:30, active:true},
			{item: "Breakfast Sandwich", cost:400, active:false},
			{item: "Blueberry Pancakes", cost:250, active:false},
			{item: "Cheese Omelet ", cost:220, active:false},
			];	
 $scope.total = 30;
  $scope.toggleActive = function(c)
			{
				c.active = !c.active;				 
				if(c.active)
				{
					$scope.total += c.cost;
				}else
				{
					$scope.total -= c.cost;
				}
			}
});
body{font:20px/1.3 'Open Sans', sans-serif; color: #5e5b64; margin:0; padding:0;}	

.container{
  margin-top:50px;
  
}

span{float:right;}
<html data-ng-app = "MyApp">
<head>
  <title>AngularJS - DataBinding</title>
  <!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">

  <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
  
 </head>
 <body data-ng-controller = "MyCtrl">
  
   <div class = "container">
     <div class = "row">
       
       <div class = "col-md-4 col-md-offset-3">
           <h2 class = "text-center"><strong>Order Form</strong></h2>
           <div class="list-group">
            <button type="button" class="list-group-item" data-ng-repeat = "item in menuCard"  data-ng-click = "toggleActive(item)"   ng-class="{active:item.active}">{{item.item}}<span>{{ item.cost | currency }}</span></button>
          </div>
         <hr/>
         
         <div class="alert alert-success" role="alert">Total Cost: <span>{{ total | currency }}</div>
           
       </div>
       
      
        
     </div>
   </div>
   
   <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
   
   <!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" ></script>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.0/angular.js"></script>
 </body>

</html>

2
  • @gunr2171 I add code Commented Dec 7, 2016 at 19:57
  • which answer helped you? Commented Dec 12, 2016 at 16:08

2 Answers 2

1

You can use radio buttons

<div ng-repeat="lang in languages">
    <input type="radio" name="selectedLang" ng-model="$parent.selectedLang" ng-value="lang" />{{lang}}
  </div>

var myApp = angular.module("MyApp", []);
myApp.controller("MyCtrl", function($scope){
 $scope.menuCard = [
			{item: "Egg and Cheese", cost:30, active:true},
			{item: "Breakfast Sandwich", cost:400, active:false},
			{item: "Blueberry Pancakes", cost:250, active:false},
			{item: "Cheese Omelet ", cost:220, active:false},
			];	
 $scope.total = 30;
$scope.languages=[{"language":"German","cost":40},{"language":"French","cost":50},{"language":"Euro","cost":30}];
  $scope.toggleActive = function(c)
			{
				c.active = !c.active;				 
				if(c.active)
				{
					$scope.total += c.cost;
				}else
				{
					$scope.total -= c.cost;
				}
			}
});
body{font:20px/1.3 'Open Sans', sans-serif; color: #5e5b64; margin:0; padding:0;}	

.container{
  margin-top:50px;
  
}

span{float:right;}
<html data-ng-app = "MyApp">
<head>
  <title>AngularJS - DataBinding</title>
  <!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">

  <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
  
 </head>
 <body data-ng-controller = "MyCtrl">
  
   <div class = "container">
     <div class = "row">
       
       <div class = "col-md-4 col-md-offset-3">
           <h2 class = "text-center"><strong>Order Form</strong></h2>
           <div class="list-group">
            <button type="button" class="list-group-item" data-ng-repeat = "item in menuCard"  data-ng-click = "toggleActive(item)"   ng-class="{active:item.active}">{{item.item}}<span>{{ item.cost | currency }}</span></button>
          </div>
         <hr/>
         <label style="display: inline-block;" ng-repeat="lang in languages">
<input type="radio" name="selectedLang" ng-model="$parent.selectedLang" ng-value="lang" />{{lang.language}}
  </label>
         
         <div class="alert alert-success" role="alert">Total Cost: <span>{{ total + selectedLang.cost | currency }}</div>
           
       </div>
       
      
        
     </div>
   </div>
   
   <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
   
   <!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" ></script>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.0/angular.js"></script>
 </body>

</html>

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you very much, this is almost what I was looking for, but there is one small problem, I believe that you know make it. "Total sum", I want to be one digit, ie, the sum of all the digits that are selected. Here I see next to the radio button "{"language":"German","cost":40}" ; Could this not to see? I just want to write, "German language" not to write in parentheses?
Thank you very much! This is 99% of what I wanted, just one more question. Radio button, whether they be "inline", that are next to each other.
@JasminLjima Change radio button "div" to "label" tag and add style="display: inline-block;"
I still need help about this, look >>> s30.postimg.org/innmht4gx/Screenshot_2.png
0

I would suggest you with this!

<div class="row">
    <div ng-repeat="lang in languages">
        <div class="col-sm-3">
            <input type="radio" name="selected" ng-model="$parent.selectedLang" ng-value="lang" />
            <div class="row">
                <div class="col-sm-1">
                    {{lang.language}}
                </div>
            </div>
            <div class="row">
                <div class="col-sm-1">
                    {{lang.cost}}
                </div>
            </div>
        </div>
    </div>
</div>

The total will look like

<div class="alert alert-success" role="alert">Total Cost:
    <span>{{ total + selected.cost | currency }}</span>
</div>

and the controller will look like

$scope.languages = [{
    "language": "German",
    "cost": 40
  }, {
    "language": "French",
    "cost": 50
  }, {
    "language": "Euro",
    "cost": 30
  }];

Add these to your css

input[type="radio"] {
    -webkit-appearance: checkbox; /* Chrome, Safari, Opera */
    -moz-appearance: checkbox;    /* Firefox */
    -ms-appearance: checkbox;     /* not currently supported */
}

Update 1: added the item name and cost + language and cost as expected in the comment.

Modification is here

 <div class="alert alert-success" role="alert">Total Cost:

        {{selectedproduct.item}} &nbsp; $ {{selectedproduct.cost}}
        + {{selected.language}} &nbsp; $ {{selected.cost}}
          <span>{{ total + selected.cost | currency }}
         </span>
        </div>

and the same is updated in the plunker.

Here is the live DEMO in plunker

3 Comments

This is exactly what I needed! But there's a problem, when I checks, for example, "German" , it will not change the total sum, for example: egg and cheese $30 + German $40 = $70
- Did I give a failing grade or someone else? If I am, I'm sorry, please, not on purpose, I just started using this page, still all investigating and click. Please help me, and tell me how to change it!? I hope you understand me, I'm a beginner!
When I click on up arrow it writes >>> s15.postimg.org/c6sw0xxi3/Screenshot_1.png

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.