UI网格AngularJS-如何更新UI网格中的列标题



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

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

这是JSFiddlehttp://jsfiddle.net/masoom/svcxh6hu/7/

控制器.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'
};

这个问题表明,更改gridOptions.columnDefs将更新网格中的列,但将新对象或数组分配给columnDefs不会产生影响,因为这样您将失去对api中对象的引用。如何动态更新角度ui网格列的列Def

我还没有找到任何有帮助的刷新,但使用此方法,您不需要调用refresh()。

最新更新