最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

AngularJS 實現(xiàn)購物車全選反選功能

 更新時間:2017年10月24日 17:13:52   作者:問天無畏  
這篇文章主要介紹了AngularJS 實現(xiàn)購物車全選反選功能,需要的朋友可以參考下

廢話不多說了,直接給大家貼代碼了,具體代碼如下所示; 

<!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>
 .div1{
  margin: 20px;
 }
 </style>
</head>
<body>
<div ng-controller="testCtrl" class="div1">
 <h4>angularJS--購物車實現(xiàn)全選/取消全選</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狀態(tài)</td>
 <td>商品名稱</td>
 <td>單價</td>
 <td>數(shù)量</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>數(shù)量:<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>
</div>
<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;
 //購物車應該是一個數(shù)組
 $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);//對數(shù)值向下取整
  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();//刪除數(shù)組中的最后的一個元素,并且返回這個元素,會改變數(shù)組里的元素
 }
 //全選按鈕check的點擊事件
 $scope.selectAllClick= function (sa) {
  for(var i=0;i<$scope.cart.length;i++){
  $scope.cart[i].checked=sa;
  }
 }
 //單個數(shù)據(jù)的check事件
 $scope.echoChange=function(id,ch,se){
  $scope.cart[id].checked=!ch;
  //當所有都選中時,全選也要被勾選
  var cc=0;//計算當前數(shù)組中checked為真的數(shù)目
  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);//當為真的數(shù)目=數(shù)組長度時,證明全部勾選
//  console.log($scope.selectAll);
 }
 //監(jiān)控數(shù)據(jù)
 $scope.$watch('cart',function(newValue,oldValue,scope){
  $scope.sumTotal=0; //總計
  $scope.jishuqi=0; //計數(shù)器
  for(var i in newValue) {
  var sumN = newValue[i].count * newValue[i].price; //計算出新的結果
  $scope.cart[i].sum = sumN.toFixed(2); //保留兩位小數(shù)并且把它賦值給元數(shù)據(jù);
  if (newValue[i].checked) {
   $scope.sumTotal += sumN;
   $scope.jishuqi++;
//   console.log($scope.sumTotal);
//   console.log($scope.jishuqi);
  }
  }
 },true);
 /*$watch簡介:在digest執(zhí)行時,如果watch觀察的的value與上一次執(zhí)行時不一樣時,就會被觸發(fā)。
  AngularJS內部的watch實現(xiàn)了頁面隨model的及時更新。
  $watch方法在用的時候主要是手動的監(jiān)聽一個對象,但對象發(fā)生變化時觸發(fā)某個事件。
  $watch(watchFn,watchAction,deepWatch);
  如果不加第三個參數(shù),那么只會監(jiān)聽cart數(shù)組,只有當cart引用改變時才會觸發(fā),因此當需要監(jiān)聽一些引用對象時需要把第三個參數(shù)設置成true。
  */
 });
</script>
</body>
</html>

PS:下面給大家分享angularjs 購物車的代碼,具體代碼如下所示:

<!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>
<div class="container">
  <!--導航欄-->
  <nav>
    <div >
      <div id="bs-example-navbar-collapse-1">
        <div>
          <input type="text" ng-model="search" placeholder="產品名稱">   
           產品價格:
          <select>
            <option>0-1000</option>
            <option>1000-2000</option>
            <option>2000-5000</option>
          </select>&nbsp;&nbsp;&nbsp;
          <input type="button" style="background:#FF0000" value="全部刪除" ng-click="removeAll()">
        </div>
      </div>
    </div>
  </nav><br />
  <table border="1 solid" cellpadding="10" cellspacing="0">
    <thead>
    <tr>
      <th ng-click="sortProduct('id')">
        產品編號
        <span></span>
      </th>
      <th ng-click="sortProduct('name')">
        產品名稱
        <span></span>
      </th>
      <th ng-click="sortProduct( 'price')">
        產品價格
        <span></span>
      </th>
      <th>
        操作
        <span></span>
      </th>
    </tr>
    </thead>
    <tbody>
    <tr ng-repeat="item in productList | filter:{ 'name':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>
</div>
<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>

好了,代碼到此結束。

總結

以上所述是小編給大家介紹的AngularJS 實現(xiàn)購物車全選反選功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網站的支持!

相關文章

最新評論

襄城县| 盐边县| 海盐县| 南昌县| 卫辉市| 蓬安县| 丰都县| 崇文区| 栾城县| 司法| 鄂州市| 喜德县| 临城县| 平安县| 宜君县| 西昌市| 宽城| 甘孜县| 开鲁县| 成都市| 米脂县| 明光市| 昌宁县| 左权县| 濮阳县| 沅陵县| 上蔡县| 诸暨市| 元谋县| 化州市| 精河县| 江口县| 左贡县| 郓城县| 万盛区| 比如县| 武功县| 百色市| 平果县| 昌黎县| 城固县|