javascript - Cant display row in an HTML table using angular ng-repeat and jquery -
i have html table expands or collapse depending if user clicks on row, main record "parent" row, if click you'll see child row displays records, issue add third row should display default because child hidden until click on parent third row wont appear , cant figure out problem =(
here's example in fiddle , below youll see directive.
angular .module('app',[]) .controller('datactrl',datactrl) .directive('drilldown',drilldown); function datactrl($scope) { $scope.category = [ { "desc": "category 1", "lw$": "45", "lw": "-4%", "l4w": "-15.7%", "l13w": "24%", "l52w": "-6%" } ] $scope.subcat = [ { "desc": "sub category 1", "lw$": "45", "lw": "-4%", "l4w": "-15.7%", "l13w": "24%", "l52w": "-9%" }, { "desc": "sub category 2", "lw$": "15", "lw": "4.2%", "l4w": "1.7%", "l13w": "-2.4%", "l52w": "-65%" }, { "desc": "sub category 3", "lw$": "767", "lw": "4.2%", "l4w": "9.7%", "l13w": "-2.4%", "l52w": "-21%" }, { "desc": "sub category 4", "lw$": "21", "lw": "14.2%", "l4w": "1.7%", "l13w": "-42.4%", "l52w": "-34%" } ]; } function drilldown() { var directive = { restrict: 'a', link: link }; return directive; function link(scope,element) { var table = $('.categories-table'); table.each(function() { var $table = $(this); $table.find('.parent').each(function(){ if($(this).nextuntil('.parent', ".child").length > 0){ $(this).children('td:first').html('+'); } }); $table.find('.child').each(function(){ if($(this).nextuntil('.child', ".grandson").length > 0){ $(this).children('td:first').html('+'); } }); var $childrows = $table.find('tbody tr').not('.parent').hide(); $table.find('button.hide').click(function() { $childrows.hide(); }); }); element.on('click',function(){ if($(this).parent().hasclass('parent') == true) { console.log("----parent"); if ($(this).text() == "+") $(this).text("-") else $(this).text("+"); $(this).parent().nextuntil('.parent', ".child").fadetoggle("slow", "linear"); $(this).parent().nextuntil('.parent', ".grandson").hide("fast"); $(this).parent().nextuntil('.parent', ".child").each(function(){ if($(this).children('td:first').text() == '-') $(this).children('td:first').text('+'); }); } else if($(this).parent().hasclass('child') == true) { console.log("----child"); if ($(this).text() == "+") $(this).text("-") else $(this).text("+"); $(this).parent().nextuntil('.child', ".grandson").fadetoggle("slow", "linear"); } }); } }
first of recommend read topic: "thinking in angularjs" if have jquery background?
obviously using jquery+angular in wrong way. , there logic error in jquery code.
if want solve problem in angular-way:
one possible solution use scope variables or object variables , ng-show directive.
something this: https://jsfiddle.net/p7ew017u/4/
<td ng-class="expand" ng-click="d.expanded = !d.expanded">+</td>
and 1 more thing: tables not displaying tree structures (like categories, subcategories, subsubcategories, etc). better use lists (ul, ol)