2014-04-30 31 views
18

Làm cách nào để nhận ng lưới để tự động thay đổi kích thước chiều cao dựa trên kích thước trang? Tài liệu trên trang web của lưới ng sử dụng chiều cao cố định. Giải pháp tốt nhất mà tôi đã thấy đến từ số này link:Ng/grid Chiều cao tự động/động

.ngViewport.ng-scope { 
    height: auto !important; 
    overflow-y: hidden; 
} 

.ngTopPanel.ng-scope, .ngHeaderContainer { 
    width: auto !important; 
} 

Điều này không hoạt động với phân trang phía máy chủ. Tôi đã sao chép ví dụ phân trang phía máy chủ từ trang web của lưới ng và áp dụng css ở trên, nhưng như bạn thấy, chỉ có 6 hàng đầu tiên được hiển thị: http://plnkr.co/edit/d9t5JvebRjUxZoHNqD7K?p=preview

Và {height: 100%} không work ...

Trả lời

31

Bạn có thể thử sử dụng lưới ng Flexible Height Plugin, tất cả những gì bạn cần làm là thêm plugin này vào thuộc tính plugin trong tùy chọn lưới và anh ấy sẽ lo phần còn lại.

Ví dụ:

$scope.gridOptions = { 
    data: 'myData', 
    enablePaging: true, 
    showFooter: true, 
    totalServerItems: 'totalServerItems', 
    pagingOptions: $scope.pagingOptions, 
    filterOptions: $scope.filterOptions, 
    plugins: [new ngGridFlexibleHeightPlugin()] 
}; 

sống Ví dụ:. http://plnkr.co/edit/zNHhsEVqmpQmFWrJV1KQ?p=preview

+1

Đồng ý, làm việc một cách hoàn hảo cho tôi. Plugin được di chuyển vì 'master' hiện là' 3.x' nên tôi đã cập nhật URL trong câu trả lời. –

+1

không hoạt động với ui-grid - v3.0.0-rc.16, có cách nào khác không? –

+0

@Sajid Ali, theo như tôi biết, ui-grid đã có hỗ trợ cho chiều cao động. –

0

$ ("gridStyle ") css (" height", "");

loại bỏ sở hữu chiều cao của bạn về chiều cao năng động lớp gridstyle của bạn sau đó tự động đặt trên ng-lưới của bạn ..

4

Nếu bạn không muốn thêm bất kỳ plugin Tôi đã thực hiện một giải pháp dễ dàng để tự động thay đổi của bảng chiều cao dựa trên các hàng nhìn thấy được. Tôi đang sử dụng Ui-Grid-Unstable v3.0. Không cần phải chạm vào CSS.

HTML của tôi trông giống như:

<div id="grid1" ui-grid="gridOptions" ui-grid-grouping ui-grid-exporter class="grid"></div> 

Trong điều khiển của bạn add:

$scope.$watch('gridApi.core.getVisibleRows().length', function() { 
    if ($scope.gridApi) { 
     $scope.gridApi.core.refresh(); 
     var row_height = 30; 
     var header_height = 31; 
     var height = row_height * $scope.gridApi.core.getVisibleRows().length + header_height; 
     $('.grid').height(height); 
     $scope.gridApi.grid.handleWindowResize(); 
    } 
}); 

Và để tắt di chuyển thêm dòng sau nơi gridOptions được khởi tạo:

enableVerticalScrollbar : uiGridConstants.scrollbars.NEVER, 

Make chắc chắn uiGridConstants được chuyển vào bộ điều khiển của bạn:

