Dynamically change gridOptions columnDefs

删除回忆录丶 提交于 2019-12-19 23:22:21

问题


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?


回答1:


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




回答2:


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"
  }
]


来源:https://stackoverflow.com/questions/30199831/dynamically-change-gridoptions-columndefs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!