Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically change gridOptions columnDefs

In the following plunkr I want the Gender dropdown filter to be populated dynamically by the ajax call, but it doesn't appear to 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 like I would like (just simulating it), but what appears to happen is that the ajax call populates the variable $scope.newSelectOptions after the columnDefs has been created.

I have tried changing line 34:

selectOptions: $scope.newSelectOptions

to:

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

but none of them work.

So how can I dynamically alter that selectOptions or other objects within the $scope.gridOptions.columnDefs dynamically?

like image 394
DeclanMcD Avatar asked Mar 17 '23 02:03

DeclanMcD


2 Answers

You can move the Gender column definition to its own js reference like 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 columnDef like 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 the ajax response when you have the new select options set it on the gender column like 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

like image 191
Kathir Avatar answered Mar 18 '23 16:03

Kathir


when you get response from server , get the column number to which you want to assign records and set selectOptions values to it e.g.

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"
  }
]
like image 34
Raunak Mali Avatar answered Mar 18 '23 16:03

Raunak Mali