[英]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.