angular 使用小技巧 增删改 全选

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <script type="text/javascript" src="js/angular-route.js" ></script>
        <script type="text/javascript" src="js/angular.js" ></script>
        <script type="text/javascript" src="js/jquery-3.2.1.min.js" ></script>
        <script>
            var app=angular.module("myApp",[])
            app.controller("myCtrl",function($scope,$http){
                $scope.jsonnet="";
                
                $http({
                    method:"get",
                    url:"http://result.eolinker.com/pWEDzFn48fd146c3a725c1571f997a44281b94d1201b661?uri=myjson1"
                }).then(function success(response){
                    $scope.jsonnet=response.data;
                },function error(response){})
                $scope.ifshow=false;
                 $scope.showtable=function(){
                     //添加信息的框显示
                     $scope.ifshow=!$scope.ifshow;
                 }
                 //定影两个变量
                 $scope.mytitle="";
                 $scope.mysummary="";
                 //修改时的数组下标
                 $scope.alertindex="";
                
                 //判断按钮是添加还是修改
                 $scope.myadd=function(){
                     
                     if($("#btn").val()=="添加"){
                          var stu={
                         detectionItem:$scope.mytitle,
                         detectionInfo:$scope.mysummary
                     }
                     $scope.jsonnet.data.push(stu);
                     $scope.ifshow=false;
                     }else{
                         //不是添加就把输入框的值赋值给相同index的数据
                         $scope.jsonnet.data[$scope.alertindex].detectionItem=$scope.mytitle;
                         $scope.jsonnet.data[$scope.alertindex].detectionInfo=$scope.mysummary;
                         $scope.ifshow=false;
                     }
                     
                     
                     
                 }
                 //删除
                 $scope.deleteG=function($index){
                     
                     $scope.jsonnet.data.splice($index,1)
                 }
                
                 //修改的方法
                 $scope.alertG=function($index){
                     //把框显示出来
                     $scope.ifshow=true;
                     //获取到修改的下标值
                      $scope.alertindex=$index;
                      //把添加的字更改为修改
                      $("#btn").val("修改")
                      //获取到修改的值把值给到输入框
                     $scope.mytitle=$scope.jsonnet.data[$index].detectionItem;
                     $scope.mysummary=$scope.jsonnet.data[$index].detectionInfo;
                     
                 }
                 //全选
                     $scope.btn2=function(){
                    
                               var v = $("input[type='checkbox']");
                    //alert(v.length);
                                   for(var i=0 ; i<v.length ; i++){
                                          if(v[i].checked == true){
                                                 for(var j = 0; j < v.length; j++) {
                                                      v[i].checked = !v[j].checked;
                                                        }
                                                        }else {
                                                            for(var z = 0; z < v.length; z++) {
                                                            v[i].checked = true;
                                                        }
                                                            }
                                                 }
                    
                    };
                
                
            })
            
            
            
        </script>
        <style>
            tr{
                text-align: center;
            }
        </style>
    </head>
    <body ng-app="myApp" ng-controller="myCtrl">
        <center>
            <!--显示添加信息的框-->
            <input type="button" value="显示添加信息框" ng-click="showtable()" />
            <input type="button" value="全选/反选" ng-click="btn2()" />
            <!--显示数据的列表-->
             <table border="1" width="1000px">
             <tr>
                 <th><input type="checkbox" ng-model="aa"/></th><th>序号</th><th>title</th><th>summary</th><th>操作</th>
             </tr>
             <tr ng-repeat="g in jsonnet.data">
                 <td><input type="checkbox" ng-model="aa" /></td>
                 <td>{{$index}}</td>
                 <td>{{g.detectionItem}}</td>
                 <td>{{g.detectionInfo}}</td>
                 <td>
                     <input type="button" value="删除" ng-click="deleteG($index);"/>
                     <input type="button" value="修改" ng-click="alertG($index)" />
                         
                 </td>
             </tr>
         </table>
         <!--分割线-->
         <hr width="100%"  />
         <!--添加信息的输入框-->
         <table border="1" width="300px" ng-show="ifshow">
             <tr>
                 <td>title</td>
                 <td><input type="text" ng-model="mytitle"/></td>
             </tr>
             <tr>
                 <td>summary</td>
                 <td><input type="text" ng-model="mysummary"/></td>
             </tr>
             <tr>
                 <td colspan="2">
                     <input type="button" value="添加" id="btn"  ng-click="myadd()"/>
                 </td>
                 
             </tr>
         </table>
        </center>
        
    </body>
</html>





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Jonly_W

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值