2017-07-30 7 views
2

ボタンをクリックするとモーダルダイアログが表示されます。その中で、私はリストビューでスイッチコントロールを使用しています。リストビューの項目からすべてのスイッチ制御値をangle2の順序で取得する方法nativescript

listviewのすべての行項目について、値を取得するためにスイッチ制御を実行する必要があります。

編集:

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]が表示されています。私は必要なもの

私はリストビューの列項目にスイッチをクリックしている時、それはキーと値としての新しいフィードのIDとtoggleValを取得する必要があります。私はマップを使用しました。

私はidとしてkeyとtoggleValをmapに保存しています。しかし、idとtoggleValをmap.soに追加することができません。その2つの値がマップに追加されたかどうかはわかりません。

単一リストビューの行アイテムを「n」回切り替えた場合、同じ位置に変更する必要があります。

+0

これは似たようなものですか? https://stackoverflow.com/questions/45198044/databinding-from-within-a-template-in-angular/45199711 – ishandutta2007

答えて

1

なぜインデックス=値で保存しないのですか?あなたは次のように値の配列を取得します:

[ 
    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 classをか使用して、そしてそれはだ時はいつでも値を格納することでチェックされているかどうか:

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); 
    }); 
} 
+0

私のnewFeedArrは次のようです: 'public static newFeedArr:Array < NewFeed> = []; '私はこの配列をどこでもグローバルに使用しています。 'NewFeed'分離pojoモデルクラスでは、私はIDとブール値を持っています。だから私は[{id = true、id = false、..}のようなキーと値を実行する必要があります。同じリストビューの行項目でコントロールを切り替えるたびに毎回同じID位置をとる必要があります。 – Steve

+0

NewFeedクラスはどのように定義されていますか?あなたは[{id = true、id = false、..}]と言って、[{id = true}、{id = false} ...]しないでください。 – Fetrarij

+0

私の編集したポストをチェックしてください。私は明らかにこれまでに試したことをすべて加えました。 – Steve

関連する問題