javascript - 如何使用 ui-grid 选择单行?

标签 javascript html angularjs ui-grid

我正在处理大学项目。我有使用 spring boot 和 angularJs 前端的 REST API。这是我在 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) 函数。 (每当我选择一行时,我想调用上面的方法)所以我希望得到这里某人的帮助。

最佳答案

如果您不想在网格中选择多行,您只需像这样在 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
    });
}};

关于javascript - 如何使用 ui-grid 选择单行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48983990/

相关文章:

javascript - 使用 JavaScript 解析 JSON 数组并返回所选部分

angularjs - Angular ,页面刷新时数据丢失

javascript - 动态添加文本框不起作用

javascript - 在 CSS 中复制 Flash 立方体旋转 : Preserve correct perspective

html - 如何将背景图像应用于一个 react 页面而不是整个应用程序?

javascript - Ng-repeat 整数值

javascript - 为什么在更改路线后 Angular 不破坏 Controller $scope ?

javascript - jQuery 垂直 slider 保持滑动

javascript - MechanicalSoup(python 3x)可以处理网站中的javascript吗?

javascript - jQuery,可选和文本对齐属性问题