• [转]ng-grid Auto / Dynamic Height


    本文转自:https://stackoverflow.com/questions/23396398/ng-grid-auto-dynamic-height

    I think I solved this problem perfectly after 48 hours,

    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 {
       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>

    Also see Plunker: http://plnkr.co/edit/np6y6rgN1ThnT0ZqyJeo?p=preview

    其他类似尝试的参考地址:

    https://stackoverflow.com/questions/27837335/angular-ui-grid-dynamically-calculate-height-of-the-grid

    https://stackoverflow.com/questions/28678582/angularjs-ui-grid-dynamic-expandablerowheight

  • 相关阅读:
    实验三 进程调度模拟程序
    实验二作业调度模拟程序
    最新广商小助手 项目进展 OpenGL ES 3D在我项目中引用 代码太多只好选重要部分出来
    最后冲刺 我的项目 广商小助手
    最新一课 老师指点用Listview适配器
    安卓小学生四则运算
    大三上学期安卓一边学一边开始做一个自己觉得可以的项目 广商小助手App 加油
    我要再接再力 学更多
    用场景来规划测试工作
    阅读第13,14,15,16,17章
  • 原文地址:https://www.cnblogs.com/freeliver54/p/7771890.html
Copyright © 2020-2023  润新知