国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

Angularjs 制作購物車功能實例代碼

2019-11-20 09:00:37
字體:
來源:轉載
供稿:網友

初學angularJS   閑暇之余做了個小案例。

功能:計算購物車商品的價格,以及刪除購物車商品。

以下是完整案例(jQuery和angularjs需要自己引入)

<!doctype html><html ng-app="myApp"><head><meta charset="utf-8"><title>無標題文檔</title><style>.cursors{cursor:pointer}</style><script src="js/jquery-1.11.1.js"></script><script src="js/angular.min.js"></script><script>var A=angular.module('myApp',[]);//購物車 加A.directive('myAdds',function(){return {link:function(scope, element, attr){element.click(function(){var This=thisangular.forEach(scope.dataList,function(data,index,array){if(attr.items==data.items){data.num=parseInt(data.num)+1;scope.allPrices();scope.$apply() //刷新視圖}});});} }})//購物車 減A.directive('myMinus',function(){return {link:function(scope, element, attr){element.click(function(){var This=thisangular.forEach(scope.dataList,function(data,index,array){if(attr.items==data.items){if(data.num<=1){if(confirm('是否刪除該產品')){data.num=0;$(This).siblings('input').val(0);scope.allPrices();scope.$apply();//delete array[index];scope.dataList.splice(index,1)$(This).parents('tr').remove();}}else{data.num=parseInt(data.num)-1;};scope.allPrices();scope.$apply();}});});} }})//全選,全不選A.directive('allOrcan',function(){return function(scope, element, attr){element.click(function(){var isCheck=$(this).find('input').prop('checked');if(isCheck){$('input[type=checkbox]').prop('checked',true);}else{$('input[type=checkbox]').not($('input[type=checkbox]').eq(0)).prop('checked',false);}angular.forEach(scope.dataList,function(data,index,array){data.Bol=isCheck;})scope.allPrices();scope.$apply();})}})//單選A.directive('oneCheck',function(){return function(scope, element, attr){element.click(function(){var This=thisangular.forEach(scope.dataList,function(data,index,array){if(attr.items==data.items){var isCheck=$(This).prop('checked');data.Bol=isCheck;scope.allPrices();scope.$apply();}})});}})A.controller('myAngular',['$scope','$filter',function($scope,$filter){$scope.dataList=[//初始化購物車的數據{Bol:'false',name:'洗衣機',num:'1',items:'0',oneprice:'900',price:''},{Bol:'false',name:'熱水器',num:'1',items:'1',oneprice:'110',price:''},{Bol:'false',name:'空調',num:'1',items:'2',oneprice:'116',price:''},{Bol:'false',name:'冰箱',num:'1',items:'3',oneprice:'2087',price:''},{Bol:'false',name:'電磁爐',num:'1',items:'4',oneprice:'135',price:''},{Bol:'false',name:'被子',num:'1',items:'5',oneprice:'50',price:''},{Bol:'false',name:'本子',num:'1',items:'6',oneprice:'2',price:''},{Bol:'false',name:'筆',num:'1',items:'7',oneprice:'115',price:''},{Bol:'false',name:'杯子',num:'1',items:'8',oneprice:'12',price:''},{Bol:'false',name:'書',num:'1',items:'9',oneprice:'5',price:''},{Bol:'false',name:'零食',num:'1',items:'10',oneprice:'13',price:''}];//總價格的計算$scope.allPrices=function(){$scope.allprice=0;angular.forEach($scope.dataList,function(data,index,array){data.price=data.num*data.oneprice;if(data.Bol==true){$scope.allprice+=parseInt(data.price);}})return $scope.allprice;};//按單價排序$scope.CartSort=function(arg){angular.forEach($scope.dataList,function(data,index,array){arguments.callee['CartSort('+arg+')']=!arguments.callee['CartSort('+arg+')']$scope.dataList=$filter('orderBy')($scope.dataList,arg,arguments.callee['CartSort('+arg+')'])})}}])</script></head><body ng-controller="myAngular"><table border="1">   <tr>     <td><label all-orcan><input type="checkbox">全選/取消全選 </label></td>     <td>名稱</td>     <td>數量</td>     <td ng-click='CartSort("oneprice")'>單價</td>     <td>價格</td>    </tr>   <tr ng-repeat="data in dataList">     <td><input type="checkbox" one-check items={{data.items}}></td>     <td ng-cloak>{{data.name}}</td>     <td><input type="text" ng-cloak ng-model="data.num" items="{{data.items}}" style="width:50px;text-align:center;"> <button my-adds items="{{data.items}}" ng-class="{cursors:true}" >+</button> <button my-minus items="{{data.items}}" ng-class="{cursors:true}" >-</button> </td>     <td ng-cloak>{{data.oneprice}}</td>     <td ng-cloak>{{data.price}}</td>    </tr>     </table>  <div>總價格:{{allPrices()}}</div></body></html><!--<script>alert(0)</script>-->

效果如圖所示: 

尊重勞動成果,轉載請注明出處(http://blog.csdn.net/sllailcp/article/details/47833315)...

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 丹巴县| 永善县| 云阳县| 崇阳县| 辉南县| 六枝特区| 宜都市| 神池县| 梨树县| 汤阴县| 无棣县| 郓城县| 密云县| 科技| 岑巩县| 峨眉山市| 迁安市| 德州市| 饶阳县| 神木县| 饶河县| 当雄县| 酒泉市| 高邮市| 东安县| 芦山县| 亚东县| 日土县| 竹山县| 阳信县| 静安区| 沐川县| 尼玛县| 襄城县| 泗水县| 广南县| 丹凤县| 石林| 梅河口市| 博白县| 临潭县|