加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angularJS处理table中checkbox的选中状态

发布时间:2020-12-17 17:30:29 所属栏目:安全 来源:网络整理
导读:贴上效果图: ? 贴上源码: ? ! DOCTYPE html html head ??? ? meta charset ="UTF-8" title / title link rel ="stylesheet" href ="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css" script src ="https://cdn.staticfile.org/

贴上效果图:

?

贴上源码:

?

<!DOCTYPE html>
<html>

<head>??? ?
    <meta charset="UTF-8">
    <title></title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
    <script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <script src="https://cdn.staticfile.org/angular.js/1.4.6/angular.min.js"></script>

<body>
    ?? ??
    ?<div ng-app=‘myApp‘ ng-controller=‘myCtrl‘>
        <div class="container">
            <div class="row">
                <div class=‘col-md-10‘>
                    <table class="table table-bordered">
                        <thead>
                            <tr>
                                <th>
                                    <input type="checkbox" ng-model=‘masterChecked‘ ng-click=‘checkMaster()‘ />
                                </th>
                                <th>id</th>
                                <td>name</td>
                                <td>age</td>
                            </tr>
                        </thead>
                        <tbody>
                            <tr ng-repeat="row in personList">
                                <td>
                                    <input type="checkbox" ng-checked=‘row.checked‘ ng-click=‘checkChild(row)‘ />
                                </td>
                                <td>{{row.id}}</td>
                                <td>{{row.name}}</td>
                                <td>{{row.age}}</td>
                            </tr>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>

    ?? ??? ?
    <script>
        var myApp = angular.module(myApp,[]);
        myApp.controller(myCtrl,function ($scope) {
            //初始化表格数据
            $scope.personList = [{
                checked: false,id: 1,name: 赵云,age: 50
            },{
                checked: false,id: 2,name: 曹操,age: 60
            },id: 3,name: 大司马,age: 50
            }];
            //主复选框选中事件
            $scope.checkMaster = function () {
                if ($scope.masterChecked == true) {
                    angular.forEach($scope.personList,function (row,index) {
                        row.checked = true;
                    });
                } else {
                    angular.forEach($scope.personList,index) {
                        row.checked = false;
                    });
                }
            }
            //子复选框选中事件
            $scope.checkChild = function (row) {
                //根据选中状态,调正row.checked值
                (row.checked == false) ? row.checked = true : row.checked = false;
                //若取消选中,则取消主复选框选中
                if (row.checked == false) {
                    $scope.masterChecked = false;
                } else {
                    //若选中,判断主复选框是否需要选中
                    var temp = true;
                    angular.forEach($scope.personList,index) {
                        //若子复选框有一个未选中,则主复选框不必选中
                        if (row.checked == false) {
                            temp = false;
                        }
                    });
                    //若子复选框全部选中,则选中主复选框
                    $scope.masterChecked = temp;
                }
            }
        });

    </script>
</body>

</html>

?

备注:没有注释,抱歉今天还是一个不擅长做一个注释的小可爱.......

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读