0

Let's say I have the following data:

let data = [
  {name: 'bob', bornOn: 1510174615339},
  {name: 'mary', bornOn: 1510164615339}
]

If I pass this into a ui-grid i'll get 2 columns. In order to parse the epoch time to a human readable format. I need to do something like this:

$scope.gridOptions.columnDefs = [
  {name: 'Name', field: 'name'},
  {name: 'Born On', field: 'bornOn', cellFilter: 'date:"yyyy-MM-dd\"'}
]

I would like to be able to do something like this instead:

$scope.gridOptions.columnDefs = [
  {name: 'Born On', field: 'bornOn', cellFilter: 'date:"yyyy-MM-dd\"'}
]

But then I lose the name field.

If I don't supply any column definitions I get every property back.

This is a bit contrived but in my actual project, I'm having to fetch objects with different properties but I don't want to manually set those different properties in their own column definitions. I just want to assign specific custom fields like the epoch times, but as soon as I set those column definitions the defaults don't get into the grid.

Is there a way to only customize a single column's columnDef but allow ui grid to default the rest of the property columns.

1 Answer 1

1

By default, if columnDefs array is empty, ui-grid generate columnDefs from data with this function:

 /**
   * @ngdoc function
   * @name buildColumnDefsFromData
   * @methodOf ui.grid.class:Grid
   * @description Populates columnDefs from the provided data
   * @param {function(colDef, col, gridOptions)} rowBuilder function to be called
   */
  Grid.prototype.buildColumnDefsFromData = function (dataRows){
    this.options.columnDefs =  gridUtil.getColumnsFromData(dataRows, this.options.excludeProperties);
  }

You can use gridUtil.getColumnsFromDataand customize a single column's in the array before you assigne it to gridOptions.columnDefs or you can modify columnDefs in $timeout function

$timeout(function(){
 console.log($scope.gridOptions.columnDefs);
});
Sign up to request clarification or add additional context in comments.

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.