angular2在不同的网格单元格中填充ngFor

时间:2017-04-02 10:30:51

标签: angular

我有一个数组:hobbies=['Reading','Sport','Travelling','Movies','Cooking','Singing'];

现在在模板中我手动填充它:

<div class="my-checkbox">
    <!-- Hobbies -->
    <span style="float: left; width: 100px;"><h5>Hobbies</h5></span>

    <div class="mdl-grid">
        <div class="mdl-cell mdl-cell--4-col">
            <label #checkbox_1 class="mdl-checkbox mdl-js-checkbox" for="checkbox-1">
                <input type="checkbox" id="checkbox-1" name="hobbies" value="sport" 
                class="mdl-checkbox__input" >
                <span class="mdl-checkbox__label">Sports</span>
            </label>
            <label #checkbox_2 class="mdl-checkbox mdl-js-checkbox" for="checkbox-2">
                <input type="checkbox" id="checkbox-2" name="hobbies" value="reading" class="mdl-checkbox__input">
                <span class="mdl-checkbox__label">Reading</span>
            </label>
        </div>
        <div class="mdl-cell mdl-cell--4-col">

            <label #checkbox_3 class="mdl-checkbox mdl-js-checkbox" for="checkbox-3">
                <input type="checkbox" id="checkbox-3" name="hobbies" value="singing"  
                class="mdl-checkbox__input">
                <span class="mdl-checkbox__label">Singing</span>
            </label>
            <label #checkbox_4 class="mdl-checkbox mdl-js-checkbox" for="checkbox-4">
                <input type="checkbox" id="checkbox-4" name="hobbies" value="travelling" 
                class="mdl-checkbox__input">
                <span class="mdl-checkbox__label">Travelling</span>
            </label>
        </div>
        <div class="mdl-cell mdl-cell--4-col">
            <label #checkbox_5 class="mdl-checkbox mdl-js-checkbox" for="checkbox-5">
                <input type="checkbox" id="checkbox-5" name="hobbies" value="movies"  
                class="mdl-checkbox__input">
                <span class="mdl-checkbox__label">Movies</span>
            </label>
            <label #checkbox_6 class="mdl-checkbox mdl-js-checkbox" for="checkbox-6">
                <input type="checkbox" id="checkbox-6" name="hobbies" value="cooking" 
                class="mdl-checkbox__input">
                <span class="mdl-checkbox__label">Cooking</span>
            </label>
        </div>
    </div> 

如下所示:

enter image description here

我想使用ngFor来填充这些复选框,但保持结构完整,即将替换值插入到连续的单元格中,如:

<div class="mdl-grid" *ngFor="let hobby of hobbies">
 <div class="mdl-cell mdl-cell--4-col">
    <label #checkbox_1 class="mdl-checkbox mdl-js-checkbox" for="checkbox-1">
        <input type="checkbox" id="checkbox-1" name="hobbies" value="sport" 
        class="mdl-checkbox__input" >
        <span class="mdl-checkbox__label">{{hobby}}</span>
    </label> 
    <label #checkbox_2 class="mdl-checkbox mdl-js-checkbox" for="checkbox-1">
        <input type="checkbox" id="checkbox-1" name="hobbies" value="sport" 
        class="mdl-checkbox__input" >
        <span class="mdl-checkbox__label">{{hobby}}</span>
    </label>                            
 </div>
</div> 

此外,因为这是mdl-stepper #checkbox_1或者标签也很重要,因为我升级了我的组件中的元素,如下所示:

@ViewChild('checkbox_2') checkbox_2:ElementRef;
@ViewChild('checkbox_3') checkbox_3:ElementRef;
@ViewChild('checkbox_4') checkbox_4:ElementRef;
@ViewChild('checkbox_5') checkbox_5:ElementRef;
@ViewChild('checkbox_6') checkbox_6:ElementRef;

componentHandler.upgradeElement(this.checkbox_1.nativeElement);
componentHandler.upgradeElement(this.checkbox_2.nativeElement);
componentHandler.upgradeElement(this.checkbox_3.nativeElement);
componentHandler.upgradeElement(this.checkbox_4.nativeElement);
componentHandler.upgradeElement(this.checkbox_5.nativeElement);
componentHandler.upgradeElement(this.checkbox_6.nativeElement);

我怎样才能做到这一点?

更新

当我尝试使用上面提到的ngFor循环时,我得到以下内容:

enter image description here

1 个答案:

答案 0 :(得分:4)

您可以创建将数组拆分为2个元素块的管道。它可能看起来像这样:

<强> chunks.pipe.ts

@Pipe({ name: 'chunks' })
export class ChunksPipe implements PipeTransform {
  transform(arr: any, chunkSize: number) {
    return arr.reduce((prev, cur, index) => (index % chunkSize) ? prev : prev.concat([arr.slice(index, index + chunkSize)]), []);
  }
}

然后你可以像这样使用ChunksPipe

<强> component.html

<div class="mdl-grid">
  <div *ngFor="let chunk of hobbies | chunks: 2" class="mdl-cell mdl-cell--4-col">
    <label *ngFor="let hobby of chunk" #checkbox class="mdl-checkbox mdl-js-checkbox">
      <input type="checkbox" name="hobbies" [value]="hobby" class="mdl-checkbox__input">
      <span class="mdl-checkbox__label">{{hobby}}</span>
    </label>
  </div>
</div>

要升级元素,我会通过@ViewChildren

获取元素

<强> component.ts

hobbies = ['Sport', 'Reading', 'Singing', 'Travelling', 'Movies', 'Cooking' ];

@ViewChildren('checkbox') checkboxes: QueryList<ElementRef>;

ngAfterViewInit() {
  this.checkboxes.forEach(cbx => componentHandler.upgradeElement(cbx.nativeElement));
}

P.S。替代方法是使用将在内部执行的指令

<强> Plunker Example

另请参阅 Grid Example