Angular购物车

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <script type="text/javascript" src="../agulajs/angular.js" ></script>
        <script>
            var app=angular.module("myApp",[]);
            app.controller("myCtrl",function($scope){
                $scope.goods=[{
                             id:290,
                             name:"ipad",
                             price:1420,
                             state: false
                             
                         },{
                            id:80,
                             name:"iphone",
                             price:5400,
                             state: false
                         },{
                            id:910,
                             name:"imac",
                             price:15400,
                             state: false
                         },{
                            id:500,
                             name:"ipad air",
                             price:2340,
                             state: false
                         }];
                              
                  /*//点击表头对其进行正逆排序
                    $scope.toog=function(tit){
                      $scope.title=tit;
                      $scope.desc=!$scope.desc;
                  }*/
                  
                  //点击列明进行排序
                $scope.orderFlag = "";
                $scope.orderLine = "id";
                $scope.toog = function(line) {
                    $scope.orderLine = line;
                    if($scope.orderFlag == "") {
                        $scope.orderFlag = "-";
                    } else {
                        $scope.orderFlag = "";
                    }
                }
                  
                  //根据index进行删除
                  $scope.delte=function(delName){
                      for(index in $scope.goods){
                       if(delName == $scope.goods[index].name){
                          if(confirm("确认要删除吗")){
                         
                         $scope.goods.splice(index,1)
                          
                          }
                       }else{
                         }
                      }
                 }
            
            //下拉菜单筛选选中商品价格
            $scope.produce="--请选择--";
            $scope.isifshow=function(price){
                if($scope.produce=="--请选择--"){
                    return true;
                }else{
                    var arr=$scope.produce.split("-");
                    var priceMin = arr[0];
                    var priceMax = arr[1];
                    if(price<priceMin || price>priceMax){
                        return false;
                    }else{
                        return true;
                    }
                }
            }
        
            //定义下拉菜单排序规则
            $scope.selOrder;
            $scope.orderSel=function(){
                if($scope.selOrder == "id"){
                    $scope.orderFlag="";
                    $scope.orderLine="id";
                }else if($scope.selOrder == "-id"){
                    $scope.orderFlag="-";
                    $scope.orderLine="id";
                }else if($scope.selOrder == "price"){
                    $scope.orderFlag="";
                    $scope.orderLine="price";
                }else if($scope.selOrder == "-price"){
                    $scope.orderFlag="-";
                    $scope.orderLine="price";
                }
            }
           
           //修改价格
           $scope.update=function(price){
                //获得价格
                for(index in $scope.goods){
                    if(price==$scope.goods[index].price){
                     var result=parseInt(window.prompt("请输入要修改的价格",price));
                    
                     if(result<0){
                         alert("请输入有误,请重新更改");
                     }else{
                         if(window.confirm("确定要将"+$scope.goods[index].name+"的价格更改为:"+result+"吗??")){
                             $scope.goods[index].price=result;
                         }
                     }
                    }else{
                        
                    }
                }
           }
        
         //全选,全不选
         $scope.selectAll=false;
         $scope.selectAllFun=function(){
             if($scope.selectAll){
                 for(index in $scope.goods){
                     $scope.goods[index].state=true;
                 }
             }else{
                 for(index in $scope.goods){
                 $scope.goods[index].state=false;    
                 }
             }
         }
        
         //反选
         $scope.checkselectAllfun=function(){
             var flag=false;
             //遍历数组,获得对象的状态
             for(index in $scope.goods){
             //alert($scope.products[index].state)
            //如果有一个对象状态是false即未选中状态,就把标志位flag变为true。    
              if(!$scope.goods[index].state){
                  flag=true;
               }
             }
             //判断是否没有一个是未选中状态
             if(flag){//这是正面至少有一个未选中
                 $scope.selectAll=false;//全选状态为false
                 
             }else{
                 $scope.selectAll=true;//全选状态为true
             }
         }
        
         //批量删除
         $scope.delselect=function(){
        
           
           //定义一个空数组,存放状态是选中的对象
           var isChected=[];
           //遍历数组,通过数组对象的状态,判断是否删除当前遍历对象
           for(index in $scope.goods){
               //如果遍历的当前对象状态为true ,则删除
               if($scope.goods[index].state){
                   //把当前选中的对象,一个个追加到isChected数组中
                   isChected.push($scope.goods[index]);
                  }
            }
              //遍历isSelected数组,因为isSelected数组中存放的是所有选中项的对象。
            for(index in isChected){
               var name=isChected[index].name;
               for(index2 in $scope.goods){
                   if(name==$scope.goods[index2].name){
                        //alert(Window.confirm("确定要全部删除吗??"))
                       
                       if(confirm("确定要全部删除吗??")){
                       
                         //$scope.goods.splice(index2,1)
                         $scope.goods=[];
                         }
                      }
               }
            }
         }
            });
        </script>
    </head>
    <body ng-app="myApp" ng-controller="myCtrl">
        <center>
        <h3>购物车</h3>
        <div>
            <input type="text"  placeholder="产品名称" ng-model="serch" style="width: 80px;"/>
            产品价格<select ng-model="produce">
                 <option>--请选择--</option>
                 <option>0-1000</option>
                 <option>1001-2000</option>
                 <option>2001-3000</option>
                 <option>3001-4000</option>
                 <option>4001-5000</option>
                 <option>5001-6000</option>
                 <option>6001-无穷大</option>
            </select>
            <select ng-model="selOrder" ng-change="orderSel()">
                <option value="">排序方式</option>
                 <option value="id">id正序</option>
                 <option value="-id">id逆序</option>
                 <option value="price">价格正序</option>
                 <option value="-price">价格逆序</option>
            </select>
            <button ng-click="delselect()">批量删除</button>
        </div>
        <table border="1" cellpadding="10" cellspacing="0">
            <thead>
                <tr>
                    <td><input type="checkbox" ng-model="selectAll" ng-click="selectAllFun()"></td>
                    <td ng-click="toog('id')">产品编号</td>
                    <td ng-click="toog('name')">产品名称</td>
                    <td ng-click="toog('price')">产品价格</td>
                    <td>操作</td>
                </tr>
            </thead>
            <tbody>
                <tr ng-repeat="user in goods|filter:serch|orderBy:(orderFlag+orderLine)" ng-if="isifshow(user.price)">
