[英]Angular conditional readonly/disable on input fields
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 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.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;
}
You could declare a variable that identifies the size of the array that comes 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..您可以声明一个变量来标识来自后端的数组大小(如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>
The new empty row has an index of $index === userList - 1
since it is the last item of that collection.新的空行的索引为
$index === userList - 1
因为它是该集合的最后一项。 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. [attr.disabled]="true"将使输入不可编辑,您可以修改代码以使此属性可配置。
add a property inside the jsonUser
object在
jsonUser
对象中添加一个属性
jsonUser = [
{
name: 'abc',
age: 17,
url: "a.com",
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" [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,并且在每个索引的数组中,必须有一个名为 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
}
]
the index 1 and 3 will be disabled索引 1 和 3 将被禁用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.