【问题标题】:Angular conditional readonly/disable on input fields输入字段的角度条件只读/禁用
【发布时间】:2018-10-09 20:42:12
【问题描述】:

我有一个带有输入字段的表格,我正在用模型值填充这些输入字段,我想将只读/禁用应用于这些填充的字段。当我单击添加行时,我将空行添加到表中。添加到表中的空行必须是可编辑的。我找不到仅将 readOnly/disable 应用于已填充的表格单元格的逻辑。

HTML

<table>
<thead>
    <th> Name </th>
    <th> Age </th>
    <th> Url </th>
    <th> Gender </th>
    <th> Image </th>
    <th> Keywords </th>
</thead>
<tbody>
    <tr *ngFor="let data of userList; let $index = index">
        <td> <input class="form-control" type="text" id="userListName"[(ngModel)]="userList[$index].name"
            name="userListName{{$index}}" [readonly]="userList[$index].name.length"/></td>
        <td> <input class="form-control" type="text" id="userListAge" [(ngModel)]="userList[$index].age"
            name="userListAge{{$index}}" readonly/></td>
        <td><input class="form-control" type="text" id="userListUrl" [(ngModel)]="userList[$index].url" name="userListUrl{{$index}}" readonly/></td>
        <td> <input class="form-control" type="text" id="userListGender" [(ngModel)]="userList[$index].gender"
            name="userListGender{{$index}}" readonly/></td>

        <td> <input class="form-control" type="text" id="userListImage" [(ngModel)]="userList[$index].image"
            name="userListImage{{$index}}"  readonly/>
        </td>
        <td> <input class="form-control" type="text" id="userListKeywords" [(ngModel)]="userList[$index].keywords"
            name="userListKeywords{{$index}}" readonly/></td>
      </tr>
     </tbody>

 <button (click) ="addRow()"> Add Row </button>
 </table>

组件:

 import { Component, OnInit } from '@angular/core';

 @Component({
 selector: 'my-app',
 templateUrl: './app.component.html',
 styleUrls: ['./app.component.css']
 })
 export class AppComponent {

 userList: userModel[] = [];

jsonUser = [
{
name: 'abc',
age: 17,
url: "a.com",
gender: "F",
image: "i-ocion.png",
keywords: "blah"
}, 
{
name: 'cde',
age: 18,
url: "cde.com",
gender: "F",
image: "i-oddcion.png",
keywords: "blahhh"
}, 
{
name: 'efg',
age: 19,
url: "efg.com",
gender: "F",
image: "i-ocfffion.png",
keywords: "blahhhhhhh"
}
]
ngOnInit() {
this.userList = this.jsonUser;
}

addRow() {
this.userList.push(new userModel);
}
}

export class userModel {
name: string;
age: number;
url: any;
gender: string;
image: string;
keywords: string;
}

Demo

【问题讨论】:

    标签: javascript html angular typescript angular-forms


    【解决方案1】:

    您可以声明一个变量来标识来自后端的数组的大小(如 initialArraySize),然后在添加新行时验证该行的索引是否大于初始数组大小,如果为真, 你让它可编辑..

    <tbody>
    <tr *ngFor="let data of userList; let index = index">
        <td> <input class="form-control" type="text" id="userListName" [(ngModel)]="userList[index].name"
            name="userListName{{index}}" [readonly]="index >== initialArraySize"/></td>
    </tr>
    </tbody>
    

    【讨论】:

      【解决方案2】:

      新的空行的索引为$index === userList - 1,因为它是该集合的最后一项。您可以使用该条件并应用您想要的任何属性。

      【讨论】:

      • 我相信如果我在表格中添加两个或更多的空行,这个条件是行不通的。
      【解决方案3】:
      <td>
      <input
          class="form-control"
          type="text"
          id="userListKeywords"
          [(ngModel)]="userList[$index].keywords"
          name="userListKeywords{{$index}}"
          [attr.disabled]="true" readonly/>
      </td>
      

      [attr.disabled]="true" 将使输入不可编辑,您可以修改代码以使该属性可配置。


      jsonUser 对象内添加一个属性

      jsonUser = [
      {
      name: 'abc',
      age: 17,
      url: "a.com",
      gender: "F",
      image: "i-ocion.png",
      keywords: "blah",
      readonly: true
      }, 
      ]
      

      然后

      <td>
      <input
          class="form-control"
          type="text"
          id="userListKeywords"
          [(ngModel)]="userList[$index].keywords"
          name="userListKeywords{{$index}}"
          [attr.disabled]="userList[$index].readonly" readonly/>
      </td>
      

      [attr.disabled]="userList[$index].readonly"

      【讨论】:

      • 我可以使输入不可编辑。我的问题是如何在循环中使少数输入可编辑。
      • 我正在寻找一种 UI 方法。我从后端收到 jsonUser 对象,我无法更改 API
      • 默认情况下,您可以在开始时将 jsonUser 项初始化为 true,然后您可以根据需要继续更新只读属性
      • 收到响应后,修改userJson.forEach(item =&gt; item.readonly = false);,并根据业务逻辑不断更新userJson
      • 在这种情况下,当添加新行时, item.readonly 将作为 false 应用于新行,因为它在 *ngFor 中。
      【解决方案4】:

      仔细看下面的代码:

          <td> <input class="form-control" type="text" id="userListImage" [(ngModel)]="userList[$index].image"
              name="userListImage{{$index}}"  [disabled]="data.readonly"/>
          </td>
          <td> <input class="form-control" type="text" id="userListKeywords" [(ngModel)]="userList[$index].keywords"
              name="userListKeywords{{$index}}" [disabled]="data.readonly"/></td>
        </tr>
       </tbody>
      

      并且在每个索引的数组中必须有一个名为 readonly 的布尔值,当它为 true 时,输入字段将被禁用(不可编辑),否则将被启用,

      jsonUser = [
      {
      name: 'abc',
      age: 17,
      url: "a.com",
      gender: "F",
      image: "i-ocion.png",
      keywords: "blah",
      readonly:true
      }, 
      {
      name: 'cde',
      age: 18,
      url: "cde.com",
      gender: "F",
      image: "i-oddcion.png",
      keywords: "blahhh",
      readonly:false
      }, 
      {
      name: 'efg',
      age: 19,
      url: "efg.com",
      gender: "F",
      image: "i-ocfffion.png",
      keywords: "blahhhhhhh",
      readonly:true
      }
      ]
      

      索引 1 和 3 将被禁用

      【讨论】:

      • 就像我提到的,我不希望对数组进行任何更改。我从后端收到 JSON,我无法更改 API。我正在寻找 UI 方法
      • 你知道哪些索引将被禁用,你将如何检查哪些输入应该被禁用,哪些输入应该被禁用
      • 请检查接受的答案。这就是我解决问题的方法。
      猜你喜欢
      • 2019-11-21
      • 2023-03-08
      • 2012-09-27
      • 1970-01-01
      • 1970-01-01
      • 2020-10-08
      • 2019-05-22
      • 2019-08-17
      • 2021-10-19
      相关资源
      最近更新 更多