将从* ngFor创建的输入字段复制到外部模式中的输入字段

时间:2019-04-11 00:25:55

标签: javascript angular

我想构建一个弹出模式编辑表单,以更改动态表格中的各个表值。现在,使用功能editToggle(i)在单击按钮时出现输入字段。最多不会出现4个输入字段,因为它们用于编辑我的数据模型中的4个值。但是,输入字段(和值)是使用* ngFor动态生成的。我需要一些方法来将这些输入字段传递/复制到我的模式中,以便在那里进行编辑,而不是在网格本身上(单击编辑按钮后当前显示在网格上)。

我尝试使用[[ngModel)]进行克隆,但是它不起作用。我试图使用函数传递它们,但是值返回null。因为HTML仅显示一个输入字段(因为它们是使用* ngFor动态创建的),所以我不知道一种单独传递值的方法。

<div>
  <table align="center">
    <tr>
      <th>
        List of Providers
      </th>
    </tr>
  </table>
  <table id="thetable" align="center">
    <tr>
      <th>Application ID</th>
      <th>Client Name</th>
      <th>Version</th>
      <th>API Key</th>
      <th>Protected Secret</th>
      <th>EDIT/DELETE</th>
    </tr>
    <tr ng-app="tblRowApp" *ngFor="let prov of providers; let i = index">
      <td *ngFor="let col of columns">
        <span class="field" *ngIf="i !== index">
          {{prov[col]}}
        </span>
        <span *ngIf="i === index">
          <input [(ngModel)]="inputClientName" class="table" value="{{prov[col]}}" (change)="EditItem(i, col, $event.target.value)" type="text" placeholder="{{prov[col]}}">      
        </span>
      <td>
        <span *ngIf="editing && i === index">
          <button (click)="save()">Save</button>
        </span>
        <span *ngIf="i !== index">
          <button class="edit" name="editButton" (click)="editToggle(i); openEditForm()">/</button>
          <button class="delete" (click)="deleteRow(i)">x</button>
        </span>
      </td>
    </tr>
  </table>
<!-- The EDITING Modal -->
<div id="editForm" class="modal_edit">
    <div class="modal-content_edit">
      <span (click)="save()" class="close">&times;</span>
      <h2 style="margin-bottom: 70px">Edit OAuthAppProvider</h2>
      <div>
        <label style="margin-bottom: 20px">
          Client Name:
        </label>
        <input [(ngModel)]="inputClientName" id="editClientName" type="text">
      </div>

      <div>
        <label style="margin-bottom: 20px">
          Version
        </label>
        <input id="editClientVersion" type="text">
      </div>

      <div>
        <label style="margin-bottom: 20px">
          API Key:
        </label>
        <input id="editClientAPIKey" type="text">
      </div>

      <div>
        <label style="margin-bottom: 20px">
          Protected Secret
        </label>
        <input id="editClientProtectedSecret" type="text">
      </div>

      <button style="float: right" class="add" (click)="save()">
        <h4 style="font-style: bold">Save</h4>
      </button>
      <button class="cancel" (click)="save()">
        <h4 style="font-style: bold">Cancel</h4>
      </button>
    </div>
  </div>

</div>
export const PROVIDERS: any[] = 
[
    {
        AppID: "11",
        ClientName: "sampleclientname1",
        apiKey: "sampleapikey1",
        Version: "1.0",
        protectedsecret: "samplesecret1"
    },
    {
        AppID: "12",
        ClientName: "sampleclientname2",
        apiKey: "sampleapikey2",
        Version: "1.0",
        protectedsecret: "samplesecret2"
    },
    {
        AppID: "13",
        ClientName: "sampleclientname3",
        apiKey: "sampleapikey3",
        Version: "1.0",
        protectedsecret: "samplesecret3"
    },
    {
        AppID: "14",
        ClientName: "sampleclientname4",
        apiKey: "sampleapikey4",
        Version: "1.0",
        protectedsecret: "samplesecret4"
    }
]

1 个答案:

答案 0 :(得分:0)

