有条件显示输入场 - 角反应形式

发布于 2025-01-17 14:35:08 字数 8173 浏览 1 评论 0原文

问题:在尝试显示表单列表的特定索引的输入字段时,两个区域一次都显示一个。

澄清:我目前有一个植入*ngfor的表格。循环列表中的每个项目时,我可以禁用某些表单字段。但是,我想根据该特定索引的输入值隐藏/显示表单的特定索引的特定表单字段。

如果要记录的物品的当前输入时间是在分配的“晚期”时间之后,我想显示此“测试原因”输入。

中输入时间

当我尝试仅显示该特定索引的“测试季节”输入时,

从NGXMATDATEPICKER

 <div *ngIf="testList.length > 0">
    <div
      class="row"
      *ngFor="
        let test of testList;
        index as i;
        first as isFirst;
        last as isLast
      "
      [formGroup]="testFormGroup"
    >
      <!-- TEST -->
      <div class="row py-2 ms-3">
        <div class="col-2">
          <div class="row title">
            DUMMY DATA ORIGIN
          </div>
          <div class="row">{{ test.city  }}</div>
        </div>
        <div class="col-2">
          <div class="row title">
            {{
              isFirst
                ? "Pick Up Early"
                : isLast
                ? "Delivery Early"
                : "Intermediate Early"
            }}
          </div>
          <div class="row">
            {{ test.apptEarly | date: "MM/dd/yy HH:mm" }}
          </div>
        </div>
        <div class="col-2">
          <div class="row title">
            {{
              isFirst
                ? "Pick Up Late"
                : isLast
                ? "Delivery Late"
                : "Intermediate Late"
            }}
          </div>
          <div class="row">
            {{ test.apptLate | date: "MM/dd/yy HH:mm" }}
          </div>
        </div>
        <div class="col-2 float-left">
          <div class="row title anti-spacer">Arrived</div>
          <div
            class="row gx-1"
            *ngIf="!test.loggedArrival"
            style="margin-left: -10px"
          >
            <mat-form-field appearance="outline">
              <input
                matInput
                [ngxMatDatetimePicker]="picker"
                [formControl]="
                  testFormArr.controls[i]['controls']['arrivalTime']
                "
                [disabled]="
                  (i > 0 &&
                    testList[i - 1].loggedDepartureTime == null) ||
                  test.loggedArrival
                "
                class="form-field"
                (dateChange)="
                  testFieldInserted('arrivalTime', $event.value)
                "
              />
              <mat-datepicker-toggle
                matSuffix
                [for]="picker"
                [disabled]="
                  (i > 0 &&
                    testList[i - 1].loggedDeparture == null) ||
                  test.loggedArrival
                "
              >
                <mat-icon matDatepickerToggleIcon class="picker-icon">
                  calendar_today
                </mat-icon>
              </mat-datepicker-toggle>
              <ngx-mat-datetime-picker
                #picker
                [showSeconds]="false"
                [enableMeridian]
              >
                <ng-template>
                  <span>OK</span>
                </ng-template>
              </ngx-mat-datetime-picker>
            </mat-form-field>
          </div>
          <div class="row spacer" *ngIf="test.loggedArrival">
            {{ test.loggedArrival | date: "MM/dd/YY HH:mm" }}
          </div>
        </div>
        <div class="col-2">
          <div class="row title anti-spacer">Departure</div>
          <div
            class="row gx-1"
            *ngIf="!test.loggedDeparture"
            style="margin-left: -10px"
          >
            <mat-form-field appearance="outline">
              <input
                matInput
                [ngxMatDatetimePicker]="picker"
                [disabled]="
                  test.loggedDeparture ||
                  !test.loggedArrival
                "
                class="form-field"
                [formControl]="
                  testFormArr.controls[i]['controls']['departureTime']
                "
                (dateChange)="
                  testFieldInserted('departureTime', $event.value)
                "
              />
              <mat-datepicker-toggle
                matSuffix
                [for]="picker"
                [disabled]="
                  test.loggedDeparture ||
                  !test.loggedArrival
                "
              >
                <mat-icon matDatepickerToggleIcon class="picker-icon"
                  >calendar_today</mat-icon
                >
              </mat-datepicker-toggle>
              <ngx-mat-datetime-picker
                #picker
                [showSeconds]="false"
                [enableMeridian]
              >
                <ng-template>
                  <span>OK</span>
                </ng-template>
              </ngx-mat-datetime-picker>
            </mat-form-field>
          </div>
          <div class="row spacer" *ngIf="test.loggedDeparture">
            {{ test.loggedDeparture | date: "MM/dd/YY HH:mm" }}
          </div>
        </div>
        <div class="col-2">
          <div class="row title anti-spacer">Reason</div>
          <div class="row gx-1" style="margin-left: -10px">
            <mat-form-field appearance="outline">
              <input
                matInput
                class="form-field"
                placeholder="Choose Reason"
                [matAutocomplete]="testReason"
                [formControl]="
                  testFormArr.controls[i]['controls']['testReason']
                "
              />
              <mat-autocomplete #testReason="matAutocomplete">
                <mat-option
                  *ngFor="let test of testReason"
                  [value]="test.value"
                >
                  {{ test.name }}
                </mat-option>
              </mat-autocomplete>
            </mat-form-field>
          </div>
        </div>
      </div>
    </div>
  </div>


    return this.formBuilder.group({

      arrivalTime: [''],

      departureTime: [''],

      testReason: [''],

    });

  }

  addFormControlItem() {

    this.testFormArr = this.testFormGroup.get('testFormArr') as FormArray;

    this.testFormArr.push(this.createFormControlField());

  }

  testFieldInserted(field: string, time: any): void {

    this.testCycleObject = {

      [field]: `${moment(test).format('YYYY-MM-DDTHH:mm:ss')}`,

    };

  }

  setTest() {

    this.testList = [];

    this.test.testTests.forEach((test, index) => {

      let testObject: TestObject = {

        city: test.city,

        state: test.state,

        apptEarly: test.appointmentEarly,

        apptLate: test.appointmentLate,

        arrivalTime:

          this.test.trackingTests &&

          this.test.trackingTests.length > 0 &&

          this.test.trackingTests.length > index

            ? this.test.trackingTests[index].arrivalTime

            : null,

        departureTime:

          this.test.trackingTests &&

          this.test.trackingTests.length > 0 &&

          this.test.trackingTests.length > index

            ? this.test.trackingTests[index].departureTime

            : null,

        actualArrivalTime: null,

        actualDepartureTime: null,

        lateReason: '',

      };

      this.testList.push(testObject);

      this.addFormControlItem();

    });

    this.validateButtons();

  }

  testReason = [

    {

      name: 'TEST REASON ONE',

      value: 'TEST REASON ONE',

    },

    {

      name: 'TEST REASON TWO',

      value: 'TEST REASON TWO',

    },

  ]; ```

Problem: When trying to show input field for specific index of form list, both areas show instead of one at a time.

Clarification: I currently have a form that is implanted in an *ngFor. When looping each item in my list, I am able to disable certain form fields. However, there is a specific form field that I would like to hide/show for that specific index of the form based on that specific index's input values.

I would like to show this "test Reason" input if the current entered time of the item being logged is after the assigned "late" time.

I get the time entered from the ngxMatDatePicker

When I go to try to show only that specific index's "testReason" input, it enables the showing of all "testReason" inputs

TEMPLATE

 <div *ngIf="testList.length > 0">
    <div
      class="row"
      *ngFor="
        let test of testList;
        index as i;
        first as isFirst;
        last as isLast
      "
      [formGroup]="testFormGroup"
    >
      <!-- TEST -->
      <div class="row py-2 ms-3">
        <div class="col-2">
          <div class="row title">
            DUMMY DATA ORIGIN
          </div>
          <div class="row">{{ test.city  }}</div>
        </div>
        <div class="col-2">
          <div class="row title">
            {{
              isFirst
                ? "Pick Up Early"
                : isLast
                ? "Delivery Early"
                : "Intermediate Early"
            }}
          </div>
          <div class="row">
            {{ test.apptEarly | date: "MM/dd/yy HH:mm" }}
          </div>
        </div>
        <div class="col-2">
          <div class="row title">
            {{
              isFirst
                ? "Pick Up Late"
                : isLast
                ? "Delivery Late"
                : "Intermediate Late"
            }}
          </div>
          <div class="row">
            {{ test.apptLate | date: "MM/dd/yy HH:mm" }}
          </div>
        </div>
        <div class="col-2 float-left">
          <div class="row title anti-spacer">Arrived</div>
          <div
            class="row gx-1"
            *ngIf="!test.loggedArrival"
            style="margin-left: -10px"
          >
            <mat-form-field appearance="outline">
              <input
                matInput
                [ngxMatDatetimePicker]="picker"
                [formControl]="
                  testFormArr.controls[i]['controls']['arrivalTime']
                "
                [disabled]="
                  (i > 0 &&
                    testList[i - 1].loggedDepartureTime == null) ||
                  test.loggedArrival
                "
                class="form-field"
                (dateChange)="
                  testFieldInserted('arrivalTime', $event.value)
                "
              />
              <mat-datepicker-toggle
                matSuffix
                [for]="picker"
                [disabled]="
                  (i > 0 &&
                    testList[i - 1].loggedDeparture == null) ||
                  test.loggedArrival
                "
              >
                <mat-icon matDatepickerToggleIcon class="picker-icon">
                  calendar_today
                </mat-icon>
              </mat-datepicker-toggle>
              <ngx-mat-datetime-picker
                #picker
                [showSeconds]="false"
                [enableMeridian]
              >
                <ng-template>
                  <span>OK</span>
                </ng-template>
              </ngx-mat-datetime-picker>
            </mat-form-field>
          </div>
          <div class="row spacer" *ngIf="test.loggedArrival">
            {{ test.loggedArrival | date: "MM/dd/YY HH:mm" }}
          </div>
        </div>
        <div class="col-2">
          <div class="row title anti-spacer">Departure</div>
          <div
            class="row gx-1"
            *ngIf="!test.loggedDeparture"
            style="margin-left: -10px"
          >
            <mat-form-field appearance="outline">
              <input
                matInput
                [ngxMatDatetimePicker]="picker"
                [disabled]="
                  test.loggedDeparture ||
                  !test.loggedArrival
                "
                class="form-field"
                [formControl]="
                  testFormArr.controls[i]['controls']['departureTime']
                "
                (dateChange)="
                  testFieldInserted('departureTime', $event.value)
                "
              />
              <mat-datepicker-toggle
                matSuffix
                [for]="picker"
                [disabled]="
                  test.loggedDeparture ||
                  !test.loggedArrival
                "
              >
                <mat-icon matDatepickerToggleIcon class="picker-icon"
                  >calendar_today</mat-icon
                >
              </mat-datepicker-toggle>
              <ngx-mat-datetime-picker
                #picker
                [showSeconds]="false"
                [enableMeridian]
              >
                <ng-template>
                  <span>OK</span>
                </ng-template>
              </ngx-mat-datetime-picker>
            </mat-form-field>
          </div>
          <div class="row spacer" *ngIf="test.loggedDeparture">
            {{ test.loggedDeparture | date: "MM/dd/YY HH:mm" }}
          </div>
        </div>
        <div class="col-2">
          <div class="row title anti-spacer">Reason</div>
          <div class="row gx-1" style="margin-left: -10px">
            <mat-form-field appearance="outline">
              <input
                matInput
                class="form-field"
                placeholder="Choose Reason"
                [matAutocomplete]="testReason"
                [formControl]="
                  testFormArr.controls[i]['controls']['testReason']
                "
              />
              <mat-autocomplete #testReason="matAutocomplete">
                <mat-option
                  *ngFor="let test of testReason"
                  [value]="test.value"
                >
                  {{ test.name }}
                </mat-option>
              </mat-autocomplete>
            </mat-form-field>
          </div>
        </div>
      </div>
    </div>
  </div>

TS


    return this.formBuilder.group({

      arrivalTime: [''],

      departureTime: [''],

      testReason: [''],

    });

  }

  addFormControlItem() {

    this.testFormArr = this.testFormGroup.get('testFormArr') as FormArray;

    this.testFormArr.push(this.createFormControlField());

  }

  testFieldInserted(field: string, time: any): void {

    this.testCycleObject = {

      [field]: `${moment(test).format('YYYY-MM-DDTHH:mm:ss')}`,

    };

  }

  setTest() {

    this.testList = [];

    this.test.testTests.forEach((test, index) => {

      let testObject: TestObject = {

        city: test.city,

        state: test.state,

        apptEarly: test.appointmentEarly,

        apptLate: test.appointmentLate,

        arrivalTime:

          this.test.trackingTests &&

          this.test.trackingTests.length > 0 &&

          this.test.trackingTests.length > index

            ? this.test.trackingTests[index].arrivalTime

            : null,

        departureTime:

          this.test.trackingTests &&

          this.test.trackingTests.length > 0 &&

          this.test.trackingTests.length > index

            ? this.test.trackingTests[index].departureTime

            : null,

        actualArrivalTime: null,

        actualDepartureTime: null,

        lateReason: '',

      };

      this.testList.push(testObject);

      this.addFormControlItem();

    });

    this.validateButtons();

  }

  testReason = [

    {

      name: 'TEST REASON ONE',

      value: 'TEST REASON ONE',

    },

    {

      name: 'TEST REASON TWO',

      value: 'TEST REASON TWO',

    },

  ]; ```

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

荒路情人 2025-01-24 14:35:08

您可以尝试使用 *ngif在您想要有条件渲染的测试[索引]中使用。

You could try using *ngIf with your test[index] that you want to conditionally render.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文