<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          在Angular7中如何創(chuàng)建拖放列表?

          共 2269字,需瀏覽 5分鐘

           ·

          2021-03-25 21:15

          我們可以使用angular Component Development Kit(CDK)提供的Drag-Drop模塊輕松創(chuàng)建待辦事項列表。

          首先,使用以下命令創(chuàng)建角度分量:

           ng g c To-Do

          現(xiàn)在,從@ angular / cdk / drag-drop導入CdkDragDrop,moveItemInArray,transferArrayItem到我們的待辦事項組件,

          為組件視圖編寫代碼:
          創(chuàng)建兩個分區(qū),一個分區(qū)用于“待完成”項目,另一個分區(qū)用于“已完成”項目。
          參數(shù)如下:

          1. cdkDropList:它是一個放置容器。

          2. cdkDropListData:它將數(shù)據(jù)綁定到視圖。

          3. cdkDropListConnectedTo:獲取當前分區(qū)連接到的另一個放置容器的ID。

          4. cdkDropListDropped:拖動項目后,必須更新數(shù)據(jù)模型。

          5. cdkDrag:它指定可以拖動的項目。

          例子:

          <div> <!-- container for both list  -->  <h1>To do</h1> <!-- To-Do list  -->  <div    cdkDropList     #todoList="cdkDropList"    [cdkDropListConnectedTo]="[doneList]"     [cdkDropListData]="todo"     (cdkDropListDropped)="drag($event)">     <div *ngFor="let item of todo" cdkDrag>{{item}}</div>   </div> </div> <div>   <h1>Done</h1> <!-- Done list  -->  <div    cdkDropList     #doneList="cdkDropList"    [cdkDropListConnectedTo]="[todoList]"     [cdkDropListData]="done"     class="example-list"    (cdkDropListDropped)="drag($event)">     <div *ngFor="let item of done" cdkDrag>{{item}}</div>   </div> </div> 
          現(xiàn)在,編寫用于偵聽事件和添加數(shù)據(jù)的代碼。
          在這里,我們使用了一個硬編碼的列表,但是你始終可以使用ngmodel指令進行輸入。
          有兩種可能性:
          1. 將項目拖到同一容器中:使用moveItemInArray將其移動到同一容器中
          2. 將項目拖到另一個容器:使用transferArrayItem移到另一個容器中。
          export class To-Do { 
          // hardcoded lists
          todo = [ 'Go to gym', 'Eat lunch', 'Take a nap', 'Physics syllabus' ];
          done = [ 'Assignment', 'Coding practice', 'Maths syllabus', 'English syllabus' ];
          //function for listening to the event
          drag(event: CdkDragDrop<string[]>) {
          //if movement if within the same container
          if (event.previousContainer === event.container) { moveItemInArray( event.container.data, event.previousIndex, event.currentIndex); }
          //if movement if to other containers
          else { transferArrayItem(event.previousContainer.data, event.container.data, event.previousIndex, event.currentIndex); } } }
          最終輸出效果如下:


          我們成功的將“Eat lunch”從“To do”列表拖到“Done”列表。


          本文完~


          瀏覽 29
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  国产欧美一区二区三区在线看蜜臀 | 欧美成人版H性爽997777 | 青青草成人视频在线 | 成人影视久久久无码三区 | 无码一道本 |