AngularJS的ng-repeat过滤器基于select选项
AngularJS ng-repeat filter based on select option
我是AngularJS新手。我试图过滤基于选择框中选择的选项显示的数据集。
<div ng-controller="CurrentTrandetailsController">
<div>
<div class="pull-right">
<label for="show-filter" class="show-label">Show </label>
<select name="show-filter" ng-model="searchText.accruedcard" id="show-filter" ng-options="trandetail.accruedcard as trandetail.accruedcard for trandetail in currentTrandetails.trandetails ">
<option value="">All</option>
</select>
</div>
<h3>Current trandetails</h3>
</div>
<div>
<table class="table table-striped table-hover">
<tbody>
<tr ng-repeat="trandetail in currentTrandetails.trandetails | filter:searchText">
<td>{{trandetail.dateAccrued}}</td>
<td>{{trandetail.accruedcard}}</td>
<td>{{trandetail.placeAccrued}}</td>
<td>{{trandetail.discountcents}}</td>
<td>{{trandetail.shortExpiryDate}}</td>
</tr>
</tbody>
</table>
</div>
</div>
我使用了http://docs.angularjs.org/api/ng.filter:filter中给出的示例,该示例使用输入框进行过滤。在选择给定的卡片时,它似乎过滤得很好。但是,当我选择"All"时,它的值设置为",它不会显示所有条目(清除过滤器)。但是,在所示示例中,清空文本框后,将显示所有条目。
我做错了什么?
你需要改变你的选择为:
<select name="show-filter" ng-model="searchText" ...
代替
<select name="show-filter" ng-model="searchText.accruedcard" ...
解释:据我所见,硬编码选项与ng-options一起使用是不常见的,这是导致问题的原因。过滤器使用select的模型,它目前是一个对象,而不是像Angular的例子中那样是一个字符串。对象模式是可以的,但是在这种情况下,当All被选中时,对象的属性变成了null
,因为它没有像其他选项一样连接到选择中。
这就是导致searchText
过滤器失败的原因,因为它期望有效的字符串(即使在使用对象进行匹配模式时)。
通过为select的模型使用字符串原语,All 'hack'被保留,因为它导致select的模型变成(''
)而不是null,这将匹配所有内容并显示所有结果。
我遇到了同样的问题。我修复它的方法是在滤镜中使用.toString()
。
相关文章:
- 根据select选项元素将表单重定向到不同的URL
- jQuery动态表单显示在select选项上
- 如何在触发事件后选择select选项
- jQuery:根据select选项中的每页项目进行分页
- 使用JavaScript删除多个select选项
- 使用select选项转换时间.使用javascript
- 使用angularjs解析JSON,并选择select选项
- 使用jQuery在select选项上设置HTML数据属性
- 如何防止onclick事件在select选项中触发
- 如何在angular js中的select选项中获取所选项目id
- ng-select 选项在与自定义指令一起使用时加倍
- jQuery未能将JSON数据附加为select选项
- 如何将html中select选项的输入与javascript一起使用
- 使用VueJs获取Select选项文本
- 从jquery中的select选项添加两个不同的数组值(text&value)
- select选项在angularjs中不显示
- jQuery v 1.1x更改select选项的值
- 将select选项添加到id中
- 基于select选项的javascript动态表单生成
- Select选项包含在验证模型MVC时不起作用