您可以设置一个名为selectedRowData的变量,并在用户单击编辑按钮时将提供程序设置为其值。可以将模式上输入的value属性设置为所选行的属性。没有组件代码很难说出其他方法的功能,所以我做了一些假设。让我知道您是否还有其他疑问。

这是指向StackBlitz的链接。

编辑

数据仅通过[value]属性进行绑定,并且没有像跟踪Reactive Forms这样的表单对象跟踪所有更改,因此应首先创建模型

我注释掉了原始解决方案,并在下面添加了更新。 selectedRowData变量由具有空属性的provider对象实例化。模态已更新为使用[(ngModel)]的双向绑定。 StackBlitz也已更新。

当用户在表单中输入其编辑内容时,表将更新。除非需要将数据保留在某处,否则不需要使用保存按钮。

检查Angular Forms Documentation,它应该有助于如何在组件之间传递表单数据。您在此处创建的内容类似于Template-driven Forms

组件

// selectedRowData = null;
selectedRowData = {
    AppID: "",
    ClientName: "",
    apiKey: "",
    Version: "",
    protectedsecret: ""
};

editToggle(rowData) {
  this.selectedRowData = rowData;
}

表格

<div>
  <table align="center">
    <tr>
      <th>
        List of Providers
      </th>
    </tr>
  </table>
  <table id="thetable" align="center">
    <tr>
      <th>Application ID</th>
      <th>Client Name</th>
      <th>Version</th>
      <th>API Key</th>
      <th>Protected Secret</th>
      <th>EDIT/DELETE</th>
    </tr>
    <tr ng-app="tblRowApp" *ngFor="let prov of providers; let i = index">
      <td *ngFor="let col of columns">
        <span class="field" *ngIf="i !== index">
          {{prov[col]}}
        </span>
        <span *ngIf="i === index">
          <input [(ngModel)]="inputClientName" class="table" value="{{prov[col]}}"
          (change)="EditItem(value)" type="text" placeholder="{{prov[col]}}">      
        </span>
      <td>
        <span *ngIf="editing && i === index">
          <button (click)="save()">Save</button>
        </span>
        <span *ngIf="i !== index">
          <button class="edit" name="editButton" (click)="editToggle(prov); openEditForm()">/</button>
          <button class="delete" (click)="deleteRow(i)">x</button>
        </span>
      </td>
    </tr>
  </table>

模式

<!-- The EDITING Modal -->
<div id="editForm" class="modal_edit">
    <div class="modal-content_edit">
      <span (click)="save()" class="close">&times;</span>
      <h2 style="margin-bottom: 70px">Edit OAuthAppProvider</h2>
      <div>
        <label style="margin-bottom: 20px">
          Client Name:
        </label>
        <!-- <input id="editClientName" type="text" [value]="selectedRowData?.ClientName"> -->
        <input id="editClientName" type="text" [(ngModel)]="selectedRowData.ClientName">
      </div>

      <div>
        <label style="margin-bottom: 20px">
          Version
        </label>
        <!-- <input id="editClientVersion" type="text" [value]="selectedRowData?.Version"> -->
        <input id="editClientVersion" type="text" [(ngModel)]="selectedRowData.Version">
  </div>
      </div>

      <div>
        <label style="margin-bottom: 20px">
          API Key:
        </label>
        <!-- <input id="editClientAPIKey" type="text" [value]="selectedRowData?.apiKey"> -->
        <input id="editClientAPIKey" type="text" [(ngModel)]="selectedRowData.apiKey">
      </div>

      <div>
        <label style="margin-bottom: 20px">
          Protected Secret
        </label>
        <!-- <input id="editClientProtectedSecret" type="text" [value]="selectedRowData?.protectedsecret"> -->
        <input id="editClientProtectedSecret" type="text" [(ngModel)]="selectedRowData.protectedsecret">
      </div>

      <button style="float: right" class="add" (click)="save()">
        <h4 style="font-style: bold">Save</h4>
      </button>
      <button class="cancel" (click)="save()">
        <h4 style="font-style: bold">Cancel</h4>
      </button>
    </div>
  </div>

</div>