0

This is my columnDef

$scope.communityColDef = [{ field: 'name', displayName: 'Name' },
                          { field: 'description', displayName: 'Description' }];

I do not have an index number in my data, but when displaying it in a grid I want to display it with a row number. There was an option to set row number in jqGrid, but I cannot find a similar feature in angularJS.

3 Answers 3

2

Okay I found a way to do this but it has multiple problems. Firstly it is only visual, this means that I do not explicitly set field data or alter your items in a way that the previous answer does. Additionally I do not know how my solution interacts with different sorting or other ngTable functions.

Here is the code :

var app = angular.module('myApp', ['ngGrid']);
app.controller('MyCtrl', function($scope) {
    $scope.myData = [{name: "Moroni", age: 50},
                     {name: "Tiancum", age: 43},
                     {name: "Jacob", age: 27},
                     {name: "Nephi", age: 29},
                     {name: "Enos", age: 34}];  
    $scope.gridOptions = { 
        data: 'myData',
        columnDefs: [
        {displayName:'#', cellTemplate: '<div>{{$parent.$index + 1}}</div>'},
        {field: 'name', displayName: 'Name'},
        {field:'age', displayName:'Age'}]
    };
});

This is a purely cosmetic solution, row numbers are added in a new column all the way on the left similar to how it works in jquery. This has the advantage that you don't have to alter your data or items just to enable a row number but comes with the problems I mentioned before.

Here ia a plnkr displaying my solution.

Sign up to request clarification or add additional context in comments.

Comments

0

Before setting data to grid option add index inside your data json.

CODE

$scope.communityColDef = [{
    field: 'index',
    displayName: 'Sr. Number'
}, {
    field: 'name',
    displayName: 'Name'
}, {
    field: 'description',
    displayName: 'Description'
}];

$scope.items = data;
//adding index for showing serial number
angular.forEach($scope.items, function(item, index) {
    item.index = index + 1;
});
$scope.gridOptions.columnDefs = $scope.communityColDef;
$scope.gridOptions.data = $scope.items;

Hope this may help you.Thanks.

Comments

0

You can also use row.rowIndex in you cell template. I modified the above plunker

var app = angular.module('myApp', ['ngGrid']);
app.controller('MyCtrl', function($scope) {
    $scope.myData = [{name: "Moroni", age: 50},
                     {name: "Tiancum", age: 43},
                     {name: "Jacob", age: 27},
                     {name: "Nephi", age: 29},
                     {name: "Enos", age: 34}];  
    $scope.gridOptions = { 
        data: 'myData',
        columnDefs: [
        {displayName:'#', cellTemplate: '<div class="ngCellText" data-ng-class="col.colIndex()"><span>{{row.rowIndex + 1}}</span></div>'},
        {field: 'name', displayName: 'Name'},
        {field:'age', displayName:'Age'}]
    };
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.