angularjs 创建“全部显示”按钮以显示隐藏行

m528fe3b  于 2023-01-12  发布在  Angular
关注(0)|答案(1)|浏览(160)

我的代码是这样的:当你选中"隐藏"列时,整行都被隐藏。我还有一个"全部显示"复选框。当我选中它时,所有的行(包括隐藏的行)都被显示。但是当我取消选中它时,所有的行都消失了。我怎样才能使它在我取消选中"全部显示"时只有隐藏的行消失,而其他的行仍然可见呢?

<body ng-app="myApp" ng-controller="myController">
    <h1>Fruits in Vietnam</h1>
    <hr>
    <form name="myForm">
        <div class="input-group input-group-lg">
            <input type="text" class="form-control" name="mdfruitname" id="txtfruitname" ng-model="mdfruitname"
                required>
            <div class="input-group-append">
                <button class="btn btn-outline-secondary" type="button" ng-click="addnew()"
                    ng-disabled="myForm.mdfruitname.$pristine || myForm.mdfruitname.$invalid">Add
                </button>
            </div>
        </div>
    </form>

    </div>
    <table class="table table-striped">
        <thead>
            <tr>
                <th>Description</th>
                <th>Hide</th>
                <th>Delete</th>
            </tr>
        </thead>
        <tbody>
            <tr ng-repeat="fruit in fruitnameDetail" ng-show="showall" ng-hide="hidef">
                <td>{{fruit.fruitname}}</td>
                <td><input type="checkbox" ng-model="hidef"></td>
                <td>
                    <a class="btn btn-xs delete-record" ng-click="removefruitname($index)">
                        <i class="glyphicon glyphicon-trash"></i>
                    </a>
                </td>

            </tr>
        </tbody>
    </table>
    <div><input type="checkbox" ng-model="showall"><span>SHOW ALL</span></div>
</body>
<script>
    var app = angular.module("myApp", []);

    app.controller('myController', function ($scope, $window) {
        $scope.fruitnameDetail = [
            {
                "fruitname": "Banana"
            },
            {
                "fruitname": "Mango"
            },
            {
                "fruitname": "Orange"
            }
        ];
        $scope.addnew = function () {
            var fruitnameDetail = {
                fruitname: $scope.mdfruitname
            };
            $scope.fruitnameDetail.push(fruitnameDetail);
            $scope.mdfruitname = '';
        };
        $scope.removefruitname = function (index) {
            var name = $scope.fruitnameDetail[index].fruitname;
            if ($window.confirm("Do you want to delete " + name + " ?")) {
                $scope.fruitnameDetail.splice(index, 1);
            }
        };
    });
</script>
ctehm74n

ctehm74n1#

不要同时使用ng-show和ng-hide。只需要稍微修改一下ng-repeat的条件。下面是应该可以工作的例子:

<body ng-app="myApp" ng-controller="myController">
    <h1>Fruits in Vietnam</h1>
    <hr>
    <form name="myForm">
        <div class="input-group input-group-lg">
            <input type="text" class="form-control" name="mdfruitname" id="txtfruitname" ng-model="mdfruitname"
                required>
            <div class="input-group-append">
                <button class="btn btn-outline-secondary" type="button" ng-click="addnew()"
                    ng-disabled="myForm.mdfruitname.$pristine || myForm.mdfruitname.$invalid">Add
                </button>
            </div>
        </div>
    </form>

    </div>
    <table class="table table-striped">
        <thead>
            <tr>
                <th>Description</th>
                <th>Hide</th>
                <th>Delete</th>
            </tr>
        </thead>
        <tbody>
            <tr ng-repeat="fruit in fruitnameDetail" ng-show="showall || !hidef">
                <td>{{fruit.fruitname}}</td>
                <td><input type="checkbox" ng-model="hidef"></td>
                <td>
                    <a class="btn btn-xs delete-record" ng-click="removefruitname($index)">
                        <i class="glyphicon glyphicon-trash"></i>
                    </a>
                </td>

            </tr>
        </tbody>
    </table>
    <div><input type="checkbox" ng-model="showall"><span>SHOW ALL</span></div>
</body>

相关问题