gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 20:43:31 25 4
gpt4 key购买 nike

我正在处理大学项目。我有使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com