11

is there any way to align the cell text values to centre/right? Thank you.

Here is a plnkr basic example.

2 Answers 2

38

As per oblivion19's comment, here is an example solution (with a little more context) by setting the cellClass property of a columnDefs object in gridOptions:

HTML:

<div ng-grid="gridOptions"></div>

JS:

$scope.someData = [{ "name": "MB", "age": 20 }, { "name": "KY", "age": 22 }];
$scope.gridOptions = {
    data: 'someData',
    columnDefs: [{
        field: 'name',
        displayName: 'Name',
        cellClass: 'grid-align'
     }, {
        field: 'age',
        displayName: 'Age'
    }]
};

CSS:

.grid-align {
    text-align: center;
}

Note that this solution only makes some columns centered (i.e., the ones with cellClass specified). To make all columns centered, simply add class="grid-align" to the ng-grid div instead.

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

1 Comment

Much easier than templating just for alignment and powerful too
7

Yes there is. You want to use the row or cell template. It is defined on the columnDefs in the controller where you set up the ng-grid.

columnDefs: [{field: 'name', displayName: 'Name'},
                 {field:'age', displayName:'Age', cellTemplate: '<div ng-class="{green: row.getProperty(col.field) > 30}"><div style="text-align:center;"  class="ngCellText">{{row.getProperty(col.field)}}</div></div>'}]
    };

I have an update Plunker below that has the columns aligned to the center.

Plunker

2 Comments

Thanks for your help mate. I found that I could add cellClass to the column, and style that class with text-align. But your method works as well. Cheers!
I really think oblivion's answer is the best solution here. No need to have a template just to set display properties.

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.