简体   繁体   English

Angular JS全选/取消全选复选框如何检索所选信息

[英]Angular JS select all/De select all checkboxes how to retrieve selected information

I am displaying output response(JSON) from REST API using Angular JS. 我正在使用Angular JS显示REST API的输出响应(JSON)。 I am planning to provide user with an option of checkbox for every result listed and also a selectall/deselectall checkbox .I am stuck at implementing select all/deselect all implementation , how to implement selectall/deselect all checkbox and also how to retrieve the selected checkbox information and form a JSON object of all the Id selected .I would love to do it the angular way 我打算为用户列出的每个结果提供一个复选框选项,以及一个selectall / deselectall复选框。我被困在实现select all / deselect all实施,如何实现selectall / deselect all复选框以及如何检索选中的复选框上。复选框信息并形成所有选定ID的JSON对象。我很乐意以有角度的方式进行操作

This is my Controller call 这是我的控制器电话

   $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."
     }
   });

This is my JSON Response from REST 这是我从REST获得的JSON响应

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

This is my HTML 这是我的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>

I would like to implement select all / deselect option and retrieve the customer ids selected and form a JSON object 我想实现全选/取消选择选项,并检索所选的客户ID,并形成一个JSON对象

Add a selected field on each of the search response items: 在每个搜索响应项上添加一个选定字段:

$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."
    }
});

Template: 模板:

<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>

then to select all/ de-select all, you can write: 然后选择全部/取消全部选择,您可以编写:

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

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

And to get the values that were selected: 并获取所选的值:

function getSelected() {
    return $scope.searchresponse.filter(function (x) {
        return x.selected;
    });
}

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM