当包含div大小的父级更改时,如何动态更改ng-grid表大小?

时间:2014-02-20 19:44:18

标签: html css angularjs twitter-bootstrap

我正在使用ng-class和一个表达式来更改包含div的大小,该表达式用于评估是否显示编辑表单。如果显示编辑表单,我想更改包含ng-grid和ng-grid本身的div的大小。

<div class=" row-fluid">
    <div ng-class="{'span7' : displayEditForm == true, 'span12': displayEditForm == false}" >
        <ul class="nav nav-tabs" style="margin-bottom: 5px;">
            <li class="active"><a href="#" ng-click="getActivitiesThatNeedMyApproval()" data-toggle="tab">Activities Needing My Approval</a></li>
            <li><a href="#" ng-click="getMyActivitiesNeedingApproval()" data-toggle="tab">My Activities Needing Approval </a></li>
            <li><a href="#" ng-click="getMyActivities()" data-toggle="tab">My Activities</a></li>
        </ul>
       <div class="edus-admin-manage-grid span12"  style="margin-left:0;" ng-grid="gridOptions"></div>
    </div>
    <div class="span5" ng-show="displayEditForm">
        <div class="edus-activity-container">
            <div class="edus-admin-activities-grid">
                <div ng-include="'/partials/' + activity.object.objectType + '.html'" class="edus-activity"></div>
                <!--  <div ng-include="'/partials/admin-activity-actions.html'"></div>-->
            </div>
        </div>
        <div ng-include="'/partials/admin-edit-activity-grid-form.html'"></div>
    </div>
</div>

包含导航栏和网格的div通过ng-class(从span12到span7)更改大小,但ng-grid不会刷新。如果在父div中发生变化,如何触发ng-grid的刷新?

我在下面包含了我的gridOptions:

$scope.gridOptions = {
plugins: [gridLayoutPlugin],
data : 'activities',
showFilter: true,
/*    enablePaging: true,*/
showColumnMenu: true,
/*  showFooter: true,*/
rowHeight: 70,
enableColumnResize: true,
multiSelect: false,
selectedItems: $scope.selectedActivities,
afterSelectionChange: function(rowItem,event){
   if($scope.selectedActivities && $scope.selectedActivities.length > 0){
        $scope.activity = $scope.selectedActivities[0];
        $scope.activityViewState.index = $scope.activities.indexOf($scope.activity);
        $scope.displayEditForm = true;
        console.log("DEBUG :::::::::::::::: updated displayEditForm.",            $scope.displayEditForm);

                        if($scope.activity.ucdEdusMeta.startDate) {

                           // $scope.activity.ucdEdusMeta.startDate = new Date($scope.activity.ucdEdusMeta.startDate);
                            $scope.edit.startDate = moment($scope.activity.ucdEdusMeta.startDate).format("MM/DD/YYYY");
                            $scope.edit.startTime = moment($scope.activity.ucdEdusMeta.startDate).format("hh:mm A");
                        }

                        if($scope.activity.ucdEdusMeta.endDate) {

                           // $scope.activity.ucdEdusMeta.endDate = new Date($scope.activity.ucdEdusMeta.endDate);
                            $scope.edit.endDate = moment($scope.activity.ucdEdusMeta.endDate).format("MM/DD/YYYY");
                            $scope.edit.endTime = moment($scope.activity.ucdEdusMeta.endDate).format("hh:mm A");
                        }

                    }
                },
       /* pagingOptions: { pageSizes: [5, 10, 20], pageSize: 10, totalServerItems: 0, currentPage: 1 },*/
       columnDefs: [
            {field: 'title', displayName: 'Title', width:'15%',
                        cellTemplate: '<div class="ngCellText", style="white-space: normal;">{{row.getProperty(col.field)}}</div>'},
            {field: 'actor.displayName', displayName: 'DisplayName', width:'10%',
                        cellTemplate: '<div class="ngCellText", style="white-space: normal;">{{row.getProperty(col.field)}}</div>'},
            {field: 'object.content', displayName:'Content', width:'35%',
                        cellTemplate: '<div class="ngCellText", style="white-space: normal;">{{row.getProperty(col.field)}}</div>'},
            {field: 'ucdEdusMeta.startDate', displayName: 'Start Date', width:'20%',
                        cellTemplate: '<div class="ngCellText" ng-class="col.colIndex()"><span ng-cell-text>{{row.getProperty(col.field) | date:"short"}} </span></div>'},
            {field: 'ucdEdusMeta.endDate', displayName: 'End Date', width:'20%',
                        cellTemplate: '<div class="ngCellText" ng-class="col.colIndex()"><span ng-cell-text>{{row.getProperty(col.field) | date:"short"}} </span></div>'}
          // {field: '', displayName: ''},
          ]
};

这是网格使用的CSS:

.edus-admin-manage-grid {
    border: 1px solid rgb(212,212,212);
    width: 100%;
    height: 700px
}

1 个答案:

答案 0 :(得分:6)

您可以使用ng-grid的布局插件(ng-grid-layout.js)。它应该与ngGrid一起位于:

ng-grid/plugins/ng-grid-layout.js

(更新:现在在https://github.com/angular-ui/ng-grid/blob/2.x/plugins/ng-grid-layout.js

您必须在主index.html文件中包含指向此js文件的其他脚本标记。包含这个与ng-grid.js的顺序非常重要。

您必须在displayEditForm上设置监视,然后调用插件的updateGridLayout()函数。

所以它会是这样的:

var gridLayoutPlugin = new ngGridLayoutPlugin();

// include this plugin with your grid options
$scope.gridOptions = {
    // your options and:
    plugins: [gridLayoutPlugin]
};

// make sure grid redraws itself whenever
// the variable that ng-class uses has changed:
$scope.$watch('displayEditForm', function() {
    gridLayoutPlugin.updateGridLayout();
});

根据我的理解,手表通常属于link功能而不是控制器,但它可以在任何一个位置工作。您还可以进一步说:

$scope.$watch('displayEditForm', function(newVal, oldVal) {
    if (newVal !== undefined && newVal !== oldVal) {
        gridLayoutPlugin.updateGridLayout();
    }
});

确保仅在数据从true / false切换时触发此操作。如果您的数据最初未定义,并且在给它初始值之前浪费时间调用网格重绘,这就很重要。