网站搜索

typescript - angular 5 显示嵌套问题和答案,并在其父项下显示项目


这是我用于问答的服务器响应.. 它是一个数组。

[
    {
        "id": 1,
        "product": 1,
        "user": "alex",
        "text": "is it ok?",
        "parent_id": null,
    },
    {
        "id": 2,
        "product": 1,
        "user": "john doe",
        "text": "yes its ok.",
        "publish": true,
        "parent_id": 1,
    },
      {
        "id": 3,
        "product": 1,
        "user": "shiva",
        "text": "how can i .. . .",
        "publish": true,
        "parent_id": null,
    },
]

第二项(id 2)是问题1的回答,因为它有parent_id=1,而id 3是独立的问题

我想在嵌套有序列表中显示每个问题下的各个回答,如果问题有回答的话……我该怎么做?

我做了类似的事情,但我觉得完全错了,有人能指导我正确地做吗?谢谢

我用于获取问题和答案的组件

  this._store.viewSingleProductQA(this.product.product_id).subscribe(data => {
      this.questions = data;

     this.responses = this.questions.filter(d => d.parent_id == this.questions.id)
    });

html :

  <div>{{question?.user}}</div>
  <div>{{question.text}}</div>
</div>
<div *ngFor="let res of responses">
  {{res.text}}
</div>

答案#1

尝试这个 HTML,其中 responses 如下

responses = [
    {
        "id": 1,
        "product": 1,
        "user": "alex",
        "text": "is it ok?",
        "parent_id": null,
    },
    {
        "id": 2,
        "product": 1,
        "user": "john doe",
        "text": "yes its ok.",
        "publish": true,
        "parent_id": 1,
    },
      {
        "id": 3,
        "product": 1,
        "user": "shiva",
        "text": "how can i .. . .",
        "publish": true,
        "parent_id": null,
    },
]

<div *ngFor="let res of responses">
  <div *ngIf="res.parent_id === null">
    Ques : {{res.text}}
    <br/>
    Answers :
    <ng-container *ngFor="let res2 of responses">

       <div *ngIf="res2.parent_id === res.id">
         {{res2.text}}
       </div>
    </ng-container>
  </div>

</div>

看这个小玩意

答案#2

如果只有一个回应(而不是回应的回应),你总是可以做到

<div *ngFor="let q of questions>
   <!--only want to show the question, not the answer-->
   <div *ngIf="!q.parent_id> 
      {{q.user}}{{q.text}}
      <div *ngFor="let a of question.filter(t=>t.parent_id==q.id)>
          response:{{a.user}}{{a.text}}
      </div>
   <div>
</div>

或者“映射”数组以创建一个新的问答列表

this.questionAndAndwers=this.question
          .filter(q=>!q.parent_id)
          .map(q=>{
               let answer=this.question.find(i=>i.parent_id==q.id)
               return {...q,
                       a_user:answer?answer.user:null,
                      a_text:answer?answer.text:null
                      ..others properties....
               }
          })

已编辑:完成答案 我们也可以映射像

this.questionAndAndwers=this.question
          .filter(q=>!q.parent_id)
          .map(q=>{
               return {...q,
                       answers:this.question.filter(i=>i.parent_id==q.id)
               }
          });

或者如果有回复的回复的回复……我们可以做一个递归函数

getAnswers(questions:any,id:any)
  {
    return questions
           .filter(i=>i.parent_id==id)
           .map(q=>{
             return {...q,
                     answers:this.getAnswers(questions,q.id)}
           })
  }
//And
this.questionAndAndwers=this.getAnswers(this.question,null)

然后我们可以制作一个组件

@Component({
  selector: 'app-question',
  template: `<div *ngFor="let q of questions">
              {{q.user}}{{q.text}}
              <app-question [questions]=q.answers></app-question>
           </div>`
})
export class QuestionComponent  {
  @Input() questions: any;
}

//In our app.component
<app-question [questions]="questionAndAndwers"></app-question>