如何根据子项将子值下的兄弟值与父项对齐?

时间:2015-11-15 15:34:06

标签: html angularjs

NaN
var app = angular.module('app', ['ngAnimate', 'ngTouch', 'ui.grid', 'ui.grid.treeView' ]);

app.controller('MainCtrl', ['$scope', '$http', '$interval', 'uiGridTreeViewConstants','uiGridTreeBaseService',function ($scope, $http, $interval, uiGridTreeViewConstants,uiGridTreeBaseService ) {
 
  
  $scope.gridOptions = {
    enableSorting: false,
    enableFiltering: false,
    showTreeExpandNoChildren: true,
    showTreeRowHeader: false,
	enableColumnMenus : false,
	rowHeight: 50,
	enableHorizontalScrollbar: 0,
    enableVerticalScrollbar: 0,
    columnDefs: [
      { name: 'name', width: '30%' , cellTemplate : "<div class=\"ui-grid-cell-contents\" title=\"TOOLTIP\"><div style=\"float:left;\" class=\"ui-grid-tree-base-row-header-buttons\" ng-class=\"{'ui-grid-tree-base-header': row.treeLevel > -1 }\" ng-click=\"grid.appScope.toggleRow(row,evt)\"><i ng-class=\"{'ui-grid-icon-minus-squared': ( ( grid.options.showTreeExpandNoChildren && row.treeLevel > -1 ) || ( row.treeNode.children && row.treeNode.children.length > 0 ) ) && row.treeNode.state === 'expanded', 'ui-grid-icon-plus-squared': ( ( grid.options.showTreeExpandNoChildren && row.treeLevel > -1 ) || ( row.treeNode.children && row.treeNode.children.length > 0 ) ) && row.treeNode.state === 'collapsed'}\" ng-style=\"{'padding-left': grid.options.treeIndent * row.treeLevel + 'px'}\"></i>&nbsp;</div>{{COL_FIELD CUSTOM_FILTERS}}</div>" },
      { name: 'gender', width: '20%' },
      { name: 'age', width: '20%' },
      { name: 'company', width: '25%' },
      { name: 'state', width: '35%' },
      { name: 'balance', width: '25%', cellFilter: 'currency' }
    ],
    onRegisterApi: function( gridApi ) {
      $scope.gridApi = gridApi;
      $scope.gridApi.treeBase.on.rowExpanded($scope, function(row) {
        if( row.entity.$$hashKey === $scope.gridOptions.data[50].$$hashKey && !$scope.nodeLoaded ) {
          $interval(function() {
            $scope.gridOptions.data.splice(51,0,
              {name: 'Dynamic 1', gender: 'female', age: 53, company: 'Griddable grids', balance: 38000, $$treeLevel: 1},
              {name: 'Dynamic 2', gender: 'male', age: 18, company: 'Griddable grids', balance: 29000, $$treeLevel: 1}
            );
            $scope.nodeLoaded = true;
          }, 2000, 1);
        }
      });
    }
  };

 $http.get('https://cdn.rawgit.com/angular-ui/ui-grid.info/gh-pages/data/500_complex.json')
 .success(function(data) {
   for ( var i = 0; i < data.length; i++ ){
     data[i].state = data[i].address.state;
     data[i].balance = Number( data[i].balance.slice(1).replace(/,/,'') );
   }
   data[0].$$treeLevel = 0;
   data[1].$$treeLevel = 1;
   data[10].$$treeLevel = 1;
   data[11].$$treeLevel = 1;
   data[20].$$treeLevel = 0;
   data[25].$$treeLevel = 1;
   data[50].$$treeLevel = 0;
   data[51].$$treeLevel = 0;
   $scope.gridOptions.data = data;
 });

  $scope.expandAll = function(){
    $scope.gridApi.treeBase.expandAllRows();
  };
  
  $scope.collapseAll = function(){
    $scope.gridApi.treeBase.collapseAllRows();
  };
  
  $scope.toggleRow = function( row,evt ){
    uiGridTreeBaseService.toggleRowTreeState($scope.gridApi.grid, row, evt);
    //$scope.gridApi.treeBase.toggleRowTreeState($scope.gridApi.grid.renderContainers.body.visibleRowCache[rowNum]);
  };

  $scope.toggleExpandNoChildren = function(){
    $scope.gridOptions.showTreeExpandNoChildren = !$scope.gridOptions.showTreeExpandNoChildren;
    $scope.gridApi.grid.refresh();
  };
}]);
.grid {
  width: 400px;
}

Remove extra column of icons from Angular UI-Grid TreeView

根据上面提供的解决方案,URL工作正常但在网格表中如何将子值下的兄弟值与每个子节点对齐到父节点?

enter image description here

<!doctype html>
<html ng-app="app">
  <head>
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.16/angular.js"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.16/angular-touch.js"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.16/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-unstable.js"></script>
    <link rel="stylesheet" href="http://ui-grid.info/release/ui-grid-unstable.css" type="text/css">
    <link rel="stylesheet" href="main.css" type="text/css">
  </head>
  <body>

