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

ng-grid Server Side Sorting error #223

Closed
karim017 opened this issue Mar 8, 2013 · 11 comments
Closed

ng-grid Server Side Sorting error #223

karim017 opened this issue Mar 8, 2013 · 11 comments

Comments

@karim017
Copy link

karim017 commented Mar 8, 2013

Client Side Sorting in ng-grid is working
But Server Side Sorting is not working.
Is there anyone who can help me on this issue. I am using latest version of ng-grid.
Here is my sample code..................

$scope.pagingOptions = {
pageSizes: [5, 10, 50],
pageSize: 5,
totalServerItems: 0,
currentPage: 1
};

$scope.sortInfo = { field: 'TransactionType', direction: 'asc', };


$scope.$watch('sortInfo', function () {
  //  alert('Sorted Info: ' + $scope.sortInfo);
    $scope.getPagedDataAsync($scope.pagingOptions.pageSize, $scope.pagingOptions.currentPage);
}, true);
$scope.$watch('sortInfo.field', function () {
    //alert('Sorted field: ' + $scope.sortInfo.field);
    $scope.getPagedDataAsync($scope.pagingOptions.pageSize, $scope.pagingOptions.currentPage);
}, true);

$scope.$watch('sortInfo.column', function () {
   // alert('Sorted column: ' + $scope.sortInfo.column);
    $scope.getPagedDataAsync($scope.pagingOptions.pageSize, $scope.pagingOptions.currentPage);
}, true);

$scope.$watch('sortInfo.direction', function () {
    //alert('Sorted direction: ' + $scope.sortInfo.direction);
    $scope.getPagedDataAsync($scope.pagingOptions.pageSize, $scope.pagingOptions.currentPage);
}, true);



$scope.$watch('pagingOptions', function () {
    $scope.getPagedDataAsync($scope.pagingOptions.pageSize, $scope.pagingOptions.currentPage);
}, true);


$scope.setPagingData = function (data, page, pageSize) {
    var pagedData = data.slice((page - 1) * pageSize, page * pageSize);
    $scope.priorities = pagedData;
    $scope.pagingOptions.totalServerItems = data[0].TotalCount;
    if (!$scope.$$phase) {
        $scope.$apply();
    }
};

$scope.getPagedDataAsync = function (pagesize, page) {

    $scope.SearchSettlementData();
    if (!$scope.$$phase) {
        $scope.$apply();
    }
};

$scope.gridOptionsTestReport = {
    data: 'gridOptionsTestReport ', enableSorting: true, showFilter: false,
    sortinfo: $scope.sortInfo,
    selectedItems: $scope.mySelections,
    enablePaging: true,
    enableSorting: true,
    useExternalSorting: true,
    pagingOptions: $scope.pagingOptions,        
    keepLastSelected: true,
    showColumnMenu: false, multiSelect: false, displaySelectionCheckbox: false,
    columnDefs: [{ field: 'TransactionNumber', displayName: "Transaction Number", cellTemplate: '<a href="#view-detail-dialog" name="{{row.getProperty(col.field)}}" onclick="javascript:testing(this)" data-toggle="modal" style="margin-top: 5px;" >{{row.getProperty(col.field)}} </a>', width: '*', sortable: false },
        { field: 'TransactionDate', displayName: "Transaction Date", width: '*',  sortable: true },
        { field: 'TransactionType', displayName: "Transaction Type", width: '*', sortable: true },
        { field: 'GrossSalesAmount', displayName: "Gross Sales Amount", width: '*', sortable: true }


    ]

};
@orneryd
Copy link
Contributor

orneryd commented Mar 8, 2013

hi karim,

I realize we forgot to update the documentation around some changes we made to sortInfo

each of the properties of sortInfo are now arrays[] and plural. this was done to accomodate multi-column sorting.

However, I see that we may need to make a change for it to work properly but I do have a workaround for now.

http://plnkr.co/edit/9CVUuwtlHGsTM2vaxYu3?p=preview

@karim017
Copy link
Author

karim017 commented Mar 8, 2013

hi Tim,

I have changed sortinfo as array but still does not fire server method in my project.

I have also change your plnkr code but code not get data in plnkr grid
please look at following code that i changed in your plnkr

var app = angular.module('myApp', ['ngGrid']);
app.controller('MyCtrl', function($scope, $http) {
$scope.filterOptions = {
filterText: "",
useExternalFilter: true
};
$scope.pagingOptions = {
pageSizes: [250, 500, 1000],
pageSize: 250,
totalServerItems: 0,
currentPage: 1
};
$scope.setPagingData = function(data, page, pageSize){
var pagedData = data.slice((page - 1) * pageSize, page * pageSize);
$scope.myData = pagedData;
alert(data.length);
$scope.pagingOptions.totalServerItems = data.length;
if (!$scope.$$phase) {
$scope.$apply();
}
};
$scope.getPagedDataAsync = function (pageSize, page, searchText) {
setTimeout(function () {
var data;
alert("getPagedDataAsync");
if (searchText) {
var ft = searchText.toLowerCase();
$http.get('largeLoad.json').success(function (largeLoad) {
data = largeLoad.filter(function(item) {
return JSON.stringify(item).toLowerCase().indexOf(ft) != -1;
});
$scope.setPagingData(data,page,pageSize);
});
} else {
$http.get('largeLoad.json').success(function (largeLoad) {
alert("test");
$scope.setPagingData(largeLoad,page,pageSize);
});
}
}, 100);
};

$scope.getPagedDataAsync($scope.pagingOptions.pageSize, $scope.pagingOptions.currentPage);

$scope.$watch('pagingOptions', function () {
    $scope.getPagedDataAsync($scope.pagingOptions.pageSize, $scope.pagingOptions.currentPage, $scope.filterOptions.filterText);
}, true);
$scope.$watch('filterOptions', function () {
    $scope.getPagedDataAsync($scope.pagingOptions.pageSize, $scope.pagingOptions.currentPage, $scope.filterOptions.filterText);
}, true);   

$scope.gridOptions = {
    data: 'gridOptionsTestReport ', 
    enableSorting: true, 
    showFilter: false,
    sortinfo: $scope.sortInfo,
    selectedItems: $scope.mySelections,
    enablePaging: true,        
    useExternalSorting: true,
    pagingOptions: $scope.pagingOptions,        
    keepLastSelected: true,
    showColumnMenu: false,
    multiSelect: false, 
    displaySelectionCheckbox: false,
    columnDefs: [
        { field: 'name', displayName: "name", width: '*',  sortable: true },
        { field: 'allowance', displayName: "allowance", width: '*', sortable: true },
        { field: 'paid', displayName: "paid", width: '*', sortable: true }]
};


$scope.sortInfo = [{ fields:[ 'name'], directions: ['asc']}];


$scope.$watch('gridOptions.ngGrid.config.sortInfo', function () {
    alert('Sorted Info: ' + $scope.sortInfo);
}, true);

});

@karim017
Copy link
Author

karim017 commented Mar 8, 2013

Is there anyone who can help me on this

@karim017 karim017 closed this as completed Mar 8, 2013
@karim017 karim017 reopened this Mar 8, 2013
@karim017
Copy link
Author

karim017 commented Mar 8, 2013

Hi I have fix my that issue
Now I have another issue on that it is that how could i get the selected field and direction on that event
I tried with $scope.sortInfo.field and $scope.sortInfo.direction but getting undefined value
Can you help me on that issue

@karim017
Copy link
Author

karim017 commented Mar 8, 2013

The paging bar is also disappeared for new version
Is there any new syntax for paging

@orneryd
Copy link
Contributor

orneryd commented Mar 8, 2013

its just the default options. the footer is hidden by default. please see
the documentation for more details.

On Fri, Mar 8, 2013 at 6:58 AM, karim017 notifications@github.com wrote:

The paging bar is also disappeared for new version
Is there any new syntax for paging


Reply to this email directly or view it on GitHubhttps://github.com//issues/223#issuecomment-14624266
.

-Tim Sweet

orneryd added a commit that referenced this issue Mar 9, 2013
@orneryd
Copy link
Contributor

orneryd commented Mar 9, 2013

ok, so for the sorting i remembered there is a scope event that you can watch for to get the current sorting info

    $scope.$on('ngGridEventSorted', function (sortInfo) {
        alert('Sorted Info: ' +sortInfo);
    });

http://plnkr.co/edit/ek3N1Dyzj6IzCK1doPuH?p=preview

also fixed some issues related to this in commit 6c476f6 which will be merged into main here shortly

@orneryd
Copy link
Contributor

orneryd commented Mar 9, 2013

fixed with #228

@orneryd orneryd closed this as completed Mar 9, 2013
@ShadesOB
Copy link

Could be something I did. But I had to use
$scope.$on('ngGridEventSorted', function (something,sortInfo) {

@hinguabhishek
Copy link

Hi,
I had implemented server side sorting and paging for Grid, but the actual issue is some how ngGridEventSorted event fired infinite time and its generate a deadlock, please check code below

var app = angular.module('myApp', ['ngGrid']);
app.controller('MyCtrl', function ($scope, $http) {
$scope.filterOptions = {
filterText: "",
useExternalFilter: true
};
$scope.pagingOptions = {
pageSizes: [50, 100, 150],
pageSize: 50,
totalServerItems: 0,
currentPage: 1
};
$scope.sortOptions = {
column: "name",
order: "desc"
};
$scope.setPagingData = function (data, page, pageSize) {
var pagedData = data.slice((page - 1) * pageSize, page * pageSize);
$scope.myData = pagedData;
$scope.pagingOptions.totalServerItems = data.length;
if (!$scope.$$phase) {
$scope.$apply();
}
};
$scope.getPagedDataAsync = function (pageSize, page, searchText) {

    var data;
    var request = {};
    request.pageOptions = {};
    request.pageOptions.PageSize = $scope.pagingOptions.pageSize;
    request.pageOptions.pageIndex = $scope.pagingOptions.currentPage;
    request.sortOptions = {};
    request.sortOptions.column = $scope.sortOptions.column;
    request.sortOptions.order = $scope.sortOptions.order;
    $http.post('api/employee/GetAllRecords', request).success(function (largeLoad) {
        //$scope.setPagingData(largeLoad, page, pageSize);
        $scope.myData = largeLoad.result;
        $scope.pagingOptions.totalServerItems = largeLoad.pageOptions.totalRecords;
        if (!$scope.$$phase) {
            $scope.$apply();
        }
    });

};



//    $scope.$watch('pagingOptions', function () {
//        $scope.getPagedDataAsync($scope.pagingOptions.pageSize, $scope.pagingOptions.currentPage, $scope.filterOptions.filterText);
//    }, true);
//    $scope.$watch('filterOptions', function () {
//        $scope.getPagedDataAsync($scope.pagingOptions.pageSize, $scope.pagingOptions.currentPage, $scope.filterOptions.filterText);
//    }, true);

$scope.gridOptions = {
    data: 'myData ',
    enableSorting: true,
    showFilter: false,
    sortinfo: $scope.sortInfo,
    showFooter: true,
    selectedItems: $scope.mySelections,
    enablePaging: true,
    enableSorting: true,
    useExternalSorting: true,
    pagingOptions: $scope.pagingOptions,
    keepLastSelected: true,
    showColumnMenu: false,
    multiSelect: false,
    totalServerItems: 'pagingOptions.totalServerItems',
    displaySelectionCheckbox: false,
    columnDefs: [{ field: 'name', displayName: "Name", sortable: true},
        { field: 'allowance', displayName: "Allowance", width: '*', sortable: true},
        { field: 'paid', displayName: "Paid", width: '*', sortable: false }
        ],
    pagingOptions: $scope.pagingOptions
};


$scope.sortInfo = [{ fields: ['name'], directions: ['asc'] }, { fields: ['allowance'], directions: ['asc'] }, { fields: ['paid'], directions: ['asc']}];


    $scope.$on('ngGridEventSorted', function (something, sortInfo) {
        alert("hi");
        //alert('Sorted Info: ' + sortInfo);
        $scope.sortOptions.column = sortInfo.fields[0] === undefined ? "name" : sortInfo.fields[0];
        $scope.sortOptions.order = sortInfo.directions[0] === undefined ? "asc" : sortInfo.directions[0];
        $scope.getPagedDataAsync($scope.pagingOptions.pageSize, $scope.pagingOptions.currentPage, $scope.filterOptions.filterText);
    });

// $scope.$watch('sortInfo', function (newval, oldval) {
// console.log("sort watch fired");
// if (!angular.equals(newval, oldval)) {
// console.log("new sorft info watch fired");
// $scope.sortOptions.column = newval.fields[0] === undefined ? "name" : newval.fields[0];
// $scope.sortOptions.order = newval.directions[0] === undefined ? "asc" : newval.directions[0];
// $scope.getPagedDataAsync($scope.pagingOptions.pageSize, $scope.pagingOptions.currentPage, $scope.filterOptions.filterText);
// }
// },true);
// $scope.setPagingData = function (data, page, pageSize) {
// var pagedData = data.slice((page - 1) * pageSize, page * pageSize);
// $scope.priorities = pagedData;
// $scope.pagingOptions.totalServerItems = data[0].TotalCount;
// if (!$scope.$$phase) {
// $scope.$apply();
// }
// };
$scope.getPagedDataAsync($scope.pagingOptions.pageSize, $scope.pagingOptions.currentPage);
});
also watch not trigger for sortInfo scope member, please help me to resolve this issue.

@jefffohl
Copy link

@Addaymichel - I had the same problem. I believe the reason that this is happening is that the sort event is fired whenever the data of the grid is updated. With server-side sorting, I was replacing the data in the grid whenever I did a sort. This caused the ng-grid sort event to continuously fire. I needed a way to make sure that this only happened once. This was my solution:

  • I created a custom header. Within this header, I replaced the ng-click="col.sort($event)" with a call to my own function which passes the col object and the $event, like this: ng-click="mySort(col,$event)"
  • My sort function looks like this:
$scope.mySort = function(col,e) {
    $scope.mySortCompleted = false;
    col.sort(e);
    $scope.mySortCompleted = true;
  };
  • Then, I watch for the sort event firing, where I check to see if my sort function has been called:
$scope.$on('ngGridEventSorted', function() {
    if ($scope.mySortCompleted) {
      return;
    }
    // code for retrieving data from the server goes here...
  });

EDIT: This is for version 2.0 of ng-grid.

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

No branches or pull requests

5 participants