Dynamically change gridOptions columnDefs

In the next plunkr, I want the Gender popup filter to be dynamically populated with an ajax call, but it doesn't work.

plunkr link

If I comment out line 17

$scope.newSelectOptions=[{"value":"New Item 1","label":"New Item 1"},{"value":"New Item 2","label":"New Item 2"},{"value":"New Item 3","label":"New Item 3"},{"value":"New Item 4","label":"New Item 4"}];

      

it works as I would like (just by simulating it), but it seems to happen that the ajax call fills the variable $scope.newSelectOptions

after creation columnDefs

.

I tried changing line 34:

selectOptions: $scope.newSelectOptions

      

in

selectOptions: newSelectOptions
selectOptions: 'newSelectOptions'
selectOptions: '$scope.newSelectOptions'

      

but none of them work.

So how can I dynamically change this selectOptions

or other objects in a $scope.gridOptions.columnDefs

dynamic way?

+3


source to share


2 answers


You can move the definition of Gender column to your own js link as shown below,

var genderColumn =  { field: 'gender', filter: { 
          term: '1', 
          type: uiGridConstants.filter.SELECT, 
          //selectOptions: [ { value: '1', label: 'male' }, { value: '2', label: 'female' }, { value: '3', label: 'unknown'}, { value: '4', label: 'not stated' }, { value: '5', label: 'a really long value that extends things' } ]
          selectOptions: []
        }, 
        cellFilter: 'mapGender', headerCellClass: $scope.highlightFilteredHeader };

      

and assign it in the Def column as shown below

columnDefs: [
      // default
      { field: 'name', headerCellClass: $scope.highlightFilteredHeader },
      // pre-populated search field
      genderColumn,                               
      // no filter input
      { field: 'company', enableFiltering: false, filter: {
        noTerm: true,
        condition: function(searchTerm, cellValue) {
          return cellValue.match(/a/);
        }
      }},
      // specifies one of the built-in conditions
      // and a placeholder for the input
      {
        field: 'email',
        filter: {
          condition: uiGridConstants.filter.ENDS_WITH,
          placeholder: 'ends with'
        }, headerCellClass: $scope.highlightFilteredHeader
      },
      // custom condition function
      {
        field: 'phone',
        filter: {
          condition: function(searchTerm, cellValue) {
            var strippedValue = (cellValue + '').replace(/[^\d]/g, '');
            return strippedValue.indexOf(searchTerm) >= 0;
          }
        }, headerCellClass: $scope.highlightFilteredHeader
      },
      // multiple filters
      { field: 'age', filters: [
        {
          condition: uiGridConstants.filter.GREATER_THAN,
          placeholder: 'greater than'
        },
        {
          condition: uiGridConstants.filter.LESS_THAN,
          placeholder: 'less than'
        }
      ], headerCellClass: $scope.highlightFilteredHeader},
      // date filter
      { field: 'mixedDate', cellFilter: 'date', width: '15%', filter: {
          condition: uiGridConstants.filter.LESS_THAN,
          placeholder: 'less than',
          term: nextWeek
        }, headerCellClass: $scope.highlightFilteredHeader
      }
    ]

      



and in ajax response, when you have new select options, set it in gender column as shown below.

genderColumn.filter.selectOptions = [{"value":"New Item 1","label":"New Item 1"},{"value":"New Item 2","label":"New Item 2"},{"value":"New Item 3","label":"New Item 3"},{"value":"New Item 4","label":"New Item 4"}];

      

http://plnkr.co/edit/2Rbhz4XMSuhjvnO2IrR7?p=preview

+4


source


when you get a response from the server, get the column number you want to assign records to and set selectOptions values ​​for it like



vm.gridOptions.columnDefs[7].filter.selectOptions = [
  {
    "value": "New Item 1",
    "label": "New Item 1"
  },
  {
    "value": "New Item 2",
    "label": "New Item 2"
  },
  {
    "value": "New Item 3",
    "label": "New Item 3"
  },
  {
    "value": "New Item 4",
    "label": "New Item 4"
  }
]

      

+1


source







All Articles