在下面的plunkr中,我希望通过ajax调用动态填充Gender下拉过滤器,但它似乎不起作用。
如果我评论第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"}];
它的工作方式与我想要的一样(只是模拟它),但似乎发生的事情是ajax调用在$scope.newSelectOptions
创建后填充变量columnDefs
。
我尝试过改变第34行:
selectOptions: $scope.newSelectOptions
为:
selectOptions: newSelectOptions
selectOptions: 'newSelectOptions'
selectOptions: '$scope.newSelectOptions'
但它们都不起作用。
那么如何动态地动态更改selectOptions
中的$scope.gridOptions.columnDefs
或其他对象?
答案 0 :(得分:4)
您可以将Gender列定义移动到自己的js引用,如下所示
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 };
并将其分配到columnDef中,如下所示
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
}
]
并在ajax响应中,当您有新的选择选项时,将其设置在性别列中,如下所示。
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"}];
答案 1 :(得分:1)
当您从服务器获得响应时,获取要为其分配记录的列号,并为其设置selectOptions值 例如
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"
}
]