2014-09-30 50 views
2

我已经在angularjs中编写了一个指令。我正在使用下面的代码..

$('#TableID tr:has(td)') 

但我不想使用#TableID即表ID。出于这个原因,我该如何使用$(this)或这个或任何想法而不是上面的代码。 下面

directives.directive('niGroup', ['$timeout', function ($timeout) { 
     return { 
      restrict: 'A', 
      scope: false, 
      link: function (scope, element, attrs) { 
       function groupTable($rows, startIndex, total) { 
        if (total === 0) { 
         return; 
        } 
        var index, currentIndex = startIndex, count = 1, lst = []; 
        var tds = $rows.find('td:eq(' + currentIndex + ')'); 
        var ctrl = $(tds[0]); 
        //var $this = $(this); 
        //var ctrl = $this.text(); 
        lst.push($rows[0]); 
        for (index = 1; index <= tds.length; index++) { 
         var t1 = ctrl.text(); 
         var t2 = $(tds[index]).text(); 
         if (ctrl.text() == $(tds[index]).text()) { 
          count++; 
          $(tds[index]).css("display", "none");//hide(); //addClass('deleted'); 
          lst.push($rows[index]); 
         } 
         else { 
          if (count > 1) { 
           ctrl.attr('rowspan', count); 
           groupTable($(lst), startIndex + 1, total - 1); 
          } 
          count = 1; 
          lst = []; 
          ctrl = $(tds[index]); 
          lst.push($rows[index]); 
         } 
        } 
       } 
       scope.niGroups = scope.$eval(attrs.niGroup); 
       //get ListName 
       scope.listName = scope.niGroups.gridData; 
       scope.from = 0; 
       scope.groupLength = 1; 
       if (scope.niGroups.groupBy != undefined && scope.niGroups.groupBy.length > 1) { 
        scope.from = parseInt(scope.niGroups.groupBy.split(',')[0]); 
        scope.groupLength = parseInt(scope.niGroups.groupBy.split(',')[1]); 
       } 
       scope.$watchCollection(
        scope.listName, 
        function (newValue, oldValue) { 
         if (newValue !== oldValue) { 
          $timeout(function() { 
           groupTable($('tr:has(td)'), scope.from, scope.groupLength); 
          }, 0); 
         } 
        } 
       ); 
      } 
     }; 
    }]); 
+0

发布您的代码 – 2014-09-30 10:21:26

回答

0

使用我的整个指令给出的$事件获得事件对象,从中可以得到 事件目标,并与jQuery使用它。

看一看here

+0

但我没有事件或$事件。 – 2014-09-30 11:35:26