简体   繁体   English

使用拖放更改列表项的顺序

[英]Change order of list items using drag and drop

I have used the following html and ts for drag and drop list items from one div to another div. 我使用以下html和ts将列表项从一个div拖放到另一个div。

Html: HTML:

<div class="listArea">
   <h4> Drag and Drop list in Green Area: </h4>
   <ul class="unstyle">
      <li id="drag1" draggable="true" (dragstart)="drag($event)">
          i am list 1
      </li>
      <li id="drag2" draggable="true" (dragstart)="drag($event)">
          i am list 2 
      </li>
      <li id="drag3" draggable="true" (dragstart)="drag($event)">
          i am list 3
      </li>
      <li id="drag4" draggable="true" (dragstart)="drag($event)">
          i am list 4
      </li>
   </ul>
</div>
<div class="buildArea" id="div1" (drop)="drop($event)" (dragover)="allowDrop($event)">
   <h4> Drop Here </h4> 
</div>

And Typescript contains the following: 并且Typescript包含以下内容:

  allowDrop(ev) {
    ev.preventDefault();
  }

  drag(ev) {
    ev.dataTransfer.setData("text", ev.target.id);
  }

  drop(ev) {
    ev.preventDefault();
    var data = ev.dataTransfer.getData("text");
    ev.target.appendChild(document.getElementById(data).cloneNode(true));
  }

Its from Html5 drag and drop, and i am able to drag and drop the list items from the div with class listArea to the div with class buildArea. 它从Html5拖放,我能够将列表项从具有listArea类的div拖放到具有buildArea类的div中。

After dropped the list items user may change the order of the list items that was dropped in buildArea div. 删除列表项后,用户可以更改在buildArea div中删除的列表项的顺序。

For eg, the given order I am list 1,2,3,4,5 may change to any order like 2,4,5,3,1 or anything user wants. 例如,给定的“我是清单1,2,3,4,5”订单可以更改为任何订单,例如2,4,5,3,1或用户想要的任何订单。

As of now i have used only angular and typescript and no third party plugin was added and i am also need the result without any third party or jquery. 到目前为止,我只使用了angular和typescript,没有添加任何第三方插件,并且我也需要没有任何第三方或jquery的结果。

I have also have look around for this but unable to get a correct solution and hence kindly help me to achieve the change of order in dropped list. 我也有此问题,但无法获得正确的解决方案,因此请帮助我完成删除列表中的订单更改。

The stackblitz link was, https://stackblitz.com/edit/angular-wyimor stackblitz链接为https://stackblitz.com/edit/angular-wyimor

In your drop area, bind list items to an array. 在放置区域中,将列表项绑定到数组。

<div class="buildArea" id="div1" (drop)="drop($event)" (dragover)="allowDrop($event)">
    <h4> Drop Here </h4>
    <ul>
        <li *ngFor="let item of targetItems [id]="item.id">{{item.text}}</li>
    </ul>
</div>

In component class: 在组件类中:

targetItems: Array<TargetItem> = [];
....
drop(ev) {
    ev.preventDefault();
    let id = ev.dataTransfer.getData("text");
    let text = document.getElementById(id).innerText;
    this.targetItems.push({id: id, text: text});
    this.targetItems.sort((a, b) => {
        // Implement your own sorting logic
        // e.g., return a.id - b.id;
    });
}

Define an interface for target item: 定义目标项目的接口:

interface TargetItem {
    text: string;
    id: number;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM