我正在尝试构建一个表,其中一个列被选中,我想选择具有从服务器获取的值的选项。 我从服务器获得4,但选择的是第一个选项。
$scope.lotteryOptions = {
data: 'myData',
enableColumnResize: true,
keepLastSelected: false,
enableRowSelection: false,
columnDefs: [{field: 'field1', displayName: 'field1'},
{field: 'Status', displayName: 'Status', cellTemplate: selectTableTemplate, enableCellEdit: true},
};
var selectTableTemplate = "<select ng-selected=\"{{row.getProperty('Status')}}\"> <option value='1' class='ng-binding'>" + 1+ "</option>" +
"<option value='2' class='ng-binding'>" + 2 + "</option>" +
"<option value='3' class='ng-binding'>" + 3 + "</option>" +
"<option value='4' class='ng-binding'>" + 4 + "</option>" +
"<option value='5' class='ng-binding'>" + 5 + "</option>" +
"<option value='6' class='ng-binding'>" + 6 + "</option></select>";
html结果是:
<select ng-selected="4">...</select>
但不是选择4选择
答案 0 :(得分:2)
ng-selected
应该应用于option
代码,而不是select
(请参阅the docs)
$scope.lotteryOptions = {
columnDefs: [
{field: 'field1'},
{field: 'Status', cellTemplate: selectTableTemplate, enableCellEdit: true}
],
data: 'myData',
enableColumnResize: true,
enableRowSelection: false,
keepLastSelected: false
};
var selectTableTemplate = '<select>' +
' <option value="1" class="ng-binding" ng-selected="COL_FIELD == 1">' + 1 + '</option>' +
' <option value="2" class="ng-binding" ng-selected="COL_FIELD == 2">' + 2 + '</option>' +
' <option value="3" class="ng-binding" ng-selected="COL_FIELD == 3">' + 3 + '</option>' +
' <option value="4" class="ng-binding" ng-selected="COL_FIELD == 4">' + 4 + '</option>' +
' <option value="5" class="ng-binding" ng-selected="COL_FIELD == 5">' + 5 + '</option>' +
' <option value="6" class="ng-binding" ng-selected="COL_FIELD == 6">' + 6 + '</option>' +
'</select>';
答案 1 :(得分:0)
ng-selected
已经是一个角度属性,因此无需插入{{ }}
:
var selectTableTemplate = "<select> <option ng-selected=\"row.getProperty('Status') == 1\" value='1' class='ng-binding'>" + 1 + "</option>" +
"<option ng-selected=\"row.getProperty('Status') == 2\" value='2' class='ng-binding'>" + 2 + "</option>" +
"<option ng-selected=\"row.getProperty('Status') == 3\" value='3' class='ng-binding'>" + 3 + "</option>" +
"<option ng-selected=\"row.getProperty('Status') == 4\" value='4' class='ng-binding'>" + 4 + "</option>" +
"<option ng-selected=\"row.getProperty('Status') == 5\" value='5' class='ng-binding'>" + 5 + "</option>" +
"<option ng-selected=\"row.getProperty('Status') == 6\" value='6' class='ng-binding'>" + 6 + "</option></select>";
注意:这也假设Status
是一个值,而不是一个对象。
<强>更新强>
ng-selected
不是select
的属性。它只是option
的一个属性。