简体   繁体   English

从id上的Firestore集合中按ID检索文档

[英]retrieve a document by id from collection of firestore on angular

I have a collection of activities in which there are multiple documents. 我有一组活动,其中有多个文档。 I want to retrieve a specific document on click button. 我想在单击按钮上检索特定文档。

Collection : activities 收藏 :活动

Document id : Auto-id 文件编号 :自动编号

Fields : activityid, locked, section, maxPlayers, name 字段 :activityid,locked,section,maxPlayers,name

here i am retrieving the whole collection as 在这里,我正在检索整个集合

this.activities = afs.collection('activities', x => x.orderBy('section', 
'asc')).valueChanges();

And i am showing these collections into the table as; 我将这些集合显示为表格;

<tr *ngFor="let activity of activities | async  | paginate: { itemsPerPage: 10, currentPage: p }; let i =index">
    <td>
        {{activity.section}}
    </td>
    <td >
        {{activity.name}}
    </td>
    <td>
        {{activity.maxPlayers}}
    </td>
    <td>
        {{activity.locked}}
    </td>
    <td>
        <button type="button" class="btn btn-success" 
            (click)="onEdit(activity.id);  secondModal.show() "><i 
            class="fa fa-pencil"></i>
                       Edit
        </button>
</td>

I passed an argument of id on OnEdit() function. 我在OnEdit()函数中传递了id参数。 In OnEdit function i want to retrieve a selective document by id. 在OnEdit函数中,我想通过ID检索选择性文档。 And show that document fields in the model as: 并将模型中的文档字段显示为:

<div *ngFor="let activity of activities | async >
    {{activity.section}}        
    {{activity.name}}                    
    {{activity.maxPlayers}}                    
    {{activity.locked}}

I tried so many thinks but dont work. 我尝试了很多想法,但没有成功。 It always shows all the documents. 它始终显示所有文档。 I want to retrieve only that document which is click by a button. 我只想检索通过按钮单击的那个文档。

Edit : In your activities.component.html file: 编辑:在您的activities.component.html文件中:

  <td>
    <button type="button" class="btn btn-success" 
      (click)="onEdit(activity);  secondModal.show()"><i 
      class="fa fa-pencil"></i>
        Edit
     </button>
  </td>

and in activities.component.ts file: 并在activities.component.ts文件中:

onEdit(activity){ 
  this.afs.doc("activities/"+activity.id).valueChanges();
  this.activity = activity;
  ..... your code
}

You have to declare an activity variable in your activities.component.ts file just like you do with activities variable , and you can use it in your modal template instead of activities no need to *ngFor directive . 您必须像在activities.component.ts文件中一样声明activity变量,就像使用activities变量一样,并且可以在模式模板中使用它,而不需要使用*ngFor指令。

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

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