如何在 Angular2 本机脚本中按顺序从列表视图行项获取所有开关控制值



单击按钮时我正在使用模态对话框。在这种情况下,我在列表视图中使用切换控制。

对于列表视图中的每个行项,我需要执行切换控制才能获取值。

编辑:

app.modal.html:

<ListView  [items]="getListData" class="list" height="160">
<ng-template let-item="item" let-myIndex="index">
<GridLayout rows="*" columns="1.5*, auto, auto">
<Label row="0" col="0" class="item-name" [text]="item.category" ></Label>
<Switch row="0" col="1" [checked]="item.toggleVal" (checkedChange)="onFirstChecked($event, myIndex)"></Switch>
<Image row="0" col="2" src="res://menu_alert" stretch="none"></Image>
</GridLayout>
</ng-template>
</ListView>

app.modal.ts 文件:

public map: Map<number, boolean>;
public newFeedsList: Array<NewFeed> = [];
public constructor(private params: ModalDialogParams) {

this.map = new Map<number, boolean>();

}
public onFirstChecked(args, index: number) {
let firstSwitch = <Switch>args.object;
if(index != null && firstSwitch.checked){
this.map.set(this.newFeedsList[index].id , firstSwitch.checked);

console.log("Map :",this.map);
console.log("Map :", JSON.stringify(this.map));

} else {

}
}

NewFeed.ts:

export class NewFeed {
constructor(public id: number,public toggleVal : string, public title: string, public description:string, public date:string,public category:string, public imageUrl:string,public iconName:string) {}
} 

因此,当我在列表视图行项目中启用开关时,我将索引存储在 Array 中。

发生了什么事情:

现在我无法在控制台中打印哈希映射。它在控制台中显示Map:[object Map]

我需要什么:

当我在列表视图行项目中单击开关时,它必须获取 NewFeed ID 并将切换瓦尔作为键和值。所以我用了地图。

在这种情况下,我将 id 保存为键并将 RangegleVal 切换为映射中的值。但是我无法添加 id 和切换瓦尔 map.so 我不知道这两个值是否添加到地图中。

如果我们切换单个列表视图行项目"n"次,它应该在相同的位置更改。

为什么不按索引=值保存它? 您将获得一个值数组,例如:

[
true,  //index 0 is checked
false, //index 1 is unchecked
true,  //index 2 is checked
...    //globally index n is value
]

并像这样构建它:

public onFirstChecked(args, index: number) {
let firstSwitch = <Switch>args.object; 
//always assign whenever it is checked or not
Global.newFeedArr[index] = firstSwitch.checked; 
}

更新

通过像您一样使用 Map 类,并在选中或不选中时存储值:

public map: Map<number, boolean>;
constructor() {
this.map = new Map<number, boolean>();
}
public onFirstChecked(args, index: number) {
let firstSwitch = <Switch>args.object;
this.map.set(this. newFeedsList[index].id, firstSwitch.checked);
//this is a debug to display current entries
console.log('this.map entries are:');
this.map.forEach(function (value, key) {
console.log(key + ' = ' + value);
});
}

最新更新