2015-05-07 55 views
0

如何使用$ event僅使用jqLit​​e查找父代tr元素?通過ng-click事件的標記找到第一個父親

這裏是Click事件處理程序/問題的例子:

<td ng-click="showDataClick($event)"><i></i></td> 

這裏是我的模板:

<tbody> 
    <tr ng-repeat="record in filteredData = (state.filteredRecords | orderBy : state.sort.column : state.sort.descending | pagination : state.settings.currentPage : state.settings.recordsPerPage)" class="table-row" audit-row record="record"> 
    </tr> 
    <tr ng-if="filteredData.length === 0" class="table-row"> 
     <td colspan="5" class="text-center">No records found</td> 
    </tr> 
</tbody> 
+1

這是否TR使用'NG-repeat'呈現? –

+0

總體目標是什麼?爲什麼不''.parent()'? – tymeJV

+0

@tymeJV他想要tr在它上面not..parent會給'tr'的tr元素.. –

回答

0

這裏閱讀你的整個問題後,後插入的行的工作演示單擊的行jsfiddle

HTML

<div ng-controller="MyCtrl"> 
    <table> 
    <tr> 
     <td ng-click="showDataClick($event)"><i>Nested Element</i> no nesting element  </td> 
     <td ng-click="showDataClick($event)"> 
      Multiple td's 
     </td> 
     <td ng-click="showDataClick($event)"> 
      <div> 
       <div> 
        nested in multiple elements 
       </div> 
      </div> 
     </td>    
    </tr> 
    <tr> 
     <td ng-click="showDataClick($event)"><i>row 1</i> 
     </td> 
    </tr> 
    <tr> 
     <td ng-click="showDataClick($event)"><i>row 2</i> 
     </td> 
    </tr> 
</table> 
</div> 

JS

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

function MyCtrl($scope) { 
    $scope.name = 'Superhero'; 
    $scope.showDataClick = function (e) { 
     var newRow = document.createElement('tr'); 
     newRow.innerHTML = '<td>Hey There I am new</td>'; 
     var sibling = e.currentTarget.parentNode; 
     var next = sibling.nextSibling; 
     var parent = sibling.parentElement; 
     parent.insertBefore(newRow, next); 
    } 
} 
+0

我認爲這會失敗,在我的使用案例中,圖標只填滿了很多'td'。這意味着有時候我需要將2個元素添加到'tr'標籤,有時候如果我點擊僅僅是'td' 1元素... – Timigen

+0

這只是爲了展示事件鏈上的例子。如果你真的想獲得tr,爲什麼不把clickhandler移動到tr元素,那麼你就會知道srcElement總是行。你有沒有想要這樣做的具體原因? – joverall22

+0

哦,我認爲這是一個答案...是的,這是'ng-click'的原因。不是那樣就能解決問題......正如我上面所說的那樣 – Timigen