Yan*_*rel 53 forms checkbox json angular2-forms angular
我的问题很简单:我有一个像这样的复选框列表:
<div class="form-group">
<label for="options">Options :</label>
<label *ngFor="#option of options" class="form-control">
<input type="checkbox" name="options" value="option" /> {{option}}
</label>
</div>
Run Code Online (Sandbox Code Playgroud)
我想发送一系列选定的选项,例如:
[option1, option5, option8]如果选择了选项1,5和8.此数组是我想通过HTTP PUT请求发送的JSON的一部分.
谢谢你的帮助!
ccw*_*den 103
这是一个简单的使用方法ngModel(最终Angular 2)
<!-- my.component.html -->
<div class="form-group">
<label for="options">Options:</label>
<div *ngFor="let option of options">
<label>
<input type="checkbox"
name="options"
value="{{option.value}}"
[(ngModel)]="option.checked"/>
{{option.name}}
</label>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
// my.component.ts
@Component({ moduleId:module.id, templateUrl:'my.component.html'})
export class MyComponent {
options = [
{name:'OptionA', value:'1', checked:true},
{name:'OptionB', value:'2', checked:false},
{name:'OptionC', value:'3', checked:true}
]
get selectedOptions() { // right now: ['1','3']
return this.options
.filter(opt => opt.checked)
.map(opt => opt.value)
}
}
Run Code Online (Sandbox Code Playgroud)
Yan*_*rel 30
感谢Gunter,我找到了解决方案!这是我的整个代码,如果它可以帮助任何人:
<div class="form-group">
<label for="options">Options :</label>
<div *ngFor="#option of options; #i = index">
<label>
<input type="checkbox"
name="options"
value="{{option}}"
[checked]="options.indexOf(option) >= 0"
(change)="updateCheckedOptions(option, $event)"/>
{{option}}
</label>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
这是我正在使用的3个对象:
options = ['OptionA', 'OptionB', 'OptionC'];
optionsMap = {
OptionA: false,
OptionB: false,
OptionC: false,
};
optionsChecked = [];
Run Code Online (Sandbox Code Playgroud)
并且有3种有用的方法:
1.发起optionsMap:
initOptionsMap() {
for (var x = 0; x<this.order.options.length; x++) {
this.optionsMap[this.options[x]] = true;
}
}
Run Code Online (Sandbox Code Playgroud)
2.更新optionsMap:
updateCheckedOptions(option, event) {
this.optionsMap[option] = event.target.checked;
}
Run Code Online (Sandbox Code Playgroud)
3.转换optionsMap成optionsChecked并将其存储在options发送POST请求之前:
updateOptions() {
for(var x in this.optionsMap) {
if(this.optionsMap[x]) {
this.optionsChecked.push(x);
}
}
this.options = this.optionsChecked;
this.optionsChecked = [];
}
Run Code Online (Sandbox Code Playgroud)
小智 23
创建一个列表: -
this.xyzlist = [
{
id: 1,
value: 'option1'
},
{
id: 2,
value: 'option2'
}
];
Run Code Online (Sandbox Code Playgroud)
Html: -
<div class="checkbox" *ngFor="let list of xyzlist">
<label>
<input formControlName="interestSectors" type="checkbox" value="{{list.id}}" (change)="onCheckboxChange(list,$event)">{{list.value}}</label>
</div>
Run Code Online (Sandbox Code Playgroud)
那么在它的组成部分ts: -
onCheckboxChange(option, event) {
if(event.target.checked) {
this.checkedList.push(option.id);
} else {
for(var i=0 ; i < this.xyzlist.length; i++) {
if(this.checkedList[i] == option.id) {
this.checkedList.splice(i,1);
}
}
}
console.log(this.checkedList);
}
Run Code Online (Sandbox Code Playgroud)
<input type="checkbox" name="options" value="option" (change)="updateChecked(option, $event)" />
export class MyComponent {
checked: boolean[] = [];
updateChecked(option, event) {
this.checked[option]=event; // or `event.target.value` not sure what this event looks like
}
}
Run Code Online (Sandbox Code Playgroud)
我遇到了同样的问题,现在我有了一个我更喜欢的答案(可能你也是)。我已将每个复选框绑定到一个数组索引。
首先我定义了一个这样的对象:
SelectionStatusOfMutants: any = {};
Run Code Online (Sandbox Code Playgroud)
然后复选框是这样的:
<input *ngFor="let Mutant of Mutants" type="checkbox"
[(ngModel)]="SelectionStatusOfMutants[Mutant.Id]" [value]="Mutant.Id" />
Run Code Online (Sandbox Code Playgroud)
As you know objects in JS are arrays with arbitrary indices. So the result are being fetched so simple:
Count selected ones like this:
let count = 0;
Object.keys(SelectionStatusOfMutants).forEach((item, index) => {
if (SelectionStatusOfMutants[item])
count++;
});
Run Code Online (Sandbox Code Playgroud)
And similar to that fetch selected ones like this:
let selecteds = Object.keys(SelectionStatusOfMutants).filter((item, index) => {
return SelectionStatusOfMutants[item];
});
Run Code Online (Sandbox Code Playgroud)
You see?! Very simple very beautiful. TG.
小智 5
这是一个没有地图、“已检查”属性和 FormControl 的解决方案。
app.component.html:
<div *ngFor="let item of options">
<input type="checkbox"
(change)="onChange($event.target.checked, item)"
[checked]="checked(item)"
>
{{item}}
</div>
Run Code Online (Sandbox Code Playgroud)
app.component.ts:
options = ["1", "2", "3", "4", "5"]
selected = ["1", "2", "5"]
// check if the item are selected
checked(item){
if(this.selected.indexOf(item) != -1){
return true;
}
}
// when checkbox change, add/remove the item from the array
onChange(checked, item){
if(checked){
this.selected.push(item);
} else {
this.selected.splice(this.selected.indexOf(item), 1)
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
134944 次 |
| 最近记录: |