Reputation: 147
I'm working with university project. I have working REST API with spring boot and a angularJs front end. this is my working part of the 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)
}
Working functions in 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;
});
}
the working primitive table in html file.
<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>
above codes are working well. but my requirement is I need to select a single row and at the time I need to call getPeripheralList(d.devHardwareId)
function for the particular device. for that I'm using ui-grid.
so I've added functions in my 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',
};
and have changed html page too..
<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>
but this doesn't work as I expect. it allow to select multiple rows which I don't wanna do and I don't understand how to call the getPeripheralList(d.devHardwareId)
function with the ui-grid. (whenever I select a single row I wanna call above method) so I wish to get help from someone here.
Upvotes: 1
Views: 2194
Reputation: 13
This was the answer
$scope.deviceGrid = {
data:'details',
enableRowSelection: true,
enableRowHeaderSelection: false,
enableSelectAll: false,
multiSelect: false,
noUnselect: true,
onRegisterApi: function (gridApi) {
gridApi.selection.on.rowSelectionChanged($scope, function (row) {
var msg = 'row selected ' + row.isSelected;
console.log(row.entity.devHardwareId);
HomeService.getPeripheralList(row.entity.devHardwareId).then (function success(response){
$scope.peripheralDetails = response.data;
$scope.errorMessage = '';
},
function error(response) {
$scope.errorMessage = 'Error occured!!!';
$scope.message = response.data.message;
});
});
}};
Upvotes: 1
Reputation: 479
If you dont want to allow selecting multiple rows in your grid you only have to configure it in your gridOptions just like this:
$scope.gridOptions = { enableRowSelection: true, enableRowHeaderSelection: false, multiSelect:false };
If you want to call a function when a row is selected you have to register gridApi and use it, you could do something like the following:
$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
});
}};
Upvotes: 2