ng-grid expand and collaspe row
Asked Answered
S

2

7

I am trying to achieve an expand and collapse row for ng-grid, basically what we have here http://datatables.net/examples/api/row_details.html if you click the "plus icon" your displayed with more detail.

I have seen a similar question asked here but no solution. https://github.com/angular-ui/ng-grid/issues/517

Does anyone know how to achieve this?

Any help is appreciated.

var app = angularexpand('myApp', ['ngGrid']);
app.controller('MyCtrl', function($scope) {
  $scope.myData = [{
    somedata: "data to be expanded in another",
    moredata: 1
  }, {
    somedata: "b data to be expanded in another",
    moredata 2
  }, {
    somedata: "c data to be expanded in another",
    moredata: 3
  }, {
    somedata: "d data to be expanded in another",
    moredata: 4
  }, {
    somedata: "e data to be expanded in another",
    moredata: 5
  }];
  $scope.gridOptions = {
    data: 'myData'
  };

  $scope.expandRow = function(e) {
    e.preventDefault();
    var getData = {
      somedata: '',
      moredata: ''
    };
    $scope.gridOptions.selectItem(index, false);
    $scope.myData.splice(index + 1, 0, getData);
    $(this.row.elm).append('<div>' + this.row.entity.somedata + '</div>');
  };
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-controller="MyCtrl">
  <div class="gridStyle" ng-grid="gridOptions"></div>
  <button ng-click="expandRow($event)">add row</button>
</body>
Softhearted answered 12/5, 2014 at 19:13 Comment(4)
I have edited my post, I basically tried to append a div over a dummy row, not exactly angular way and since am using append it works sometimes and sometimes it causes peculiar behaviourSofthearted
a working fiddle would be great! here is an empty one with angularjs loaded: jsfiddle.net/ddNp9Almucantar
Seems ng-grid will have a major revamp for 3.0, there include things like hide/show out of the box, for now I just use datatables that has all these features already included. Alp thanks for all your assistance.Softhearted
@Softhearted If you have self-answered the question, don't hesitate to post and accept it. See stackoverflow.com/help/self-answerSabulous
T
2

I have never done this with ng-grid, however, have achieved this functionality with a regular table. You could write a custom directive to solve this.

Something along the lines below should do the trick. This example was NOT tested in JSFiddle. The key here is using 'ng-repeat-start' and 'ng-repeat-end' instead of just 'ng-repeat'.

HTML:

<div ng-controller="MyCtrl">
    <table class="table dataTable no-hover">
	<tbody>	
	    <tr ng-repeat-start="row in rows" ng-init="ind = $index"> 
		<td ng-click="rowExpanded[row.name]=!rowExpanded[row.name]"></td>
                <td ng-repeat="val in row.vals" class="column"> </td>
            </tr>
            <tr id="rowexpand" ng-show="rowExpanded[row.name]" colspan="100%" ng-repeat-end></tr>
	</tbody>
    </table>
</div>

AngularJS Controller:

var myApp = angular.module('myApp',[]);

function MyCtrl($scope) {
    $scope.rowExpanded=[];
    
    $scope.rows= [
        { "name": "row1",
         "vals" :[1, 2, 3, 4, 5], 
        },
         { "name": "row1",
         "vals" :[1, 2, 3, 4, 5], 
        }
    ]
        
}
Tibbetts answered 21/3, 2015 at 0:14 Comment(0)
A
-1

Currently nggrid doesn't support this feature [issue #1111][1] and its being implemented in UIGrid 3.0 version.

But i have found a workaround and this is how you can try to achieve in nggrid using rowTemplate and a little bit of jquery. hope this helps!

rowTemplate:

"< div class="**row**" >"

  "add column data"

  // expand or collapse image

    < div class=\"col-xs-1 col-md-1\" >< img "id='**showHide**_{{row.rowIndex}}' ng-src='src/img/{{**imgArrowRight**}}' ng-click=\"**rowExpand** (row.rowIndex);\" />< / div>"< /div>< div id='**expRowId**_{{row.rowIndex}}' style=\"display:none;\">< div class=\"**expData**\">< span ng-bind=\"**row.entity.Attr**\">< /span>
//add whatever you want in the expanded row.< /div>< /div>

//Defined customArray for handling dynamic row Toggle

 angular.forEach($scope.**gridData**, function(row,index) {
                $scope.customArray[index] = true;
 });

//row expand code

$scope.**rowExpand** = function(**index**){
                        $scope.customArray[index] = $scope.customArray[index] === false ? true : false;

                        if($scope.customArray[index]){
                            $('#showHide_'+index).attr('src','src/assets/img/rigthIcon.gif');
                            $('#expRowId_'+index).hide();
                        }else{
                            $('#showHide_'+index).attr('src','src/assets/img/downIcon.gif');
                            $('#expRowId_'+index).show();
                        }
                    }

Also, override the ngRow style class

.ngRow {
    position: relative !important;
}
Analyst answered 29/4, 2015 at 17:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.