Use one textarea to update multiple variables

I have a webpage containing a form where for each question, the user enters a numerical response and some additional comments. The comments are currently inputted in an input type=”text”, but to allow for more space, I want to open a dialog with a larger textarea when the input box is clicked.

I would like to create one dialog popup box containing the textarea, which is bound using ngModel to a variable depending on which comment input box is clicked. A JS implementation would also work though.

This is what I have so far:

form-page.component.html

  <br><br>
                        <p-card class="p-fluid">
                            <ng-template pTemplate="title">
                                <span style="color:#00a499;">Maximum Demand (MVA)</span>
                            </ng-template>
                            <ng-template pTemplate="content">
                                <div class="formgrid grid">
                                    <div class="field col">
                                        <label for="remarks_max_demand">Remarks</label>
                                        <input #remarks_max_demand="ngModel" type="text" pInputText
                                            id="remarks_max_demand"
                                            [(ngModel)]="currentRemark"
                                            (click)="openRemarksDialog();setRemark(data.generalData.max_demand.remarks_max_demand)" />
                                    </div>
                                </div>
                            </ng-template>
                        </p-card>

                        <br><br>
                        <p-card class="p-fluid">
                            <ng-template pTemplate="title">
                                <span style="color:#00a499;">Total Installed Capacity (MVA)</span>
                            </ng-template>
                            <ng-template pTemplate="content">
                                <div class="formgrid grid">
                                    <div class="col-12">
                                        <h4 style="text-align: left;">Grid</h4>
                                    </div>
                                    <div class="field col">
                                        <label for="remarks_TIC_grid">Remarks</label>
                                        <input type="text" pInputText id="remarks_TIC_grid" [(ngModel)]="data.generalData.total_installed_capacity.remarks_TIC_grid"
                                        (click)="openRemarksDialog();setRemark(data.generalData.total_installed_capacity.remarks_TIC_grid)"/>
                                    </div>
                                </div>

<p-dialog 
    [(visible)]="visible" 
    [style]="{ width: '600px' }" 
    header="Remarks" 
    [modal]="true" 
    styleClass="p-fluid">

    <ng-template pTemplate="content">
        <div class="field">
            <label for="remarks_input">Please type any remarks.</label>
            <textarea 
                id="remarks_input"
                rows="10"
                cols="30" 
                pInputTextarea 
                [(ngModel)]="currentRemark"
                >
            </textarea>
        </div>
    </ng-template>

    <ng-template pTemplate="footer">
    <p-button 
        label="Continue" 
        icon="pi pi-arrow-right" 
        [text]="true" 
        (onClick)="hideRemarksDialog()" />
    </ng-template>
</p-dialog>

form-page.component.ts

import { Component, OnInit } from '@angular/core';
import { Router, RouterOutlet } from '@angular/router';
import { FormBuilder, Validators } from '@angular/forms';
import { Subscription } from 'rxjs';
import { MessageService } from 'primeng/api';
import { EvaluationService } from './service/data';
import { Dropdown } from 'src/app/models/dropdown.model';
import { DropdownService } from 'src/app/services/dropdown.service';
import { FilterInput, PagedResultDto } from 'src/app/models/custom.model';
import { DefinitionLibrary } from 'src/app/models/definition-library.model';
import { DefinitionLibraryService } from 'src/app/services/definition-library.service';
import { TableLazyLoadEvent } from 'primeng/table';

@Component({
  selector: 'app-form-page',
  templateUrl: './form-page.component.html',
  styleUrl: './form-page.component.scss'
})
export class FormPageComponent implements OnInit {
  data: any;
  currentRemark!: any

  constructor(public messageService: MessageService, public evaluationService: EvaluationService, private dropdownService: DropdownService, private definitionLibraryService: DefinitionLibraryService, private router: Router) { };

    openRemarksDialog() {
    this.visible = true;

  }

  setRemark(data?: any){
    this.currentRemark = data;
  }

