美文网首页
angualr实现鼠标拖拽排序功能

angualr实现鼠标拖拽排序功能

作者: 4ea0af17fd67 | 来源:发表于2018-08-28 15:49 被阅读425次

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

相关文章

  • angualr实现鼠标拖拽排序功能

    angualr2以上版本我使用的是angualr6.x最新版 ng2-dragula https://github...

  • js实现拖拽

    ①鼠标按下+鼠标移动 → 拖拽②鼠标松开 → 无拖拽③鼠标偏移 → 拖拽距离 js实现 ① onmousedown...

  • POS-2017

    拖拽排序功能 实现方法: 使用jquery的Sortable功能可以实现拖拽功能 index页面 html部分 商...

  • DOM拖拽排序(JavaScript)

    实现拖拽排序功能,废话少说,上代码 html部分 js部分 效果如下

  • react.js 拖拽

    react.js拖拽排序功能的实现 1.使用 react-dnd npm install--save react-...

  • 拖拽,碰撞检测

    1. 拖拽 1.1 拖拽原理 鼠标拖拽效果的实现,就是在鼠标摁下和移动的时候,修改元素的定位值的效果。 1.1.1...

  • selenium常用元素操作(二)

    一、鼠标操作 selenium的ActionChains类实现鼠标的移动右键,鼠标悬停,拖拽,双击等模拟鼠标的操作...

  • Angular cdk 学习之 drag-drop

    Angualr drag-drop里面的功能能让我们非常方便的处理页面上视图的拖拽(自由拖拽、列表排序拖拽、...

  • vue拖拽元素

    实现目的 代码 实现鼠标按住title可以拖拽set模块。 drag.js 参数e是鼠标点击event,参数a是需...

  • 鼠标指针实现拖拽

    实现思路: 移动对象绝对定位 计算移动距离 移动对象跟随鼠标 添加 JS 监听按下松开 需要补充的知识:docum...

网友评论

      本文标题:angualr实现鼠标拖拽排序功能

      本文链接:https://www.haomeiwen.com/subject/haqqwftx.html