我正在与大学项目合作。我有REST API和弹簧启动以及angularJs前端。这是我对angularJs的工作部分。
HomeService.js
this.getDeviceList = function getDeviceList(){
return $http.get(REST_SERVICE_URI + '/get_device_list')
}
this.getPeripheralList = function getPeripheralList(dev_hard_id){
return $http.get(REST_SERVICE_URI + '/get_peripherals/?dev_hard_id=' + dev_hard_id)
}
HomeController.js中的工作函数
$scope.getDeviceList = function () {
HomeService.getDeviceList()
.then (function success(response){
$scope.details = response.data;
$scope.errorMessage = '';
},
function error(response){
$scope.errorMessage = 'Error occured!!!';
$scope.message = response.data.message;
});
}
$scope.getPeripheralList = function (devHardwareId){
HomeService.getPeripheralList(devHardwareId)
.then (function success(response){
$scope.peripheralDetails = response.data;
$scope.errorMessage = '';
},
function error(response) {
$scope.errorMessage = 'Error occured!!!';
$scope.message = response.data.message;
});
}
html文件中的工作原语表。
<div class="panel-heading">
Logged in user: <b th:inline="text"> [[${#httpServletRequest.remoteUser}]] </b>
<br>
<span class="lead">Devices</span></div>
<div class="panel-body">
<div class="table-responsive">
<table class="table table-hover">
<thead>
<tr>
<th>Device Name</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="d in details track by d.devHardwareId">
<td>{{d.deviceName}}</td>
<td align="right"><button type="button" ng-click="getPeripheralList(d.devHardwareId)" class="btn btn-success custom-width">Attached Devices</button></td>
</tr>
</tbody>
</table>
</div>
</div>
以上代码运作良好。但我的要求是我需要选择一行,当时我需要为特定设备调用getPeripheralList(d.devHardwareId)
函数。为此,我使用ui-grid。
所以我在HomeController.js
中添加了功能$scope.gridOptions = { enableRowSelection: true, enableRowHeaderSelection: false };
$scope.gridOptions.columnDefs = [
{ name: 'Device HardwareId' },
{ name: 'Device Name'},
{ name: 'Device Ip Address'},
{ name: 'Attached Cameras' }
];
$scope.gridOptions.multiSelect = false;
$scope.gridOptions.modifierKeysToMultiSelect = false;
$scope.gridOptions.noUnselect = true;
$scope.gridOptions.onRegisterApi = function( gridApi ) {
$scope.gridApi = gridApi;
};
$scope.toggleRowSelection = function() {
$scope.gridApi.selection.clearSelectedRows();
$scope.gridOptions.enableRowSelection = !$scope.gridOptions.enableRowSelection;
$scope.gridApi.core.notifyDataChange( uiGridConstants.dataChange.OPTIONS);
};
$scope.deviceGrid = {
data: 'details',
};
并且也改变了html页面..
<button type="button" class="btn btn-success" ng-click="toggleRowSelection()">Toggle rowSelection</button>
<strong>rowSelection:</strong>
<span ng-bind="gridApi.grid.options.enableRowSelection"></span>
<div ui-grid="deviceGrid" ui-grid-selection class="grid"></div>
但这并不像我预期的那样有效。它允许选择我不想做的多行,我不明白如何使用ui-grid调用getPeripheralList(d.devHardwareId)
函数。 (每当我选择一行时,我想打电话给上面的方法)所以我希望得到这里的人的帮助。
答案 0 :(得分:1)
如果你不想在网格中选择多行,你只需要在gridOptions中配置它就像这样:
$scope.gridOptions = { enableRowSelection: true, enableRowHeaderSelection: false, multiSelect:false };
如果要在选择行时调用函数,则必须注册gridApi并使用它,您可以执行以下操作:
$scope.gridOptions = { enableRowSelection: true, enableRowHeaderSelection: false, multiSelect:false,
onRegisterApi: function (gridApi) {
gridApi.selection.on.rowSelectionChanged($scope, function (row) {
// Your call to your function goes here
});
}};