angular.module('app').controller('mainController', function($scope, uiGridConstants) { ... 

Hy vọng điều này sẽ hữu ích!

+0

10x 4 '$ scope.gridApi.core.refresh()' && '$ scope.gridApi.grid.handleWindowResize()' =) hữu ích .. – oCcSking

0

bạn có thể thêm tự động kiểu như thế này:

ng-style="{ 'height': myData.length*34 }"myData

0

tôi thấy sử dụng đoạn mã này vào stylesheet giải quyết vấn đề của tôi. Tôi đã vô hiệu hóa plugin nhưng nó hoạt động theo một trong hai cách.

.ngViewport.ng-scope{ 
    height: auto !important; 
    overflow-y: hidden; 
} 

.ngTopPanel.ng-scope, .ngHeaderContainer{ 
    width: auto !important; 
} 
.ngGrid{ 
    background-color: transparent!important; 
} 

Tôi hy vọng nó sẽ giúp ai đó!

0

Trong phương pháp mà bạn đang cố gắng lưu trữ dữ liệu cho mỗi hàng trong mỗi lưới, hãy thêm mã để tính chiều dài của lưới trong một mảng riêng biệt và đẩy độ dài được tính đó sang mảng khác. Đảm bảo chỉ mục của lưới và chỉ mục của mảng phải giống nhau, để chúng ta có thể truy cập vào nó từ giao diện người dùng cùng nhau.Cách tiếp cận của tôi là như sau:

    //Stores the calculated height of each grid. 
        $scope.gridHeights = []; 


        //Returns the height of the grid based on the 'id' passed from the UI side. 
        //Returns the calculated height appended with 'px' to the HTML/UI 
        $scope.getHeight = function(id){ 
         if($scope.gridHeights[id]) { 
          return { 
           'height': $scope.gridHeights[id] + 'px' 
          }; 
         }else{ 
          return { 
           'height': '300px' 
          }; 
         } 
        }; 


        for (var idx = 0; idx < $scope.auditData.length; idx++) { 

         //gets the rows for which audit trail/s has to be displayed based on single or multi order. 
         $scope.gridData[idx] = $scope.auditData[idx].omorderAuditTrailItems; 

         //Calculates and pushes the height for each grid in Audit Trail tab. 
         $scope.gridHeights.push(($scope.auditData[idx].omorderAuditTrailItems.length + 2)*30); 


     //IN HTML, set the height of grid as : 
     <div id='orderAuditTrailList'> 
      <div class="row fits-auditTrail" ng-style="getHeight(id)"> 
       <div class="fits-ng-grid" ng-if="auditTrail.omorderAuditTrailItems.length>0" ng-grid="gridOrderAuditTrailList[id]" ng-style="getHeight(id)"></div> 
      </div> 
    </div> 
1

Tôi nghĩ rằng tôi đã giải quyết vấn đề này một cách hoàn hảo sau 48 giờ,

var app = angular.module('app', ['ngTouch', 'ui.grid', 'ui.grid.pagination', 'ui.grid.autoResize']); 
 

 
app.controller('MainCtrl', ['$scope', '$http', '$interval', function($scope, $http, $interval) { 
 

 
    var paginationOptions = { 
 
    pageNumber: 1, 
 
    pageSize: 20, 
 
    }; 
 
    $scope.currentPage = 1; 
 
    $scope.pageSize = paginationOptions.pageSize; 
 
    $scope.gridOptions = { 
 
    rowHeight: 30, 
 
    enableSorting: true, 
 
    paginationPageSizes: [$scope.pageSize, $scope.pageSize * 2, $scope.pageSize * 3], 
 
    paginationPageSize: paginationOptions.pageSize, 
 
    columnDefs: [{ 
 
     name: 'name' 
 
    }, { 
 
     name: 'gender', 
 
     enableSorting: false 
 
    }, { 
 
     name: 'company', 
 
     enableSorting: false 
 
    }], 
 
    onRegisterApi: function(gridApi) { 
 
     $scope.gridApi = gridApi; 
 
     gridApi.pagination.on.paginationChanged($scope, function(newPage, pageSize) { 
 
     paginationOptions.pageNumber = newPage; 
 
     paginationOptions.pageSize = pageSize; 
 
     $scope.pageSize = pageSize; 
 
     $scope.currentPage = newPage; 
 
     $scope.totalPage = Math.ceil($scope.gridOptions.totalItems/$scope.pageSize); 
 
     }); 
 
    } 
 
    }; 
 

 
    var loadData = function() { 
 
    var url = 'https://cdn.rawgit.com/angular-ui/ui-grid.info/gh-pages/data/100.json'; 
 
    $http.get(url) 
 
     .success(function(data) { 
 
     $scope.gridOptions.totalItems = data.length; 
 
     $scope.totalPage = Math.ceil($scope.gridOptions.totalItems/$scope.pageSize); 
 
     $scope.gridOptions.data = data; 
 
     }); 
 
    }; 
 

 
    loadData(); 
 

 
    // for resize grid's height 
 
    $scope.tableHeight = 'height: 600px'; 
 

 
    function getTableHeight(totalPage, currentPage, pageSize, dataLen) { 
 
    var rowHeight = 30; // row height 
 
    var headerHeight = 50; // header height 
 
    var footerHeight = 60; // bottom scroll bar height 
 
    var totalH = 0; 
 
    if (totalPage > 1) { 
 
     // console.log('hehehehe'); 
 
     if (currentPage < totalPage) { 
 
     totalH = pageSize * rowHeight + headerHeight + footerHeight; 
 
     } else { 
 
     var lastPageSize = dataLen % pageSize; 
 
     // console.log(lastPageSize); 
 
     if (lastPageSize === 0) { 
 
      totalH = pageSize * rowHeight + headerHeight + footerHeight; 
 
     } else { 
 
      totalH = lastPageSize * rowHeight + headerHeight + footerHeight; 
 
     } 
 
     } 
 
     console.log(totalH); 
 
    } else { 
 
     totalH = dataLen * rowHeight + headerHeight + footerHeight; 
 
    } 
 
    return 'height: ' + (totalH) + 'px'; 
 
    } 
 

 
    $interval(function() { 
 
    $scope.tableHeight = getTableHeight($scope.totalPage, 
 
     $scope.currentPage, $scope.pageSize, 
 
     $scope.gridOptions.data.length); 
 
    console.log($scope.tableHeight); 
 
    $scope.gridApi.grid.handleWindowResize(); 
 
    $scope.gridApi.core.refresh(); 
 
    }, 200); 
 

 

 
}]);
.grid { 
 
    width: 600px; 
 
}
<!doctype html> 
 
<html ng-app="app"> 
 

 
<head> 
 
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.js"></script> 
 
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular-touch.js"></script> 
 
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular-animate.js"></script> 
 
    <script src="http://ui-grid.info/docs/grunt-scripts/csv.js"></script> 
 
    <script src="http://ui-grid.info/docs/grunt-scripts/pdfmake.js"></script> 
 
    <script src="http://ui-grid.info/docs/grunt-scripts/vfs_fonts.js"></script> 
 
    <script src="http://ui-grid.info/release/ui-grid.js"></script> 
 
    <link rel="stylesheet" href="http://ui-grid.info/release/ui-grid.css" type="text/css"> 
 
    <link rel="stylesheet" href="main.css" type="text/css"> 
 
</head> 
 

 
<body> 
 

 
    <div ng-controller="MainCtrl"> 
 
    <div ui-grid="gridOptions" ui-grid-pagination id="grid" style="{{tableHeight}}"></div> 
 
    <div> 
 
     <p>Current Page: {{ currentPage }}</p> 
 
     <p>Total Page: {{ totalPage }}</p> 
 
     <p>Page Size: {{ pageSize }}</p> 
 
     <p>Table Height: {{tableHeight}}</p> 
 
    </div> 
 
    </div> 
 

 

 
    <script src="app.js"></script> 
 
</body> 
 

 
</html>

Xem thêm Plunker: http://plnkr.co/edit/np6y6rgN1ThnT0ZqyJeo?p=preview

Các vấn đề liên quan