简体   繁体   English

使用angularJS获取表中的行索引

[英]Get index of row in table using angularJS

I want to get index of each row in this 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>

I want to have a variable that contains the index of each row not to just display it 我想要一个包含每行索引的变量,而不是只显示它

you can use $index 你可以使用$ index

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

I think you should be using the <tr> tag to place your ng-repeat directive. 我认为您应该使用<tr>标签来放置ng-repeat指令。 You can use ng-repeat's $index property. 您可以使用ng-repeat's $index属性。 See ng-repeat 's documentation . 请参阅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>

$index works fine when the ng-repeat is not filtered. 当未过滤ng-repeat时,$ index工作正常。 If you use a filter, it might be better to use indexOf(). 如果使用过滤器,最好使用indexOf()。 Example: 例:

<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>

If you want to use your own variable as index you can use this: 如果要将自己的变量用作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