Michael
Michael

Reputation: 13616

How to sort table by column when grid initialize?

I use ui-grid in my project.

Here is declaration of the table in html:

 <div id="grid1" ui-grid="gridOptions1" class="grid"></div>

Here is defenition of the table in controller:

  app.controller('MainCtrl', ['$scope', '$http', 'uiGridConstants', function ($scope, $http, uiGridConstants) {
  $scope.gridOptions1 = {
    enableSorting: true,
    columnDefs: [
      { field: 'name' },
      { field: 'gender' },
      { field: 'company', enableSorting: false }
    ],
    onRegisterApi: function( gridApi ) {
      $scope.grid1Api = gridApi;
    }
  };


 $http.get('https://cdn.rawgit.com/angular-ui/ui-grid.info/gh-pages/data/100.json')
    .success(function(data) {
      $scope.gridOptions1.data = data;
    });

And here is working PLUNKER!

My question is how to sort table by Gender column when grid initialize?

Upvotes: 1

Views: 1089

Answers (1)

NTP
NTP

Reputation: 4448

you have to set it in columnDef for sorting when grid is initialised.

  $scope.gridOptions1 = {
    enableSorting: true,
    columnDefs: [
      { field: 'name' },
      { field: 'gender',sort: { direction: uiGridConstants.ASC, priority: 1 } },
      { field: 'company', enableSorting: false }
    ],
    onRegisterApi: function( gridApi ) {
      $scope.grid1Api = gridApi;
    }
  };

you can also set direction to desc if you want to display male first

direction: uiGridConstants.DESC

Plunker

Upvotes: 2

Related Questions