zoukankan      html  css  js  c++  java
  • ng-checked选择和点击增加dom

     

    1.需求

    在添加页面实现一个checkbox的选择,然后在详情页面展示时,会自动选上之前被选中的。

    2.添加页面

    看官最好将这个代码复制过去看看效果。

    
    
    
    <!DOCTYPE html><html><head><meta charset="utf-8"><script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script></head><body><div ng-app="myApp" ng-controller="myCtrl" >
        选择
        <div ng-repeat="item in list">
            <input type="checkbox" name="tagName" value="item.id" ng-click="select(item.id,$event)"> {{item.shortName}}
        </div>
        结果:{{result}}</div><script>    
        var app = angular.module('myApp', []);
        app.controller('myCtrl', function($scope) {
            //创建checkbox用的
            $scope.list=[{"id":1,"shortName":"张三"},{"id":2,"shortName":"李四"},{"id":3,"shortName":"王二"}];
            //存储已选
            $scope.result = [];
            //触发事件
            $scope.select = function(id,event){            
                console.log(event)//打印看看这是什么,有利于理解
                console.log(action)
    
                var action = event.target;
                if(action.checked){//选中,就添加
                    if($scope.result.indexOf(id) == -1){//不存在就添加
                        $scope.result.push(id);
                    }
                }else{//去除就删除result里
                    var idx = $scope.result.indexOf(id);
                    if( idx != -1){//不存在就添加
                        $scope.result.splice(idx,1);
                    }
                }
            };
        });
    </script></body></html>

    3.详情展示

    //假设添加页面的结果是:$scope.result = [3,2];

    <!DOCTYPE html><html><head><meta charset="utf-8"><script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script></head><body>
    
    <div ng-app="myApp" ng-controller="myCtrl" >
        回写时设置不可选,即设ng-disabled="true"
        <div ng-repeat="item in list">
            <input type="checkbox" name="tagName"  ng-checked="isSelected(item.id)" value="item.id" ng-disabled="true" > {{item.shortName}}
        </div>
        结果:{{result}}</div>
    
    <script>    
        var app = angular.module('myApp', []);
        app.controller('myCtrl', function($scope) {
            //创建checkbox用的
            $scope.list=[{"id":1,"shortName":"张三"},{"id":2,"shortName":"李四"},{"id":3,"shortName":"王二"}];
    
            //在添加页面得到的结果
            //你会发现,顺序也不会影响结果
            $scope.result = [3,2];
    
            //被选中条件:ng-checked的结果为true
            $scope.isSelected = function(id){          
                return $scope.result.indexOf(id)!=-1; 
                //只要返回的结果为true,则对应的checkbox就会被选中,
                //所以我的思路是看存添加页面的结果里是否含有当前id即value值,
                //有就返回的true,没有就返回false
            };
        });
    </script></body></html>
     
     
     
     
    {"list":[{"content":"普通发票","type":1},{"content":"电子产品","type":2}]}  要传给后台的数据   页面展示  上面传type=1,下面增加的填type=2
     
     
    html
     
                  

            div.form-group label.control-label.col-md-3 发票类型 div.col-md-6 label.checkbox.pull-left(ng-repeat="(key,type) in globalConfig.invoice_type" style="margin-left:30px") input(type="checkbox" name="invoice_type" ng-model="invoice_type[key]" ng-true-value="true" ng-false-value="" ng-click="") | {{type}} //- select.form-control(ng-options="key as value for (key,value) in globalConfig.invoice_type" ng-model="info.list.invoice_type" multiple="true") //- option(value='') (发票类型可多选) div.form-group label.control-label.col-md-3 发票内容 div.col-md-6 .form-group span.btn.btn-danger(ng-click="addFn(sendData.list)") 添加 div(ng-repeat="spec in sendData.list") .input-group input.form-control(type="text" ng-model="spec.content" name="brand_name" ) .input-group-addon(ng-click="removeFn( sendData.content,spec)") i.glyphicon.glyphicon-remove

     
     
    js
     
    angular.module('laoyou')
    .controller('invoiceCtrl', ['$scope','$http','$stateParams','api',function($scope, $http, $stateParams,api){       
     
     $scope.sendData = {list:[]};
            $scope.addFn=function(list,item){
                // console.log(list)
                // [{type: 2, content: "ert"}, {type: 2, content: "fgh"}]
                list.push({type:2});
                /*if(list == undefined){
                    list = [];               
                }else{
                    list.push({});
                }*/
     
            }
            $scope.removeFn=function(list,item){
                var index=list.indexOf(item);
                list.splice(index,1)
            }
     
    //      初始化invoice_type
            $scope.invoice_type={};
     
            $scope.sendFn = function () {
     
                sendData=angular.copy($scope.sendData);
     
                // sendData= {};
                // sendData.content = $scope.sendData.content;
     
                var type= [{"content":"普通发票","type":1},{"content":"增值税专用发票","type":1},{"content":"电子发票","type":1}];
     
                // sendData.type=[];
                var invoice=$scope.invoice_type;
                console.log(invoice)
                for(var x in invoice){
                    if(invoice[x]){
                        sendData.list.push(type[x-1]); //传入想要的数
                    }
                }
     
                 console.log(sendData);
                 /*var info = {};
                 info.list = [sendData];*/
                // console.log(info);
                $http.post("invoice/save", sendData).success(function (data) {
                    if (data.statusCode === '0') {
                        alert("提交成功");
                        /*if ($scope.pageModel === 'add') {
                            $state.go("home.employee.view", {
                                id: data.data
                            })
                        }else{
                            $state.go("home.employee.view", {
                                id: $stateParams.id
                            })
                        }*/
                    }
                })
            }   
    }])
  • 相关阅读:
    linux file命令小记
    利用actionscript访问wfs服务
    在C/C++中static有什么用途?(请至少说明两种)
    单元测试、集成测试、系统测试的侧重点是什么?
    测试计划工作的目的是什么?测试计划文档的内容应该包括什么?其中哪些是最重要的?
    简述什么是静态测试、动态测试、黑盒测试、白盒测试、α测试&#160;β测试
    一台客户端有三百个客户与三百个客户端有三百个客户对服务器施压,有什么区别?
    BUG管理工具的跟踪过程(用BugZilla为例子)
    软件测试分为几个阶段&#160;各阶段的测试策略和要求是什么?
    软件质量保证体系是什么&#160;国家标准中与质量保证管理相关的几个标准是什么?他们的编号和全称是什么?
  • 原文地址:https://www.cnblogs.com/s-quan/p/6024076.html
Copyright © 2011-2022 走看看