<div ng-controller="MainCtrl">
  <button id="expandAll" type="button" class="btn btn-success" ng-click="expandAll()">Expand All</button>
  <button id="collapseAll" type="button" class="btn btn-collapse" ng-click="collapseAll()">collapseAll</button>
  <button id="toggleFirstRow" type="button" class="btn btn-success" ng-click="toggleRow(0)">Toggle First Row</button>
  <button id="toggleSecondRow" type="button" class="btn btn-success" ng-click="toggleRow(1)">Toggle Second Row</button>
  <button id="toggleExpandNoChildren" type="button" class="btn btn-success" ng-click="toggleExpandNoChildren()">Toggle Expand No Children</button>
  <br/>
  <br/>
  <div id="grid1" ui-grid="gridOptions" ui-grid-tree-view class="grid"></div>
</div>
    <script src="app.js"></script>
  </body>
</html>
var app = angular.module('app', ['ngAnimate', 'ngTouch', 'ui.grid', 'ui.grid.treeView']);

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


    $scope.gridOptions = {
      enableSorting: true,
      enableFiltering: true,
      showTreeExpandNoChildren: true,
      showTreeRowHeader: false,
      columnDefs: [{
        name: 'name',
        width: '30%',
        cellTemplate: "<div class=\"ui-grid-cell-contents\" title=\"TOOLTIP\"><div style=\"float:left;\" class=\"ui-grid-tree-base-row-header-buttons\" ng-class=\"{'ui-grid-tree-base-header': row.treeLevel > -1 }\" ng-click=\"grid.appScope.toggleRow(row,evt)\"><i ng-class=\"{'ui-grid-icon-minus-squared': ( ( grid.options.showTreeExpandNoChildren && row.treeLevel > -1 ) || ( row.treeNode.children && row.treeNode.children.length > 0 ) ) && row.treeNode.state === 'expanded', 'ui-grid-icon-plus-squared': ( ( grid.options.showTreeExpandNoChildren && row.treeLevel > -1 ) || ( row.treeNode.children && row.treeNode.children.length > 0 ) ) && row.treeNode.state === 'collapsed'}\" ng-style=\"{'padding-left': grid.options.treeIndent * row.treeLevel + 'px'}\"></i> &nbsp;</div>{{COL_FIELD CUSTOM_FILTERS}}</div>"
      }, {
        name: 'gender',
        width: '20%'
      }, {
        name: 'age',
        width: '20%'
      }, {
        name: 'company',
        width: '25%'
      }, {
        name: 'state',
        width: '35%'
      }, {
        name: 'balance',
        width: '25%',
        cellFilter: 'currency'
      }],
      onRegisterApi: function(gridApi) {
        $scope.gridApi = gridApi;
        $scope.gridApi.treeBase.on.rowExpanded($scope, function(row) {
          if (row.entity.$$hashKey === $scope.gridOptions.data[50].$$hashKey && !$scope.nodeLoaded) {
            $interval(function() {
              $scope.gridOptions.data.splice(51, 0, {
                name: 'Dynamic 1',
                gender: 'female',
                age: 53,
                company: 'Griddable grids',
                balance: 38000,
                $$treeLevel: 1
              }, {
                name: 'Dynamic 2',
                gender: 'male',
                age: 18,
                company: 'Griddable grids',
                balance: 29000,
                $$treeLevel: 1
              });
              $scope.nodeLoaded = true;
            }, 2000, 1);
          }
        });
      }
    };

    $http.get('https://cdn.rawgit.com/angular-ui/ui-grid.info/gh-pages/data/500_complex.json')
      .success(function(data) {
        for (var i = 0; i < data.length; i++) {
          data[i].state = data[i].address.state;
          data[i].balance = Number(data[i].balance.slice(1).replace(/,/, ''));
        }
        data[0].$$treeLevel = 0;
        data[1].$$treeLevel = 1;
        data[10].$$treeLevel = 1;
        data[11].$$treeLevel = 1;
        data[20].$$treeLevel = 0;
        data[25].$$treeLevel = 1;
        data[50].$$treeLevel = 0;
        data[51].$$treeLevel = 0;
        $scope.gridOptions.data = data;
      });

    $scope.expandAll = function() {
      $scope.gridApi.treeBase.expandAllRows();
    };

    $scope.toggleRow = function(row, evt) {
      uiGridTreeBaseService.toggleRowTreeState($scope.gridApi.grid, row, evt);
      //$scope.gridApi.treeBase.toggleRowTreeState($scope.gridApi.grid.renderContainers.body.visibleRowCache[rowNum]);
    };

    $scope.toggleExpandNoChildren = function() {
      $scope.gridOptions.showTreeExpandNoChildren = !$scope.gridOptions.showTreeExpandNoChildren;
      $scope.gridApi.grid.refresh();
    };
  }
]);
.grid {
  width: 500px;
  height: 400px;
}

Plunker Demo

1 个答案:

答案 0 :(得分:0)

更改:

ng-style="{'padding-left': grid.options.treeIndent * row.treeLevel + 'px'}

到:

ng-style="{'padding-left': (grid.options.treeIndent+10) * row.treeLevel + 'px'}

Result