angular-ui/ui-grid

Default sort in columnDefs isn't present when onRegisterApi function is called

Open

#4,390 建立於 2015年9月17日

在 GitHub 查看
 (2 留言) (0 反應) (0 負責人)JavaScript (5,395 star) (2,496 fork)batch import
good first issue

描述

I've set default sorts for most of my grids and call my external api to retrieve the data during the onRegisterApi call.

Here's my onRegisterApi call:

     onRegisterApi: function (gridApi) {
            $scope.gridApi = gridApi;

            // Setup sortChanged handler
            $scope.gridApi.core.on.sortChanged($scope, function (grid, sortColumns) {
                $scope.refresh_collection();
            });

            // Setup paginationChanged handler
            $scope.gridApi.pagination.on.paginationChanged($scope, function (newPage, pageSize){
                $scope.refresh_collection();
            });

            $scope.$watch('search', function (newVal, oldVal) {
                if (newVal !== oldVal) {
                    $scope.refresh_collection();
                }
            }, true);

            // Initial load
            init();
        },

I make my API calls and retrieve the sorting information in the $scope.refresh_collection() function, which is called from init() when the api first loads.

Here's how I get my sorting data to send to the server:

// Handle sorting
        var sortColumns = $scope.gridApi.grid.getColumnSorting();
        if (sortColumns && sortColumns[0]) {
            pagination.order_by = sortColumns[0].name + ':' + sortColumns[0].sort.direction.toUpperCase();
        }
        else {
            pagination.order_by = '';
        }

However, when I try to access $scope.gridApi.grid.getColumnSorting(), it comes back as an empty array.

I've noticed that I don't see the sorting icon appearing until slightly after the grid loads, so I'm assuming that the default sort isn't applied until after the onRegisterApi() function. If that's the case, I'd think it should also trigger the core.on.sortChanged event so that it can be handled as if the sort has been changed by the user.

I'm ok with either behaviour; either adding the default sort when onRegisterApi() fires, or having it fire the sortChanged event when the default sort is actually applied.

貢獻者指南