Javascript 如何使用对象(而不是数组)将orderBy和filter与ng repeat一起使用?
我有这样的数据: 我有一个输入/ng重复:Javascript 如何使用对象(而不是数组)将orderBy和filter与ng repeat一起使用?,javascript,angularjs,angularjs-ng-repeat,Javascript,Angularjs,Angularjs Ng Repeat,我有这样的数据: 我有一个输入/ng重复: <input ng-model="query" placeholder="Suche . . ."> <ul> <li ng-repeat="p in persons | orderBy:'name_de' | filter:query"> Some output here . . . </li> </ul> 这里有一些输出 现在的问题是,我如何才能订购和过滤这些内容 它与一
<input ng-model="query" placeholder="Suche . . .">
<ul>
<li ng-repeat="p in persons | orderBy:'name_de' | filter:query"> Some output here . . . </li>
</ul>
这里有一些输出李>
现在的问题是,我如何才能订购和过滤这些内容
它与一系列人一起工作,但我需要“ID”,所以对象是必需的
我也在寻找一种方法来过滤对象的N-属性
e、 g表示name_de和name_en(因此,如果我搜索Andre和Andrew,它将显示ID123),但忽略“description”文本(首先,我遇到了过滤器检查所有属性的问题)您可以使用toArrayfilter模块更多信息请参见此处: 还有您的解决方案的示例演示 JS: html:
body ng app=“app”>
- {{p.name_de}}->{p.name_en}
回答您的问题
它与一系列人一起工作,但我需要“ID”,以便
对象是必需的
您可以将ID作为person对象的属性,使其看起来像这样吗
$scope.persons = [
{ id: "ID123",
name_de: "Andre",
name_en: "Anrew",
age: 30,
description: "He is the father of xyz and . . .",
. . .
},
{ id: "IDabc",
name_de: "Markus",
name_en: "Mark",
age: 20,
},
. . .
];
要按name_de和name_en(或任何其他相关属性)进行搜索,可以尝试编写自定义筛选器。这相当容易
var app = angular.module("YourApp");
app.filter('MyCustomFilter', function(){
return function(objects, criteria){
if(!criteria)
return objects;
var filterResult = new Array();
for(var index in objects)
if(objects[index].name_de.indexOf(criteria) != -1 || objects[index].name_en.indexOf(criteria) != -1)
filterResult.push(objects[index]);
return filterResult;
}
});
您的HTML将如下所示:
<input type="text" data-ng-model="personFilter" />
<div data-ng-repeat="person in persons | MyCustomFilter:personFilter"> ... </div>
...
无论是orderBy
还是filter
过滤器都不支持开箱即用的对象源。在将其传递给筛选器之前,您必须编写一个自定义筛选器或将其转换为数组(将每个ID存储为另一个属性)。请尝试:筛选器:{name_de:query,name_en:query}这不起作用Rob:(不管怎样,如果我将其转换为数组,我如何处理ID?例如,我与persons.idxyz链接到其他人(父亲、母亲和其他关系)(.xyz)然后请包含更多您尝试过的代码,例如关系如何存储和查询。到目前为止,数据还不完整,我只是开始。我想使用的一件事是,例如,检查父亲是否也有父亲属性=然后是祖父(我不必自己将祖父添加为属性)我的ID由前4个字母+姓名长度+0/1(男/女)+A/B/C组成…如果ID不是唯一的(如果我想使用来自1个人的数据,这很有用,因为我知道姓名和其他任何东西(只是在极少数情况下,我必须检查我想要的A/B/C…),我可以使用persons[索引]当然,但这看起来不是很“清晰”:我会看最后一件事…如果我搜索“父亲”,它仍然会显示Andrew..因为过滤器检查所有属性。我如何“忽略”一些属性?@easyX ie:如果你只想搜索名称使用:过滤器:{name\u de':query}
如果我想搜索name_de和name_en?(或任何其他组合)?@easyX请看这里,我想添加像father:“id123”这样的属性,这样我就可以做这样的事情:graddafter=persons[persons.IDabc.father]。father我不知道如何使用数组(没有抽象的索引号)最后我会有1000多人..所以我需要一个清晰的视图好的,在这种情况下,你对关联数组所做的是正确的。
$scope.persons = [
{ id: "ID123",
name_de: "Andre",
name_en: "Anrew",
age: 30,
description: "He is the father of xyz and . . .",
. . .
},
{ id: "IDabc",
name_de: "Markus",
name_en: "Mark",
age: 20,
},
. . .
];
var app = angular.module("YourApp");
app.filter('MyCustomFilter', function(){
return function(objects, criteria){
if(!criteria)
return objects;
var filterResult = new Array();
for(var index in objects)
if(objects[index].name_de.indexOf(criteria) != -1 || objects[index].name_en.indexOf(criteria) != -1)
filterResult.push(objects[index]);
return filterResult;
}
});
<input type="text" data-ng-model="personFilter" />
<div data-ng-repeat="person in persons | MyCustomFilter:personFilter"> ... </div>