Javascript 如何使用ui网格选择单行?

Javascript 如何使用ui网格选择单行?,javascript,html,angularjs,ui-grid,Javascript,Html,Angularjs,Ui Grid,我在大学项目部工作。我有一个带spring boot和angularJs前端的RESTAPI。这是我在angularJs的工作部分 HomeService.js this.getDeviceList = function getDeviceList(){ return $http.get(REST_SERVICE_URI + '/get_device_list') } this.getPeripheralList = function getPeripheralList(dev_h

我在大学项目部工作。我有一个带spring boot和angularJs前端的RESTAPI。这是我在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;

    });
}
$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文件中的工作原语表

<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>
并且也改变了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网格调用
getPeripheralList(d.devHardwareId)
函数。(每当我选择一行时,我想调用上面的方法)因此我希望从这里的人那里得到帮助。

如果不想允许在网格中选择多行,只需在网格选项中配置它,如下所示:

$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
    });
}};
这就是答案

$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;

});
    });
}};

如何重命名和筛选数据库中的某些字段?Ui网格有一些内置筛选器。但它允许您创建自己的过滤函数。也许您应该看看ui网格的文档。希望有帮助!