2017-07-16 61 views
0

好的,所以我有一個選中標記表單的列表,點擊它們後,我提交一個事件並將所有選中的框值放入一個列表中,然後將該列表與另一個列表進行比較將匹配檢查值從true更改爲false(使用index)。indexOf比較兩個列表,不匹配確切值

當我點擊加拿大,我的控制檯打印預期的輸出(加拿大是真實的,美國和墨西哥是假的), ,但後來我點擊墨西哥,所以現在加拿大和墨西哥已被選中,我所有的國家都轉向假。

enter image description here

我想知道,如果這是我正在implenting指數的方式有問題,或者是它調用導致此功能的順序?一個複選框應該返回true。

組件:

import { Component, OnInit } from '@angular/core'; 
import { FormGroup, FormControl } from '@angular/forms'; 

@Component({ 
    selector: 'app-geo-drop', 
    templateUrl: './geo-drop.component.html', 
    styleUrls: ['./geo-drop.component.css'] 
}) 

export class GeoDropComponent implements OnInit { 
    selectedItems: any [] = []; 

    places = [ 
     { code: 'US', allow: 'true', name: 'United States', continent: 'North America' }, 
     { code: 'CA', allow: 'true', name: 'Canada', continent: 'North America' }, 
     { code: 'MX', allow: 'false', name: 'Mexico', continent: 'North America' } 
    ]; 

    countriesForm: FormGroup; 

    constructor() { } 

    ngOnInit() { 
     // add checkbox for each country 
     this.countriesForm = new FormGroup({}); 
     for (let i = 0; i < this.places.length; i++) { 
      this.countriesForm.addControl(
       this.places[i].name, new FormControl(false) 
      ) 
     } 
    } 

    allow(selectedPlaces: Array<any>, allPlaces: Array<any>) { 

     allPlaces.filter(function (place) { 
      if (place.name.indexOf(selectedPlaces) > -1) { 
       place.allow = true; 
      } else { 
       place.allow = false; 
      }; 
     }); 
     this.places.forEach(item => { 
      console.log(item.name, item.allow); 
     }); 
    } 

    search(place, event) { 

     let index = this.selectedItems.indexOf(place.name); 
     if (event.target.checked) { 
      if (index === -1) { 
       this.selectedItems.push(place.name); 
       console.log(this.selectedItems); 
      } 
     } else { 
      if (index !== -1) { 
       this.selectedItems.splice(index, 1); 
       console.log(this.selectedItems); 
      } 
     } 
     this.allow(this.selectedItems, this.places); 
    } 
} 

HTML

<div class="geo-list"> 
    <div class="content-box container"> 
     <form [formGroup]="countriesForm"> 
      <div class="place" *ngFor="let place of places"> 
       <input 
        type="checkbox" 
        formControlName="{{place.name}}" 
        (change)="search(place, $event)" 
       > 
       {{ place.name }} | {{ place.code }} | {{ place.continent }} 
      </div> 
     </form> 
    </div> 
</div> 
+0

我開始想,這可能是因爲我的OnInit函數不會從我的地方列表匹配的當前值的複選框。但是,我的允許函數遍歷並處理它,所以它不應該馬上改變值?或者我應該在我的onInit函數中調用允許函數? – FussinHussin

+0

作爲一個方面說明,也許有一種方法可以直接從表單到地點列表傳遞真假值,反之亦然?像,使框檢查=真,並提交回數據。以及使用正確的檢查來初始化複選框。仍在工作,但任何想法,讓我知道 – FussinHussin

+0

更新我添加[checked] =「{{place.allow}}」更新這個複選框列表。這增加了一些甚至怪異的行爲 – FussinHussin

回答

0

我找到了一個簡單的解決方案。儘管我不相信它是最快的,但如果知道更快的方法,請發佈它。此方法直接在每個(更改)上更改該值,然後將複選框綁定到允許的當前值

comp。

export class GeoDropComponent implements OnInit { 

    places = [ 
     { code: 'US', allow: true, name: 'United States', continent: 'North America' }, 
     { code: 'CA', allow: true, name: 'Canada', continent: 'North America' }, 
     { code: 'MX', allow: false, name: 'Mexico', continent: 'North America' } 
    ]; 

    countriesForm: FormGroup; 

    constructor() { } 

    // use ng on destroy to send final copy? 

    ngOnInit() { 
     // add checkbox for each country 
     this.countriesForm = new FormGroup({}); 
     for (let i = 0; i < this.places.length; i++) { 
      this.countriesForm.addControl(
       this.places[i].name, new FormControl(false) 
      ) 
     } 
    } 

    search(place, event) { 
     for (let i = 0; i < this.places.length; i++) { 
      if (this.places[i].name === place.name) { 
       this.places[i].allow === true ? this.places[i].allow = false : this.places[i].allow = true; 
      } 
     } 
     console.log(place.allow); 
     console.log(this.places); 
    } 
} 

HTML

<input 
        type="checkbox" 
        formControlName="{{place.name}}" 
        value="{{place.allow}}" 
        (change)="search(place, $event)" 
        [checked]="place.allow" 
       > 
1

答案就在你自己的調試信息;你問的索引是「加拿大,墨西哥」 由於你們的國家都沒有被稱爲「加拿大,墨西哥」,它們是錯誤的。

您需要遍歷HTML中的所有選定框來修復它。

+0

所以你說過濾器功能不是在這裏使用的?我應該直接使用for循環?我試圖不這樣做,因爲這個列表完成後將會非常大。它需要快速解析,建議? – FussinHussin

+1

@FussinHussin 1.我沒有說明過濾器函數的任何內容,我只是解釋爲什麼你的調試值是錯誤的。您可以在圖片中看到值(2)是'加拿大,墨西哥'而不是'墨西哥',因此在預定義列表中不符合任何其他值。至於你的其他陳述:我對Angular並不熟悉,但如果你很快需要從另一個列表中排除列表,請查看「Except」(過濾掉列表中已出現在其他列表中的任何內容)。 – Chrotenise

+0

right好吧,我只是想弄清楚如何比較加拿大和墨西哥作爲單獨的價值觀。即時消息沒有發現任何東西在谷歌除了。有鏈接? – FussinHussin

相關問題