简体   繁体   English

如何在angularjs的数组顶部推送相同的文档

[英]How to push same documents in top an array in angularjs

I have a data as follows in which i store each date a record 我有如下数据,其中我存储每个日期的记录

     {
      "attendances": [
        {
          "_id": "586bb4cf5ab55f517ca4304b",
          "course": {
            "_id": "586645636ce1252f20ba1ad6",
            "name": "Robotics"
          },
          "date":"22/11/2016",
          "ispresent": "Yes",
        },
        {
          "_id": "586bafe95ab55f517ca43048",
          "course": {
            "_id": "586645636ce1252f20ba1ad6",
            "name": "Robotics"
          },
      "date":"21/11/2016",
          "ispresent": "Yes",
        },
{
          "_id": "586bafe95ab55f517ca43048",
          "course": {
            "_id": "586645636ce1252f20ba1ad6",
            "name": "Statistics"
          },
      "date":"21/11/2016",
          "ispresent": "Yes",
        },
         {
          "_id": "586bafe95ab55f517ca43048",
          "course": {
            "_id": "586645636ce1252f20ba1ad6",
            "name": "Statistics"
          },
      "date":"21/11/2016",
          "ispresent": "Yes",
        }
      ],
    }

I want the output as follows in a table 我想要一个表中的输出如下

        Robotics    yes yes
        Statistics  No  yes

I have each record for each course with each date.Can some one help me please. 我有每个日期的每门课程的每条记录。请帮我一下。

You can do it by adding a groupBy filter, like the one from angular-filter lib https://github.com/a8m/angular-filter , which will group your objects based on the name property. 您可以通过添加groupBy过滤器来完成此操作,例如angular-filter lib https://github.com/a8m/angular-filter中的一个过滤器,它将根据name属性对对象进行分组。

And then your html will look like something like this: 然后您的html看起来像这样:

 <div ng-repeat="(key,value) in attendances | groupBy: 'course.name'">
   {{key}} - <span ng-repeat="item in value">{{item.ispresent}}</span>
 </div>

find this working Sample: https://plnkr.co/edit/KVVK1A0QJ7VF7DbXZGi5?p=preview 找到此工作示例: https : //plnkr.co/edit/KVVK1A0QJ7VF7DbXZGi5?p=preview

Let's imagine that the data is stored inside your controller in DATA variable. 假设数据存储在控制器中的DATA变量中。

In your template, just do: 在您的模板中,只需执行以下操作:

<table>
  <tr ng-repeat="d in DATA">
    <td>{{d.course.name}}</td>
    <td>{{ d.ispresent ? d.ispresent : 'no'}}</td>
  <tr>
</table>

This will display no if ispresent property doesn't exist. 如果ispresent属性不存在,则将显示no。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM