代码之家  ›  专栏  ›  技术社区  ›  devmrh

角度5显示嵌套的问题并回答父项下的显示项

  •  2
  • devmrh  · 技术社区  · 7 年前

    这是我的服务器对问题和答案的响应。它是一个数组。

    [
        {
            "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的响应,因为它有partent_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>
    

    2 回复  |  直到 7 年前
        1
  •  1
  •   Nandita Sharma    7 年前

    试试这个html,其中的响应如下

    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>
    

    检查这个 fiddle

        2
  •  1
  •   Eliseo    7 年前

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

    <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>