最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Angular conditional readonlydisable on input fields - Stack Overflow

programmeradmin3浏览0评论

I have a table with input fields which I am populating with model values, I want to apply readonly/disable to these populated fields. when I click on add row, I add empty rows to the table. The empty rows added to the table must be editable. I am unable to find a logic which applies readOnly/disable only to already populated table cells.

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>

Component:

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

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

 userList: userModel[] = [];

jsonUser = [
{
name: 'abc',
age: 17,
url: "a",
gender: "F",
image: "i-ocion.png",
keywords: "blah"
}, 
{
name: 'cde',
age: 18,
url: "cde",
gender: "F",
image: "i-oddcion.png",
keywords: "blahhh"
}, 
{
name: 'efg',
age: 19,
url: "efg",
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

I have a table with input fields which I am populating with model values, I want to apply readonly/disable to these populated fields. when I click on add row, I add empty rows to the table. The empty rows added to the table must be editable. I am unable to find a logic which applies readOnly/disable only to already populated table cells.

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>

Component:

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

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

 userList: userModel[] = [];

jsonUser = [
{
name: 'abc',
age: 17,
url: "a.",
gender: "F",
image: "i-ocion.png",
keywords: "blah"
}, 
{
name: 'cde',
age: 18,
url: "cde.",
gender: "F",
image: "i-oddcion.png",
keywords: "blahhh"
}, 
{
name: 'efg',
age: 19,
url: "efg.",
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

Share Improve this question edited Oct 10, 2018 at 14:55 User123 asked Oct 9, 2018 at 20:42 User123User123 3732 gold badges10 silver badges23 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 5

You could declare a variable that identifies the size of the array that es from backend (like initialArraySize), then when you add a new row you verify if the index of that row is greater than the initial array size, if its true, you make it editable..

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

The new empty row has an index of $index === userList - 1 since it is the last item of that collection. You can use that condition and apply any attribute you want.

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

[attr.disabled]="true" will make the input non-editable, you can modify your code to make this attribute configurable.


add a property inside the jsonUser object

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

and then

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

Care fully see the following code:

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

and in array in each index there must be a boolean named as readonly and when it is true then the input field will be disabled(uneditable) otherwise it will be enabled,

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

the index 1 and 3 will be disabled

发布评论

评论列表(0)

  1. 暂无评论