我在使用 AngularJS 的标签过滤 JSON 数据时遇到了一些麻烦。我在这里尝试了 ExpertSystem 稍微修改的过滤器(使用标签应用动态过滤器):
groupifyApp.filter('filterByTags', function () {
return function (items, tags) {
var filtered = [];
if (tags.length == 0) {
filtered = items;
}
(items || []).forEach(function (item) {
var matches = tags.some(function (tag) {
return (item.data1.indexOf(tag.text) > -1) || (item.data2.indexOf(tag.text) > -1);
});
if (matches) {
filtered.push(item);
}
});
return filtered;
};
});
在我的 HTML 中:
<tags-input ng-model="searchTags" type="text" placeholder="Search tags">
<auto-complete source="allTags()"></auto-complete>
</tags-input>
Model: {{searchTags}}
<br />
<table class="table table-hover">
<thead>
<th>Group name</th>
<th>Tags</th>
<th>Type</th>
<th>Looking for</th>
<th class="col-sm-1"> </th>
</thead>
<tr ng-repeat="match in allGroups | filterByTags:searchTags">
<td>{{match.name}}</td>
<td>
<ul>
<li ng-repeat="tag in match.tags">{{tag}}</li>
</ul>
</td>
<td>{{match.type}}</td>
<td>
<ul>
<li ng-repeat="deficit in match.deficits">{{deficit}}</li>
</ul>
</td>
<td>
<button class="btn btn-sm btn-default"><span class="glyphicon glyphicon-hand-left"></span></button>
</td>
</tr>
</table>
JSON 看起来像这样:
[
{name: "Elite", type: "Learning", tags: ["RWTH", "Aachen"], competences: ["Complexity"], deficits: ["Java"], members: ["Niklas", "Marv"]},
{name: "Old Boys", type: "Assignments", tags: ["FH", "Aachen"], competences: ["Java"], deficits: ["AAT"], members: ["Joel", "Joel's Sister"]}
];
不幸的是,该表没有被过滤。它总是打印出整个表格(即使没有我的修改)。我想我必须定义要过滤哪一列,但我对 AngularJS 或 JavaScript 一点都不熟悉。那么有什么建议吗?