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

角度条件只读/禁用输入字段

  •  0
  • User123  · 技术社区  · 6 年前

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

    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

    4 回复  |  直到 6 年前
        1
  •  4
  •   Dan Atkinson    3 年前

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

    <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
  •  0
  •   DrNio    6 年前

    新空行的索引为 $index === userList - 1

        3
  •  0
  •   Vivek Kumar    6 年前
    <td>
    <input
        class="form-control"
        type="text"
        id="userListKeywords"
        [(ngModel)]="userList[$index].keywords"
        name="userListKeywords{{$index}}"
        [attr.disabled]="true" readonly/>
    </td>
    

    [attr.disabled]=“真”


    在中添加属性 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>
    

        4
  •  0
  •   Touqeer Aslam    6 年前

    请仔细查看以下代码:

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

    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将被禁用