angualr实现鼠标拖拽排序功能

简介: angualr2以上版本我使用的是angualr6.x最新版ng2-dragulahttps://github.com/valor-software/ng2-dragula1.

angualr2以上版本
我使用的是angualr6.x最新版

ng2-dragula

https://github.com/valor-software/ng2-dragula

1.安装依赖
npm install ng2-dragula
# or
yarn add ng2-dragula

2.添加这一行到你的 polyfills.ts:
(window as any).global = window;

3.引入模块    DragulaModule.forRoot() 如下:
import { DragulaModule } from 'ng2-dragula';
@NgModule({
  imports: [
    ...,
    DragulaModule.forRoot()
  ],
})
export class AppModule { }

4.引入公用css node_modules/dragula/dist/dragula.css
或者直接复制所有css放styles.scss文件

5.使用指令,给一个自定义名称,随意的字符串就行,标识一个div的内容可以拖拽,dragula="div1"和 [dragula]="Vampires"意义等同 代码如下
<ul dragula="div1">
  <li>Dracula</li>
  <li>Kurz</li>
  <li>Vladislav</li>
  <li>Deacon</li>
</ul>

6.多个div需要拖拽如下
<div dragula="div1">

</div>
<div dragula="div2">

</div>
<div dragula="div3">

</div>
7. 如果需要双向绑定拖拽的数据 [(dragulaModel)]

 [(dragulaModel)]等同于 [dragulaModel]="vampires" (dragulaModelChange)="vampires = $event" 类似ng自带的[(ngModel)]

<ul dragula="VAMPIRES" [(dragulaModel)]="vampires">
  <li *ngFor="let vamp of vampires">
    {{ vamp.name }} likes {{ vamp.favouriteColor }}
  </li>
</ul>
等同于
<ul dragula="VAMPIRES" [dragulaModel]="vampires" (dragulaModelChange)="vampires = $event">
  ...
</ul>

8.拖拽过程中的事件订阅
import { Subscription } from 'rxjs';
import { DragulaService } from 'ng2-dragula';

export class MyComponent {
  // RxJS Subscription is an excellent API for managing many unsubscribe calls.
  // See note below about unsubscribing.
  subs = new Subscription();

  constructor(private dragulaService: DragulaService) {

    // These will get events limited to the VAMPIRES group.

    this.subs.add(this.dragulaService.drag("VAMPIRES")
      .subscribe(({ name, el, source }) => {
        // ...
      })
    );
    this.subs.add(this.dragulaService.drop("VAMPIRES")
      .subscribe(({ name, el, target, source, sibling }) => {
        // ...
      })
    );
    // some events have lots of properties, just pick the ones you need
    this.subs.add(this.dragulaService.dropModel("VAMPIRES")
      // WHOA
      // .subscribe(({ name, el, target, source, sibling, sourceModel, targetModel, item }) => {
      .subscribe(({ sourceModel, targetModel, item }) => {
        // ...
      })
    );

    // You can also get all events, not limited to a particular group
    this.subs.add(this.dragulaService.drop()
      .subscribe(({ name, el, target, source, sibling }) => {
        // ...
      })
    );
  }

  ngOnDestroy() {
    // destroy all the subscriptions at once
    this.subs.unsubscribe();
  }
}

还有一个非常棒的插件可以看看
https://github.com/xieziyu/angular2-draggable

相关文章
|
10天前
|
JavaScript
【sgExcelGrid】自定义组件:简单模拟Excel表格拖拽、选中单元格、横行、纵列、拖拽圈选等操作
【sgExcelGrid】自定义组件:简单模拟Excel表格拖拽、选中单元格、横行、纵列、拖拽圈选等操作
【sgExcelGrid】自定义组件:简单模拟Excel表格拖拽、选中单元格、横行、纵列、拖拽圈选等操作
|
10月前
|
JSON 小程序 JavaScript
小程序地图插入图标后 怎么实现点击图标弹出窗口
小程序地图插入图标后 怎么实现点击图标弹出窗口
70 0
|
11月前
|
编解码
【PyAutoGUI操作指南】02 鼠标控制功能+获取当前坐标+鼠标事件+鼠标滚动查询
左上角的像素位于坐标0,0。如果屏幕分辨率为1920 x 1080,则右下角的像素将为1919,1079(因为坐标从0开始,而不是1)。
346 0
QT 事件过滤器实现鼠标悬浮时两个按钮背景互换
QT 事件过滤器实现鼠标悬浮时两个按钮背景互换
QT 事件过滤器实现鼠标悬浮时两个按钮背景互换
拖拽排序-网格布局
拖拽排序-网格布局
191 0
拖拽排序-网格布局
C#鼠标拖拽,移动图片实例
最近工作需要做一个鼠标可以拖拽移动图片的功能。写了几个基本功能,勉强能用。
399 0
C#鼠标拖拽,移动图片实例
鼠标拖拽翻页效果
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/w178191520/article/details/84192737 ...
971 0