Home > Web Front-end > JS Tutorial > body text

AngularJS implementation of shopping cart selection and reverse selection function example sharing

小云云
Release: 2018-01-03 11:33:49
Original
1920 people have browsed it

This article mainly introduces AngularJS to implement the function of selecting all and inverting the selection in the shopping cart. Friends who need it can refer to it. I hope it can help everyone.

Without further ado, I will post the code directly for you. The specific code is as follows;

<!DOCTYPE html>
<html lang="en" ng-app="testMo">
<head>
 <meta charset="UTF-8">
 <title></title>
 <link rel="stylesheet" href="css/bootstrap.css" rel="external nofollow" >
 <style>
 .p1{
  margin: 20px;
 }
 </style>
</head>
<body>
<p ng-controller="testCtrl" class="p1">
 <h4>angularJS--购物车实现全选/取消全选</h4>
 <button type="button" class="btn btn-info" ng-click="addProduct()">添加商品</button>
 <button type="button" class="btn btn-danger" ng-click="deleteProduct()">删除商品</button>
 <br><br>
 <table class="table table-bordered table-responsive" >
 <thead>
 <td>操作</td>
 <td>check状态</td>
 <td>商品名称</td>
 <td>单价</td>
 <td>数量</td>
 <td>小计</td>
 </thead>
 <tr ng-repeat="p in cart" >
  <td><input type="checkbox" ng-checked="p.checked" ng-click="echoChange(p.id,p.checked,selectAll)"></td>
  <td>{{p.checked}}||{{p.checked}}</td>
  <td>{{p.name}}</td>
  <td>单价:¥{{p.price}}</td>
  <td>数量:<input type="number" ng-model="p.count" min="0" value="p.count"></td>
  <td>小计:¥{{p.sum}}</td>
 </tr>
 </table>
 <br>
 <input type="checkbox" ng-model="selectAll" ng-click="selectAllClick(selectAll)"><span ng-hide="selectAll" >全选</span><span ng-show="selectAll">取消全选</span>
 <br><br>
 已选择<span>{{jishuqi}}</span>件商品,总金额:<span>¥{{ sumTotal }}</span>
</p>
<script src="../js/angular.js"></script>
<script>
 angular.module('testMo',['ng']).controller('testCtrl',function($scope){
// $scope.p1=new Object();
// $scope.p1.price=10;
// $scope.p1.count=1;
 //购物车应该是一个数组
 $scope.selectAll=false;//全选默认为false
 $scope.cart=[{id:0,name:'商品0',price:10,count:5,sum:10,checked:false}];
 $scope.addProduct= function (){
  var p=new Object();
  p.id=$scope.cart.length;
  p.name='商品'+ p.id
  p.price=Math.floor(Math.random()*100);//对数值向下取整
  p.count=1;
  p.sum= p.price* p.count;
  p.checked=false;
  $scope.cart.push({id: p.id,name: p.name,price:p.price,count: p.count,sum: p.sum,checked: p.checked});
  console.log($scope.cart);
 }
 //删除商品
 $scope.deleteProduct= function (){
  $scope.cart.pop();//删除数组中的最后的一个元素,并且返回这个元素,会改变数组里的元素
 }
 //全选按钮check的点击事件
 $scope.selectAllClick= function (sa) {
  for(var i=0;i<$scope.cart.length;i++){
  $scope.cart[i].checked=sa;
  }
 }
 //单个数据的check事件
 $scope.echoChange=function(id,ch,se){
  $scope.cart[id].checked=!ch;
  //当所有都选中时,全选也要被勾选
  var cc=0;//计算当前数组中checked为真的数目
  for(var i=0;i<$scope.cart.length;i++){
//  if($scope.cart[i].checked==true){
//   cc++;
//  }
  $scope.cart[i].checked?cc++:cc;
  }
  $scope.selectAll=(cc==$scope.cart.length);//当为真的数目=数组长度时,证明全部勾选
//  console.log($scope.selectAll);
 }
 //监控数据
 $scope.$watch('cart',function(newValue,oldValue,scope){
  $scope.sumTotal=0; //总计
  $scope.jishuqi=0; //计数器
  for(var i in newValue) {
  var sumN = newValue[i].count * newValue[i].price; //计算出新的结果
  $scope.cart[i].sum = sumN.toFixed(2); //保留两位小数并且把它赋值给元数据;
  if (newValue[i].checked) {
   $scope.sumTotal += sumN;
   $scope.jishuqi++;
//   console.log($scope.sumTotal);
//   console.log($scope.jishuqi);
  }
  }
 },true);
 /*$watch简介:在digest执行时,如果watch观察的的value与上一次执行时不一样时,就会被触发。
  AngularJS内部的watch实现了页面随model的及时更新。
  $watch方法在用的时候主要是手动的监听一个对象,但对象发生变化时触发某个事件。
  $watch(watchFn,watchAction,deepWatch);
  如果不加第三个参数,那么只会监听cart数组,只有当cart引用改变时才会触发,因此当需要监听一些引用对象时需要把第三个参数设置成true。
  */
 });
