-6ren">
gpt4 book ai didi

javascript - UI Grid AngularJS - 如何更新 ui-grid 中的列标题?

转载 作者:行者123 更新时间:2023-11-30 00:31:51 26 4
gpt4 key购买 nike

我正在开发一个数据驱动的应用程序,其中的列(表标题)会根据用户点击进行动态更新。我正在从 JSON 文件加载数据。

当我使用带有 ng-repeat="heading in gridHeaders">{{ heading }} 的普通表时它有效,但是当我包含 ui-grid 并且 View 使用不同的数据集更新时,网格单元变为空,列标题保持不变。如何更新 ui-grid 中的列定义?

这是 JSFiddle http://jsfiddle.net/masoom/svcxh6hu/7/

Controller .js

$scope.gridOptions = {
data: 'grid',
columnDefs : '' /* not sure how to use the <ng-repeat="heading in gridHeaders"> {{ heading}} here*/
};

/*
Watch for the change in the Tree's current node.
When user clicks on a node, $scope.currentNode will update
*/
$scope.$watch(function () {
return $scope.currentNode;
}, function () {
$scope.displayGrid(angular.fromJson($scope.currentNode.json));
});

/*
Sets the Grid's data model. The view will update when this is called.
*/
$scope.displayGrid = function (data) {
$scope.grid = data;
$scope.gridHeaders = new Array();
if (typeof data != 'undefined') {
for (var key in data[0]) {
$scope.gridHeaders.push(key);
}
}
}

index.html

<table ui-grid="{ data: grid }" class="grid1" ng-show="gridHeaders.length>0" >

<thead>
<tr>
<th ng-repeat="heading in gridHeaders">{{ heading }}</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="obj in grid">
<td ng-repeat="heading in gridHeaders">{{ obj[heading] }}</td>
</tr>
</tbody>
</table>
<div ui-grid="gridOptions" class="grid1" ng-show="gridHeaders.length>0" >
</div>

最佳答案

$scope.gridHeaders1 =  [{field: 'name', displayName: 'Name'}, {field:'age', displayName:'Age'}];            
$scope.gridHeaders2 = [{field: 'name', displayName: 'Name'}, {field:'age', displayName:'Age'}, {field:'occupation', displayName:'Occupation'}];
$scope.gridOptions = {
data: 'myData',
columnDefs: 'gridHeaders1'
};

关于javascript - UI Grid AngularJS - 如何更新 ui-grid 中的列标题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29165191/

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