【问题标题】:angularjs. Check if at least one checkbox is checked in a table角js。检查表格中是否至少有一个复选框被选中
【发布时间】:2015-05-12 01:55:29
【问题描述】:

我有以下 html:

<tbody ng-repeat="notification in notifications">
    <tr>
        <td rowspan="{{notification.specs.length+1}}">{{notification.notification_id}}</td>
    </tr>
    <tr ng-repeat="position in notification.specs">
        <td>{{position.lot}}</td>
        <td>{{position.id}}</td>
        <td>{{position.kind}}</td>
        <td>{{position.name}}</td>
        <td>{{position.release_form}}</td>
        <td>{{position.mnn}}</td>
        <td>{{position.tn}}</td>
        <td>{{position.metric}}</td>
        <td>{{position.price}}</td>
        <td>{{position.count}}</td>
        <td>{{position.full_character}}</td>
        <td>{{position.package_count}}</td>
        <td>{{position.who_checked}}</td>
        <td><input type="checkbox" ng-init="position.is_checked = false" ng-model="position.is_checked"></td>
        <td style="text-align:center" ><i class="icon-folder-open"></i></td>
    </tr>
</tbody>

我需要检查表格中是否至少有一个复选框被选中。如果它被选中,我必须显示一个特殊的块。如果没有选中任何复选框,我必须隐藏该块。

更新

当我从服务器获取数据时,模型中没有 is_checked 这样的字段。 有什么方法可以手动将字段添加到模型中?

【问题讨论】:

    标签: javascript angularjs


    【解决方案1】:

    公开一个 isChecked 函数,然后在您的视图中绑定到它。

    控制器:

     $scope.isChecked = function(notifications) {
            for (var i = 0; i < notifications.length; ++i) {
            {
                var specs = notifications[i].specs;
                for (var j = 0; j < specs.length; ++j) {
                    if (specs[j].is_checked)
                        return true;
                }
            }
            return false;
     };
    

    HTML:

    <div ng-show="isChecked()"> Special </div>
    

    【讨论】:

    • positionsng-repeat 循环内。我必须在循环外的这个函数中使用一个参数。
    • 当我从服务器获取数据时,我的模型中没有 is_checked 这样的字段。有什么方法可以手动将字段添加到模型中?
    • 我不喜欢从局部调用函数,因为每次 $digest 正在进行时都会调用它们
    • 这个答案确实有效。我不认为这是最好的实现,但它仍然有效 =) 谢谢@pixelbits
    • 是的,此解决方案与在函数上设置 $watch 相同。但它比深度 $watch 要好。
    【解决方案2】:

    这应该可行:

    MyApp.controller('MyCtrl', ['$scope', 'filterFilter',
        function ($scope, filterFilter) {
            $scope.isChecked = function(positions) {
                var checked = filterFilter(positions, {is_checked: true});
                return !!checked.length;
            };
        }
    ]);
    

    那么您可以在模板中使用isChecked 函数。

    【讨论】:

    • $scope 没有属性 position。它有一个属性notification,它有一个positions 的数组。无论如何,如果你过滤notification.positions,然后检查它的长度,就可以了。
    • 我更新了我的答案。附言尽管提问者可能想直接在模板中这样做,但最好显示纯代码和想法......
    • 其实我以为我必须在 coroller 中创建一个返回布尔值的方法。你写的这件事看起来是真的!我会试着理解这段代码。
    【解决方案3】:

    未经测试,但应该可以工作

    <tbody ng-repeat="notification in notifications">
        <tr>
            <td rowspan="{{notification.specs.length+1}}">{{notification.notification_id}}</td>
        </tr>
        <tr ng-repeat="position in notification.specs">
            {{provisorCtrl.specs = notification.specs}}
            <td>{{position.lot}}</td>
            <td>{{position.id}}</td>
            <td>{{position.kind}}</td>
            <td>{{position.name}}</td>
            <td>{{position.release_form}}</td>
            <td>{{position.mnn}}</td>
            <td>{{position.tn}}</td>
            <td>{{position.metric}}</td>
            <td>{{position.price}}</td>
            <td>{{position.count}}</td>
            <td>{{position.full_character}}</td>
            <td>{{position.package_count}}</td>
            <td>{{position.who_checked}}</td>
            <td><input type="checkbox" ng-init="position.is_checked = false" ng-model="position.is_checked"></td>
            <td style="text-align:center" ><i class="icon-folder-open"></i></td>
        </tr>
    </tbody>
    <!--added-->
    <div id="specialBlock" ng-if="notifications | filter:{'is_checked':true}:true">show if any checkbox</div>
    

    其他,也许更好的方法是在复选框上添加 ng-change

    <td><input type="checkbox" ng-change="boxIsSelected = position.is_checked" ng-model="position.is_checked"></td>
    

    那么

    <!--added-->
    <div id="specialBlock" ng-if="boxIsSelected">show if any checkbox</div>
    

    【讨论】:

    • 好答案。我还会稍微更改一下 HTML 代码:不要在 &lt;tbody&gt; 上使用 ng-repeat,而是在第一个 &lt;tr&gt; 和第二个 &lt;tr&gt; 上使用 ng-repeat-startng-repeat-end。此外,ng-initfalse 的值可能根本不需要。
    • 嗯...notifications 孩子有字段is_checked?我相信,这行不通。
    • 是的,应该是notification.specs
    • 但问题是没有notification.specs - 有notifications[n].specs。至少在答案的模板中。
    • 如果我理解您的意思,则没有字段 is_checked(这只是一个客户字段)。结构如下notifications[n].specs[n]
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-11
    相关资源
    最近更新 更多