Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Checks dont get unchecked on nested controllers #148

Open
dsprogramacion opened this issue Aug 12, 2016 · 3 comments
Open

Checks dont get unchecked on nested controllers #148

dsprogramacion opened this issue Aug 12, 2016 · 3 comments

Comments

@dsprogramacion
Copy link

Hi
This is my template:

<body ng-app="App" ng-controller="MainController" layout="column" flex>
    <section ui-view layout="row" flex>
            <md-sidenav>
               ...
            </md-sidenav>
            <md-content layout="column" flex>
            <section ui-view layout="row" flex>
                       ...
                       <md-checkbox ng-model="$parent.$parent.checkboxAll" ng-change="toggleCheck()">             
                       </md-checkbox>
                       ...
                       <tr ng-repeat="o in pagedItems[currentPage - 1]">
                            <td md-cell ng-click="$event.stopPropagation()">
                                  <md-checkbox checklist-model="$parent.$parent.checkedItems" 
                                  checklist-value="o" ></md-checkbox>
                            </td>
                            ...
                      </tr>
                </section>
            </md-content>
        </section>
</body>

And those are is my controller:
`.controller('MainController', ['$scope', '$filter', '$state', '$timeout', 'AUTH_EVENTS', 'ITEMS_EVENTS', function($scope, $filter, $state, $timeout, AUTH_EVENTS, ITEMS_EVENTS) {

$scope.currentPage;
$scope.itemsPerPage = {prev: 15, current: 15};
$scope.quantities = [15, 30, 50, 100];

$scope.initialSort;
$scope.itemsSortBy;

$scope.items;
$scope.itemsCount;
$scope.pagedItems;

$scope.searchInput;

$scope.checkboxKeys;
$scope.checkboxAll;
$scope.checkedItems;

//Pagination, Checkbox and Search Functions
$scope.toggleCheck = function() {

    $scope.checkedItems = [];

    if($scope.checkboxAll) {

        var allItems = angular.copy($scope.pagedItems[$scope.currentPage-1]);
        for(var i in allItems) {

            obj = {};
            for(var k in $scope.checkboxKeys) {

                obj[k] = allItems[i][$scope.checkboxKeys[k]];
            }

            $scope.checkedItems.push(obj);
        }
    }
};

var searchMatch = function(haystack, needle) {

    return haystack.toLowerCase().indexOf(needle.toLowerCase()) !== -1;
};


$scope.search = function(sort, initialSort, items) {

    $scope.checkboxAll = false;
    $scope.checkedItems = [];

    $scope.currentPage = 1;

    if(initialSort) {

        $scope.items = items;
        $scope.itemsSortBy = initialSort;
        $scope.initialSort = initialSort;
    }

    if(sort || $scope.itemsSortBy != $scope.initialSort) {

        var sortingOrder = sort || $scope.itemsSortBy;
        $scope.filteredItems = $filter('sortTableData')($scope.items, sortingOrder);

    } else {

        if($scope.searchInput) {

            $scope.filteredItems = $filter('filter')($scope.items, function(item) {

                for(var attr in item) {

                    if(item[attr] != null) {

                        if(searchMatch(item[attr], $scope.searchInput)) {

                            return true;
                        }
                    }
                }

                return false;
            });

        } else {

            $scope.filteredItems = $scope.items;
        }
    }

    $scope.itemsCount = $scope.filteredItems.length;
    $scope.groupToPages();
};

$scope.groupToPages = function() {

    $scope.pagedItems = [];

    for(var i = 0; i < $scope.itemsCount; i++) {

        if(i % $scope.itemsPerPage.current === 0) {

            var index = Math.floor(i / $scope.itemsPerPage.current);
            $scope.pagedItems[index] = [$scope.filteredItems[i]];

        } else {

            $scope.pagedItems[index].push($scope.filteredItems[i]);
        }
    }
};

$scope.paginate = function(page, limit) {

    if($scope.itemsPerPage.current != $scope.itemsPerPage.prev) {

        $scope.itemsPerPage.prev = $scope.itemsPerPage.current;
        $scope.search(null, null, $scope.items);
    }
};`

And the other Controller has inside (related to the topic):

$scope.sortBy = '-created';
$scope.$parent.$parent.checkboxKeys = {orderId: 'id', orderCanalId: 'orderCanalId', canalId: 'canalId'};

$scope.checkboxAll = false;
$scope.checkedItems = [];

$scope.search(null, $scope.sortBy, response.data.data);

When i click on the checkbox all check, it works fine, all the checks get checked, but when i click again, even tho theres no data inside the array (which is the ng-model for the checklist-model), the checks stay checked. Please Help!!!

@vigneshnallamad
Copy link

I'm facing the same issue. Please help.

@dsprogramacion
Copy link
Author

dsprogramacion commented Feb 12, 2017 via email

@vigneshnallamad
Copy link

Exactly.. I ended up using the md-checkbox way to solve the issue... Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants