実現したいこと
-
- 項目を並べ替えるボタンを追加する (↑↓ボタンをクリックで、項目が前後に移動する)
仮説
- 配列の値を取得→条件式→移動
コード
html
1 2 3<h1>Todo App</h1> 4<form [formGroup]="rForm"> 5 <div class="form-group registItemArea"> 6 <div class="input-group"> 7 <input id="title" class="form-control" placeholder="Title" formControlName="title" /> 8 <span class="input-group-btn"> 9 <button class="btn btn-default" type="button" (click)="hasDetail = !hasDetail">詳細</button> 10 </span> 11 </div> 12 </div> 13 <div class="card card-block card-header" (click)="hasDetail = !hasDetail"> 14 <div class="form-group"> 15 <label for="description"> 16 <strong>詳細:</strong> 17 </label> 18 <textarea id="text" class="form-control" formControlName="description"></textarea> 19 </div> 20 21 <div class="form-group"> 22 <label for="date"> 23 <strong>日付:</strong> 24 </label> 25 <input type="date" class="form-control" formControlName="date" /> 26 </div> 27 </div> 28</form> 29<div class=" row "> 30 <div class="col-sm-2 "> 31 <button class="createBtn btn btn-success " [disabled]="this.rForm.invalid" (click)="onSaveTodoItem()">作成</button> 32 </div> 33</div> 34<hr> 35<div class="row itemListArea "> 36 <div class="col-sm-12"> 37 <li *ngFor="let item of itemList; let i=index;"> 38 <div accordion-heading [class.isComplete]="item.isComplete"> 39 <button class="btn btn-danger" (click)="onUpItem(i)">↑</button> 40 <button class="btn btn-danger" (click)="onDownItem(i)">↓</button> 41 {{item.title}} 42 </div> 43 <div> 44 <strong>詳細:</strong> 45 <pre>{{item.description ? item.description : "-"}}</pre> 46 </div> 47 <div> 48 <strong>日付:</strong> 49 <span>{{item.date ? item.date : "-"}}</span> 50 </div> 51 <div> 52 <button class="btn btn-danger" (click)="onDeleteItem(i)">削除</button> 53 <button class="btn btn-success" (click)="item.isComplete = !item.isComplete ">{{item.isComplete ? "未完了に戻す" : "完了にする"}}</button> 54 55 </div> 56 </li> 57 </div> 58</div>
ts
1import { Component, OnInit } from '@angular/core'; 2import { FormGroup, FormControl } from '@angular/forms'; 3import { FormBuilder } from '@angular/forms'; 4import { Validators } from '@angular/forms'; 5import { FormArray } from '@angular/forms'; 6 7interface TodoItem { 8 title: string; 9 description: string; 10 isComplete: boolean; 11 date: any; 12} 13 14@Component({ 15 selector: 'app-form-practice', 16 templateUrl: './form-practice.component.html', 17 styleUrls: ['./form-practice.component.scss'] 18}) 19export class FormPracticeComponent implements OnInit { 20 21 // 詳細が表示されているか 22 public hasDetail = false; 23 // アイテムリスト 24 public itemList: Array<TodoItem> = new Array<TodoItem>(); 25 26 rForm: FormGroup; 27 constructor(protected fb: FormBuilder) { 28 } 29 ngOnInit() { 30 this.createForm(); 31} 32 createForm(): void { 33 // Form の作成と初期値設定をします。 34 this.rForm = this.fb.group({ 35 title: ['', 36 [ 37 Validators.required 38 ] 39 ], 40 description: [''], 41 date: [''], 42 isComplete: [false] 43 }); 44 } 45 46 // todoItem を 保存します 47 onSaveTodoItem(): void { 48 const item: TodoItem = { 49 title: this.rForm.get('title').value, 50 isComplete: false, 51 description: null, 52 date: null 53 }; 54 55 if (this.hasDetail) { 56 item.description = this.rForm.get('description').value; 57 item.date = this.rForm.get('date').value; 58 } 59 60 this.itemList.push(item); 61 this.clearForm(); 62 console.log(this.itemList); 63 } 64 65 // フォームの値をリセット 66 clearForm(): void { 67 this.rForm.reset(); 68 } 69 70 // 指定した要素を削除 71 onDeleteItem(index: number): void { 72 this.itemList.splice(index, 1); 73 } 74 75// 指定した要素を上に移動 76 onUpItem(index: number): void { 77 this.itemList.splice(0, index); 78 } 79// 指定した要素を下に移動 80 onDownItem(index: number): void { 81 this.itemList.splice(-1, index); 82 } 83}
回答1件
あなたの回答
tips
プレビュー