【问题标题】:Angular JS select all/De select all checkboxes how to retrieve selected informationAngular JS全选/De全选复选框如何检索所选信息
【发布时间】:2017-04-26 18:16:29
【问题描述】:

我正在使用 Angular JS 显示来自 REST API 的输出响应 (JSON)。我计划为用户提供列出的每个结果的复选框选项以及全选/取消全选复选框。我坚持实施全选/取消全选实现,如何实现全选/取消全选复选框以及如何检索选定的复选框信息并形成所有选定 Id 的 JSON 对象。我很乐意以角度方式进行

这是我的控制器调用

   $http.post('http://localhost:8080/services/search/'+id+"", searchCriteria).then(function(response) {
     $scope.searchresponse = response.data.items;
     if($scope.searchresponse.length != 0){
       console.log($scope.searchresponse);
     }
     else {
       $scope.showerror = true;
       $scope.ErrorMsg ="There is no record matching your criteria."
     }
   });

这是我来自 REST 的 JSON 响应

{
    "items": [{
        "customerId": "ABC",
        "type": "D"
    }, {
        "customerId": "DEF",
        "type": "A"
    }],
    "more": false
} 

这是我的 HTML

    <tr ng-repeat="details in successresponse">
        <td align="left" class="validationMsg">
            <input type="checkbox" name="entireday" id="entireday">
            {{details.customerId}}
            {{details.type}}
        </td>
    </tr>

我想实现全选/取消选择选项并检索选择的客户 ID 并形成一个 JSON 对象

【问题讨论】:

    标签: javascript angularjs json checkbox


    【解决方案1】:

    在每个搜索响应项上添加一个选定的字段:

    $http.post('http://localhost:8080/services/search/'+id+"", searchCriteria).then(function(response) {
    
        if (response.data && response.data.items.length != 0) {
            $scope.searchresponse = response.data.items.map(function (x) {
                x.selected = false;
                return x;
            });
            console.log($scope.searchresponse);
        } 
        else {
            $scope.showerror = true;
            $scope.ErrorMsg ="There is no record matching your criteria."
        }
    });
    

    模板:

    <tr ng-repeat="details in successresponse">
        <td align="left" class="validationMsg">
            <input type="checkbox" name="entireday" id="entireday" 
                   ng-model="details.selected">
            {{details.customerId}}
            {{details.type}}
        </td>
    </tr>
    

    然后全选/取消全选,可以这样写:

    $scope.selectAll = function() {
        angular.forEach($scope.searchresponse, function (x) {
            x.selected = true;
        });
    }
    
    $scope.deselectAll = function() {
        angular.forEach($scope.searchresponse, function (x) {
            x.selected = false;
        });
    };
    

    并获取选择的值:

    function getSelected() {
        return $scope.searchresponse.filter(function (x) {
            return x.selected;
        });
    }
    
    // alternative implementation
    var getSelected = [].filter.bind(
        $scope.searchresponse, 
        function (x) { return x.selected; });
    

    【讨论】:

    • 为什么我们需要在控制器内部设置复选框。我应该如何调用 getselected 函数来检索控制器内的值并形成一个仅包含 customerid 的 JSON 对象
    • 您需要初始化控制器中的复选框,因为这是您从 REST 端点获取初始值集的地方。您可以调用 getSelected 从列表中获取在您需要时选择的项目。例如,当调用另一个 REST 端点时,您只需执行 data = getSelected().map(function (x) { return x.customer_id; }); 即可获取客户 ID。此外,您的 OP 中的代码非常少。如果您想要更多详细信息,则必须提供更多详细信息。
    • 当我尝试检索值时出现此错误 - Array.prototype.filter 在 null 或 undefined 上调用
    • 这意味着你没有在你的控制器中初始化$scope.searchresponse
    • @hey 2ps,我如何返回客户类型和 id。我想为我的 rest call 构造 JSON obj。这样我就可以获取客户 ID 的详细信息并键入并循环遍历它并进行一些验证
    猜你喜欢
    • 2017-03-18
    • 1970-01-01
    • 2015-03-21
    • 1970-01-01
    • 2019-01-17
    • 2016-02-20
    • 2014-07-14
    • 2016-10-01
    • 1970-01-01
    相关资源
    最近更新 更多