</script>
</body>
</html>
Copy after login

PS: Let me share with you the code of the angularjs shopping cart. The specific code is as follows Shown:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>购物车</title>
  <script type="text/javascript" src="js/angular.js"></script>
</head>
<body ng-app="product" ng-controller="productController">
<center>
<h2>商品列表</h2>
<p class="container">
  <!--导航栏-->
  <nav>
    <p >
      <p id="bs-example-navbar-collapse-1">
        <p>
          <input type="text" ng-model="search" placeholder="产品名称">   
           产品价格:
          <select>
            <option>0-1000</option>
            <option>1000-2000</option>
            <option>2000-5000</option>
          </select>   
          <input type="button" style="background:#FF0000" value="全部删除" ng-click="removeAll()">
        </p>
      </p>
    </p>
  </nav><br />
  <table border="1 solid" cellpadding="10" cellspacing="0">
    <thead>
    <tr>
      <th ng-click="sortProduct(&#39;id&#39;)">
        产品编号
        <span></span>
      </th>
      <th ng-click="sortProduct(&#39;name&#39;)">
        产品名称
        <span></span>
      </th>
      <th ng-click="sortProduct( &#39;price&#39;)">
        产品价格
        <span></span>
      </th>
      <th>
        操作
        <span></span>
      </th>
    </tr>
    </thead>
    <tbody>
    <tr ng-repeat="item in productList | filter:{ &#39;name&#39;:search} | orderBy:(orderSign+orderColumn) ">
      <td>
        {{item.id}}
      </td>
      <td>
        {{item.name}}
      </td>
      <td>
        {{item.price | currency:'(RMB)'}}
      </td>
      <td>
         <input type="button" style="background:#FF0000" value="删除"  ng-click="delProduct(item.name)">
      </td>
    </tr>
    </tbody>
  </table>
</p>
<script>
  angular.module('product',[])
    .factory('productList',function(){
      return [
        { id:910,name:"imac",price:15400 },
        { id:80,name:"iphone",price:5400 },
        { id:29,name:"ipad",price:14200 },
        { id:500,name:"ipad air",price:23400 },
        { id:1200,name:"ipad mini",price:22000},
        { id:100,name:"android",price:9990 }
      ]
    })
    .controller('productController',function($scope,productList){
      /*$scope.search = "ipad";//定义一个变量
      alert($scope.search);*/
      $scope.productList=productList
      $scope.orderColumn='name'; //排序字段
      $scope.orderSign='-';   //为空时正序 为负号时倒序
      $scope.sortProduct=function(sortColumn){ //点击列标题排序事件
        $scope.orderColumn=sortColumn;//觉得按照那一列进行排序
        if($scope.orderSign=="-"){
          $scope.orderSign="";
        }else{
          $scope.orderSign='-';
        }
      };
      //删除产品
      $scope.delProduct = function(name){
        //alert(name);
        if(name!=""){
          if(confirm("是否删除"+name+"商品") ){
            var p;
            for (index in $scope.productList) {
              p = $scope.productList[index];
              if(p.name == name){
                $scope.productList.splice(index,1);
              }
            }
          }
        }
      }
      //清空购物车
$scope.removeAll = function(){
if(confirm("你确定要清空购物车所有商品吗?")){
$scope.productList = [];
}
}
    });
</script>
</center>
</body>
</html>
Copy after login

Okay, the code ends here.

Related recommendations:

JS checkbox all-selected and inverted method

JavaScript checkbox all-selected and inverted event Detailed implementation explanation

JQuery implementation of selecting all check boxes in the list and inverting the selection function encapsulation

The above is the detailed content of AngularJS implementation of shopping cart selection and reverse selection function example sharing. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!