  getRemark(){
    return this.currentRemark;
  }

  hideRemarksDialog() {
    this.visible = false;
  }

    ngOnInit(): void {
    this.getOpuDropdown();
    this.data = this.evaluationService.getEvaluationData()
  }
}

The above works for the first remarks box (remarks_max_demand), but not for the others. How can I get it to work for multiple boxes/variables?

Thank you!

2

To fix this issue, you need to pass the specific remark associated with each input field into the dialog and bind the currentRemark to that specific field dynamically. Instead of directly modifying the model in each input field’s click event, you can refactor the logic to identify the field being clicked and bind the correct remark.

Solution:
Modify the openRemarksDialog function to accept the specific remark value.
Ensure that currentRemark is set to the specific remark for the field clicked.

Here’s the updated code:

form-page.component.html

<p-card class="p-fluid">
    <ng-template pTemplate="title">
        <span style="color:#00a499;">Maximum Demand (MVA)</span>
    </ng-template>
    <ng-template pTemplate="content">
        <div class="formgrid grid">
            <div class="field col">
                <label for="remarks_max_demand">Remarks</label>
                <input #remarks_max_demand="ngModel" type="text" pInputText
                    id="remarks_max_demand"
                    [ngModel]="data.generalData.max_demand.remarks_max_demand"
                    (click)="openRemarksDialog(data.generalData.max_demand.remarks_max_demand, 'remarks_max_demand')" />
            </div>
        </div>
    </ng-template>
</p-card>

<br><br>

<p-card class="p-fluid">
    <ng-template pTemplate="title">
        <span style="color:#00a499;">Total Installed Capacity (MVA)</span>
    </ng-template>
    <ng-template pTemplate="content">
        <div class="formgrid grid">
            <div class="field col">
                <label for="remarks_TIC_grid">Remarks</label>
                <input type="text" pInputText id="remarks_TIC_grid" 
                    [ngModel]="data.generalData.total_installed_capacity.remarks_TIC_grid"
                    (click)="openRemarksDialog(data.generalData.total_installed_capacity.remarks_TIC_grid, 'remarks_TIC_grid')" />
            </div>
        </div>
    </ng-template>
</p-card>

<p-dialog [(visible)]="visible" [style]="{ width: '600px' }" header="Remarks" [modal]="true" styleClass="p-fluid">
    <ng-template pTemplate="content">
        <div class="field">
            <label for="remarks_input">Please type any remarks.</label>
            <textarea id="remarks_input" rows="10" cols="30" pInputTextarea [(ngModel)]="currentRemark"></textarea>
        </div>
    </ng-template>

    <ng-template pTemplate="footer">
        <p-button label="Continue" icon="pi pi-arrow-right" [text]="true" (onClick)="saveRemark();hideRemarksDialog()" />
    </ng-template>
</p-dialog>

form-page.component.ts

export class FormPageComponent implements OnInit {
  data: any;
  currentRemark!: string;
  currentField!: string;
  visible = false;

  constructor(
    public messageService: MessageService,
    public evaluationService: EvaluationService,
    private dropdownService: DropdownService,
    private definitionLibraryService: DefinitionLibraryService,
    private router: Router
  ) {}

  openRemarksDialog(remark: string, field: string) {
    this.currentRemark = remark;
    this.currentField = field;
    this.visible = true;
  }

  saveRemark() {
    // Save the current remark to the corresponding field
    if (this.currentField === 'remarks_max_demand') {
      this.data.generalData.max_demand.remarks_max_demand = this.currentRemark;
    } else if (this.currentField === 'remarks_TIC_grid') {
      this.data.generalData.total_installed_capacity.remarks_TIC_grid = this.currentRemark;
    }
    // Add more fields as needed
  }

  hideRemarksDialog() {
    this.visible = false;
  }

  ngOnInit(): void {
    this.getOpuDropdown();
    this.data = this.evaluationService.getEvaluationData();
  }
}

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật