首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >角倍拖放(ng2-dragula)

角倍拖放(ng2-dragula)
EN

Stack Overflow用户
提问于 2018-02-12 11:16:30
回答 1查看 3.2K关注 0票数 3

我正在寻找拖放库,它支持多次拖动。但是我找不到角2+。ng2-dragula支持我们想要的东西,除了多次拖放。有人能建议任何库或如何覆盖ng2 dragula来实现同样的目标吗?以下是我们所看到的特点:

  1. 拖到目标容器中的特定位置
  2. 拖动多个项目
  3. 在同一容器内拖曳
  4. 为任何类型的元素工作
  5. 支持复制和移动到目标
EN

回答 1

Stack Overflow用户

发布于 2019-03-29 02:51:16

基于这句话问题:拖放多个项目#50

我已经做了一些改进,它允许多个项目拖放并按选定的项目下降:https://jsfiddle.net/twfcd5hx/1/

关于ng2-dragula:https://stackblitz.com/edit/ng2-dragula-base-i41w6c?file=src/app/app.component.ts

相关代码(请注意,一旦我找到了纯角度代码,我试过用纯角度的方法就会更新,但不会按预期工作):

代码语言:javascript
复制
this.dragulaService.cloned("dnd").subscribe(({ clone, original, cloneType }) => {
      this.mirrorContainer = $('.gu-mirror').first();
      this.mirrorContainer.removeClass('ex-over');
      this.selectedItems = $('.ex-over');
      this.hasMultiple = this.selectedItems.length > 1 || (this.selectedItems.length == 1 && !$(original).hasClass('ex-over'));
      if (this.hasMultiple) {
        $('.gu-transit').addClass('ex-over');
        this.selectedItems = $('.ex-over');
        this.mirrorContainer.empty();
        var height = 0,
          width = 0;
        let temp = this.mirrorContainer;
        this.selectedItems.each(function (index) {
          var item = $(this);
          var mirror = item.clone(true);
          mirror.removeClass('ex-over gu-transit');
          temp.append(mirror);
          temp.css('background-color', 'transparent');
          item.addClass('gu-transit');
          var rect = item[0].getBoundingClientRect();
          height += rect.height;
          width = rect.width;
        });
        this.mirrorContainer = temp;
        this.mirrorContainer.css('height', height + 'px');
      }
    });

您需要在import $ from 'jquery';文件上声明.ts并安装@types/jqueryjquery

解释:

默认情况下,this.mirrorContainer = $('.gu-mirror').first();获取dragula创建的镜像容器

this.mirrorContainer.removeClass('ex-over');多选择项将具有这个类,但我们不希望它出现在镜像中的项目上。

this.selectedItems = $('.ex-over');获得多个选定项。

this.hasMultiple = this.selectedItems.length > 1 || (this.selectedItems.length == 1 && !$(original).hasClass('ex-over'));检查是否选择了多个项,并考虑了从尚未选中的项开始拖动的位置

代码语言:javascript
复制
if (this.hasMultiple) { // we have multiple items selected
    $('.gu-transit').addClass('ex-over'); // edge case: if they started dragging from an unselected item, adds the selected item class
    this.selectedItems = $('.ex-over'); // update list of selected items in case of edge case above
    this.mirrorContainer.empty();  // clear the mirror container, we're going to fill it with clones of our items
    var height = 0,
      width = 0; // will track final dimensions of the mirror container
    let temp = this.mirrorContainer; // used to temporary store this.mirrorContainer since otherwise would return undefined
    this.selectedItems.each(function (index) { // clone the selected items into the mirror container
      var item = $(this); // the item
      var mirror = item.clone(true); // clone the item
      mirror.removeClass('ex-over gu-transit'); // remove the state classes if necessary
      temp.append(mirror); //add the clone to mirror container
      temp.css('background-color', 'transparent');
      item.addClass('gu-transit'); //add drag state class to item
      var rect = item[0].getBoundingClientRect(); // update the dimensions for the mirror container
      height += rect.height;
      width = rect.width;
    });
    this.mirrorContainer = temp; // restore this.mirrorContainer value after updated
    this.mirrorContainer.css('height', height + 'px'); //set final height of mirror container
  }

注意,在下面的代码中,我使用el.remove()删除存储到目标元素中的拖放元素,因为我已经创建了moveback()moveto()函数(相关解释)。

代码语言:javascript
复制
this.dragulaService.drop("dnd")
  .subscribe(({ name, el, target, source, sibling }) => {
    if (source.attributes[2].value === "data" && (target.attributes[3].value === "ct" || target.attributes[2].value === "target")) {
      if (this.target.length > 0) {
        this.placeholdertarget = false;
      }
      if (this.data.length < 1) {
        this.placeholderdata = true;
      }
      if (!this.hasMultiple) {
        let target = document.getElementsByClassName('target');
        for (let i = target.length - 1; i >= 0; i--) {
          if (target[i].className.includes('ex-over') && i >= 0) {
            this.removeClass(target[i], 'ex-over');
          }
        }
      } else {
        el.remove();
        this.moveto();
      }
      for (let i = 0; i < this.target.length; i++) {
        this.target[i].state = false;
      }
      this.challtarget = false;
    } else if (source.attributes[2].value === "target" && (target.attributes[3].value === "cd" || target.attributes[2].value === "data")) {
      if (this.target.length < 1) {
        this.placeholdertarget = true;
      }
      if (this.data.length > 0) {
        this.placeholderdata = false;
      }
      if (!this.hasMultiple) {
        let target = document.getElementsByClassName('data');
        for (let i = target.length - 1; i >= 0; i--) {
          if (target[i].className.includes('ex-over') && i >= 0) {
            this.removeClass(target[i], 'ex-over');
          }
        }
      } else {
        el.remove();
        this.moveback();
      }
      for (let i = 0; i < this.data.length; i++) {
        this.data[i].state = false;
      }
      this.challdata = false;
    } else {
      // console.log('spilled');
      // this.dragulaService.find('dnd').drake.cancel(true);
    }
  });
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48745177

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档