Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在angular2 nativescript中按顺序从listview行项目中获取所有开关控制值_Angular_Typescript_Nativescript_Angular2 Nativescript - Fatal编程技术网

如何在angular2 nativescript中按顺序从listview行项目中获取所有开关控制值

如何在angular2 nativescript中按顺序从listview行项目中获取所有开关控制值,angular,typescript,nativescript,angular2-nativescript,Angular,Typescript,Nativescript,Angular2 Nativescript,我在点击按钮时使用的是模态对话框。在这种情况下,我在listview中使用开关控件 对于listview中的每一行项目,我需要执行开关控制以获取值 编辑: <ListView [items]="getListData" class="list" height="160"> <ng-template let-item="item" let-myIndex="index"> <GridLayout rows="*" col

我在点击按钮时使用的是模态对话框。在这种情况下,我在listview中使用开关控件

对于listview中的每一行项目,我需要执行开关控制以获取值

编辑:

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


      }

    }
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) {}

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


      }

    }
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) {}

} 
因此,当我在listview行项目中启用开关时,我将索引存储在数组中

发生了什么:

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


      }

    }
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) {}

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

我需要什么:

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


      }

    }
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) {}

} 
当我在listview行项目中单击switch时,它必须获取NewFeed id并将其切换为键和值。所以我用了地图

这样,我将id保存为键,将toggleVal保存为映射中的值。但我无法在映射中添加id和toggleVal。因此我不知道这两个值是否已添加到映射中


如果我们将单个listview行项目切换“n”次,它应该在相同的位置更改。

为什么不按index=value保存它?您将得到一个值数组,如:

[
    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; 
}
public onFirstChecked(参数,索引:编号){
让firstSwitch=args.object;
//无论是否选中,始终分配
Global.newFeedArr[index]=firstSwitch.checked;
}
更新

通过像您一样使用,并在检查或未检查时存储值:

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);
    });
}
公共地图:地图;
构造函数(){
this.map=新映射();
}
public onfirschecked(参数,索引:编号){
让firstSwitch=args.object;
this.map.set(this.newFeedsList[index].id,firstSwitch.checked);
//这是显示当前条目的调试
log('this.map条目为:');
this.map.forEach(函数(值、键){
console.log(key+'='+值);
});
}

这类似吗?我的newFeedArr似乎是这样的:
公共静态newFeedArr:Array=[]我使用这个数组作为全局数组来访问任何地方。在
NewFeed
单独的pojo模型类中,我有id和布尔值。所以我需要执行像[{id=true,id=false,…}]这样的键和值,每当我们在同一个listview行项目上切换控件时,它都应该采用相同的id位置。NewFeed类是如何定义的?你说[{id=true,id=false,…}],不应该[{id=true},{id=false}…]?请查看我编辑的帖子。很明显,我添加了迄今为止尝试的所有内容。我使用map类更新了答案,您将无法使用console.log获得值