<!--                <tr ng-repeat="user in goods|filter:serch|orderBy:title:desc" ng-if="isifshow(user.price)">
-->                  
                    <td><input type="checkbox" ng-model="user.state" ng-click="checkselectAllfun()"></td>
                    <td>{{user.id}}</td>
                    <td>{{user.name}}</td>
                    <td>{{user.price}}</td>
                    <td>
                        <button ng-click="delte(user.name)">删除</button>
                        <button ng-click="update(user.price)">修改</button>
                    </td>
                </tr>
            </tbody>
        </table>
        </center>
    </body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
点击表头即可实现排序的代码./*table排序步骤 1、给排序的表格加id 2、标题栏加<thead> 3、排序的内容加<tbody> 5、引入此js 6、在文档的末尾加入 [removed] var to = new TableOrder("idTable"), odID = to.creat({ type: "int", desc: false }), arrOrder = []; $$A.forEach([ ["idNum", { type: "int" }],//排序对象(对应列的id) ["idTitle", { index: 1 }], ["idExt", { index: 1, property: "_ext" }], ["idAddtime", { index: 2, type: "date" }], ["idSize", { index: 3, property: "_order", type: "int" }], ["idCheckbox", { index: 4, type: "bool", value: SetCheck }], ["idRadio", { index: 5, type: "bool", value: SetCheck }] ], function (arr){ var o = $$(arr[0]), order = to.creat(arr[1]); order.onBegin = function(){ ClearCss(); odID.desc = this.desc; } order.onEnd = function(){ o.className = this.desc ? "desc" : "asc";//设置样式 this.desc = !this.desc;//取反排序 } o.onclick = function(){ to.sort(order, odID); } arrOrder.push(o);//记录排序项目(这里主要用来设置样式) }); [removed] 7、【排序对象】 属性 默认值//说明 index: 0,//td索引 property: "innerHTML",//获取数据的属性 type: "string",//比较的数据类型 desc: true,//是否按降序 compare: null,//自定义排序函数 value: null,//自定义取值函数 repair: this._repair,//是否解决checkbox和radio状态恢复bug onBegin: function(){},//排序前执行 onEnd: function(){}//排序后执行 */ //完整的示例 //<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> //<html> //<head> //<meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> //<title>Table 排序</title> //[removed][removed] //</head> //<body> //<style type="text/css"> //.odTable {width:500px;border:1px solid #ebebeb;line-height:20px;font-size:12px;background:#FFF;} //.odTable thead {background-color:#ebebeb;} //.odTable span {color:#333;padding-right:15px;cursor:pointer;} //.odTable .desc, .odTable .asc {background:url(http://images.cnblogs.com/cnblogs_com/cloudgamer/169629/r_2.jpg) right center no-repeat;} //.odTable .desc {background-image:url(http://images.cnblogs.com/cnblogs_com/cloudgamer/169629/r_1.jpg);} //</style> //<table border="0" cellspacing="0" cellpadding="5" class="odTable" id="idTable"> // <thead> // <tr> // <td align="center"><span id="idNum">ID</span></td> // <td> <span id="idTitle">名称</span> / <span id="idExt">类型</span></td> // <td width="100" align="center"><span id="idAddtime">上传时间</span></td> // <td width="50" align="center"><span id="idSize">大小</span></td> // <td width="30" align="center"><span id="idCheckbox">C</span></td> // <td width="30" align="center"><span id="idRadio">R</span></td> // </tr> // </thead> // <tbody> // <tr> // <td align="center">1</td> // <td _ext="htm">new.htm</td> // <td align="center">2008/9/12</td> // <td align="right" _order="433244">423.09 K</td> // <td align="center"><input type="checkbox"/></td> // <td align="center"><input name="c" type="radio"/></td> // </tr> // <tr> // <td align="center">2</td> // <td _ext="js">Scroller.js</td> // <td align="center">2008/9/23</td> // <td align="right" _order="2560">2.5 K</td> // <td align="center"><input type="checkbox"/></td> // <td align="center"><input name="c" type="radio"/></td> // </tr> // <tr> // <td align="center">3</td> // <td _ext="js">AlertBox.js</td> // <td align="center">2008/9/23</td> // <td align="right" _order="3563">3.48 K</td> // <td align="center"><input type="checkbox"/></td> // <td align="center"><input name="c" type="radio"/></td> // </tr> // <tr> // <td align="center">4</td> // <td _ext="xml">1.xml</td> // <td align="center">2008/10/4</td> // <td align="right" _order="11397">11.13 K</td> // <td align="center"><input type="checkbox"/></td> // <td align="center"><input name="c" type="radio"/></td> // </tr> // <tr> // <td align="center">5</td> // <td _ext="xml">4.xml</td> // <td align="center">2008/10/4</td> // <td align="right" _order="108">108 b</td> // <td align="center"><input type="checkbox"/></td> // <td align="center"><input name="c" type="radio"/></td> // </tr> // <tr> // <td align="center">6</td> // <td _ext="htm">news.htm</td> // <td align="center">2008/10/4</td> // <td align="right" _order="14074">13.74 K</td> // <td align="center"><input type="checkbox"/></td> // <td align="center"><input name="c" type="radio"/></td> // </tr> // <tr> // <td align="center">7</td> // <td _ext="js">function.js</td> // <td align="center">2008/10/4</td> // <td align="right" _order="2844">2.78 K</td> // <td align="center"><input type="checkbox"/></td> // <td align="center"><input name="c" type="radio"/></td> // </tr> // <tr> // <td align="center">8</td> // <td _ext="mp3">神秘园 - Nocturne.mp3</td> // <td align="center">2008/9/20</td> // <td align="right" _order="3111293">2.97 M</td> // <td align="center"><input type="checkbox"/></td> // <td align="center"><input name="c" type="radio"/></td> // </tr> // <tr> // <td align="center">9</td> // <td _ext="doc">详细功略+剧情流程(一).doc</td> // <td align="center">2009/2/2</td> // <td align="right" _order="63488">62 K</td> // <td align="center"><input type="checkbox"/></td> // <td align="center"><input name="c" type="radio"/></td> // </tr> // <tr> // <td align="center">10</td> // <td _ext="doc">详细功略+剧情流程(二).doc</td> // <td align="center">2009/2/2</td> // <td align="right" _order="164352">160.5 K</td> // <td align="center"><input type="checkbox"/></td> // <td align="center"><input name="c" type="radio"/></td> // </tr> // </tbody> //</table> //<br /> //有中文的先排前面,再按时间倒序,ID倒序排序: //<input name="" type="button" value="Sort" id="idBtn" /> //[removed] //var to = new TableOrder("idTable"), odID = to.creat({ type: "int", desc: false }), arrOrder = []; //$$A.forEach([ // ["idNum", { type: "int" }], // ["idTitle", { index: 1 }], // ["idExt", { index: 1, property: "_ext" }], // ["idAddtime", { index: 2, type: "date" }], // ["idSize", { index: 3, property: "_order", type: "int" }], // ["idCheckbox", { index: 4, type: "bool", value: SetCheck }], // ["idRadio", { index: 5, type: "bool", value: SetCheck }] //], function (arr){ // var o = $$(arr[0]), order = to.creat(arr[1]); // order.onBegin = function(){ ClearCss(); odID.desc = this.desc; } // order.onEnd = function(){ // o.className = this.desc ? "desc" : "asc";//设置样式 // this.desc = !this.desc;//取反排序 // } // o.onclick = function(){ to.sort(order, odID); } // arrOrder.push(o);//记录排序项目(这里主要用来设置样式) //}); //$$("idNum").onclick(); //////////////////////////////////////////////////////////////////////// //var od1 = to.creat({ index: 1, onEnd: ClearCss, // compare: function(value1, value2) { // var re = /[\u4E00-\u9FA5]/, v1 = re.test(value1), v2 = re.test(value2); // return v1 == v2 ? 0 : (v1 ? 1 : -1); // } // }) // ,od2 = to.creat({ index: 2, type: "date" }) ,od3 = to.creat({ type: "int" }); //$$("idBtn").onclick = function(){ to.sort(od1, od2, od3); } //[removed] //</body> //</html>

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值