Jez Jez - 5 months ago 26
AngularJS Question

Animate expanding/contracting table

right now, the code below shows a table with rows which expand/contract when clicked. I want each row to animate its contents sliding down and up when clicked. How do I do this?



function MyCtrl($scope) {
$scope.environment_service_packages =
[
{name: 'Click to expand', info: {text: 'some extra info', show: false}},
{name: 'obj2', info: {text: 'some extra info for obj2', show: false}},
];
}

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<body ng-app>
<table ng-controller="MyCtrl" class="table table-hover table-striped">
<tr class="info">
<td>...</td>
</tr>
<tbody ng-repeat="x in environment_service_packages">
<tr ng-click="x.info.show = !x.info.show">
<td> {{ x.name }}
</tr>
<tr ng-show="x.info.show">
<td>
{{ x.info.text }}
</td>
</tr>
</tbody>
</table>
</body>




Answer

Try this. I just added a fadeIn. Try some other effects and see it that works.
Courtesy this awesome library (animate.css)[https://daneden.github.io/animate.css/]

function MyCtrl($scope) {
  $scope.environment_service_packages = [{
    name: 'Click to expand',
    info: {
      text: 'some extra info',
      show: false
    }
  }, {
    name: 'obj2',
    info: {
      text: 'some extra info for obj2',
      show: false
    }
  }, ];
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.5.2/animate.min.css" rel="stylesheet" />

<body ng-app>
  <table ng-controller="MyCtrl" class="table table-hover table-striped">
    <tr class="info">
      <td>...</td>
    </tr>
    <tbody ng-repeat="x in environment_service_packages">
      <tr ng-click="x.info.show = !x.info.show">
        <td>{{ x.name }}
      </tr>
      <tr ng-show="x.info.show" class="animated fadeIn">
        <td>
          {{ x.info.text }}
        </td>
      </tr>
    </tbody>
  </table>
</body>