Angularjs:Order和Filter by array intersect

时间:2015-12-10 16:57:07

标签: javascript angularjs node.js

这是我的条件:我通过异步调用检索具有类别的用户列表。

客户可以选择类别。

现在,我必须只显示在自己的类别中,从客户端中选择的用户,按自己的类别与所选类别匹配的数量。

实施例。 (括号内的数字是ID)

User B - Cats: - (no one)
User C - Cats: [7]Cat7, [2]Cat2
User A - Cats: [1] Cat1, [2]Cat2, [5]Cat5

客户选择的猫:[2],[1],[8]

所以在这里,用户B不会出现,剩下的顺序是A(2个匹配),C(1个匹配)。

这是一个笨蛋,我不知道从哪里开始。

http://plnkr.co/edit/oNlD8T?p=preview

最好在Angular中做(以及如何做?)或做一个新的异步调用(观看选定的猫)和服务器进程全部? (服务器端使用PHP,我不知道如何使用nodejs执行此操作)

2 个答案:

答案 0 :(得分:1)

您可以使用filter过滤器和自定义表达式function(value, index, array),您可以将其与所选数组进行比较。

https://docs.angularjs.org/api/ng/filter/filter

另一种方法是使用lodash,它有_.some_.intersection,这样可以更容易地编写自定义表达式。

https://lodash.com/docs#some
https://lodash.com/docs#intersection

您还可以为自己的filter(name, filterFactory)制作自定义过滤器: https://docs.angularjs.org/tutorial/step_09

答案 1 :(得分:0)

我为你创建了一个plunker plnkr.co/edit/nHVX5EbWYLarHnHR3LPQ?p=preview 希望这有助于您的要求。

我刚刚修改了现有代码。添加了一个条件来检查数组的长度是否大于零ng-show =“user.cats.length> 0”。

with cte1 (ID,parentID,type,name,visited,Lvl) as (
    select t.ID, t.parentID, t.type, t.name, t.visited, 0 as [Lvl]
    from t_hierarchy t
    where t.parentID is not null
    union all
    select c.ID, t.parentID, c.type, c.name, c.visited, c.Lvl + 1
    from t_hierarchy t
        inner join cte1 c on c.parentID = t.ID
    where t.parentID is not null
),
cte2 (ID,name,type,parentID,parentName_for_reference,visited,Lvl) as (
    Select t_hierarchy.ID, t_hierarchy.name, t_hierarchy.type, t_hierarchy.parentID, p.name as parentName_for_reference, t_hierarchy.visited, 0 as Lvl
        From t_hierarchy
        left join t_hierarchy p ON p.ID = t_hierarchy.parentID
        where t_hierarchy.parentID IS NULL
    UNION ALL
    Select t_hierarchy.ID, t_hierarchy.name, t_hierarchy.type, t_hierarchy.parentID,p.name as parentName_for_reference, t_hierarchy.visited, Lvl + 1 
    From t_hierarchy
    inner join cte2 on t_hierarchy.parentID = cte2.ID
    inner join t_hierarchy p ON p.ID = t_hierarchy.parentID
)

select cte2.ID,cte2.name,cte2.type,cte2.parentID,cte2.parentName_for_reference,cte2.visited,cte2.Lvl
,CASE WHEN type = 'city' THEN 'N/A' ELSE CAST(cnt.totalDescendents as varchar) END AS totalDescendents
,CASE WHEN type = 'city' THEN 'N/A' ELSE CAST(COALESCE(cnt2.totalDescendentsVisited,0) as varchar) END AS totalDescendentsVisited
,CASE WHEN type = 'city' THEN 'N/A' ELSE CAST((CAST(ROUND(CAST(COALESCE(cnt2.totalDescendentsVisited,0) as float)/CAST(cnt.totalDescendents as float),2) AS numeric(36,2))*100) as varchar) END as asPercentage
from cte2
left JOIN (
     SELECT  theID = parentID, COUNT(*) as totalDescendents
     FROM cte1
     WHERE type = 'city'
     GROUP BY parentID
  ) cnt ON cnt.theID = cte2.ID
 left JOIN (
     SELECT  theID = parentID, COUNT(*) as totalDescendentsVisited
     FROM cte1
     WHERE type = 'city' AND visited = 1
     GROUP BY parentID
  ) cnt2 ON cnt2.theID = cte2.ID
ORDER BY ID

另请注意,ng-show并不一定需要大于号码(> 0)。它只是增加了解决方案的清晰度。

正如Alon Valadji建议的那样,我用ng-show和ng-if更新了plunker。查看本文angular-performance ng-show-vs-ng-if并根据您的要求做出决定。