ng-grid expand and collaspe row

旧街凉风 提交于 2019-12-01 04:13:59

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], 
        }
    ]
        
}
Johny

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;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!