簡體   English   中英

使用angularJS獲取表中的行索引

[英]Get index of row in table using angularJS

我想使用angularJS獲取此表中每行的索引

<table>
<tbody>
  <tr ng-repeat = "cust in costomers">
     <td> cust.name </td>
     <td> cust.phone </td>
     <td> cust.address </td>
  </tr>
</tbody>

我想要一個包含每行索引的變量,而不是只顯示它

你可以使用$ index

   <tr ng-repeat = "cust in costomers">
     <td> {{$index}} </td>
  </tr>

我認為您應該使用<tr>標簽來放置ng-repeat指令。 您可以使用ng-repeat's $index屬性。 請參閱ng-repeat文檔

<table>
  <tbody>
    <tr ng-repeat = "cust in costomers">
      <td> {{$index}} </td>
      <td> {{cust.name}} </td>
      <td> {{cust.phone}} </td>
      <td> {{cust.address}} </td>
    </tr>
  </tbody>
</table>

當未過濾ng-repeat時,$ index工作正常。 如果使用過濾器,最好使用indexOf()。 例:

<input type="text" ng-model="filterCustomers" placeholder="search...">
<table>
  <tbody>
    <tr ng-repeat = "cust in costomers | filter:filterCustomers">
      <td> {{costomers.indexOf(cust) + 1}} </td>
    </tr>
  </tbody>
</table>

如果要將自己的變量用作index ,可以使用:

<table>
<tbody>
  <tr ng-repeat = "(index, cust) in costomers">
     <td> {{index}} </td>
     <td> cust.name </td>
     <td> cust.phone </td>
     <td> cust.address </td>
  </tr>
</tbody>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM