angular
  • typescript
  • ng2-dragula
  • 2017-04-19 81 views 1 likes 
    1

    我想拖放一個副本與ng2 dragula 這是我的模板。Dragula拖放一種方式與副本ng2

    `<div> 
        <div class='wrapper'> 
        <div class='container' id='no-drop' [dragula]='"first-bag"'> 
         <div>Drag/drop item 1</div> 
        </div> 
        <div class='container' [dragula]='"first-bag"'> 
         <div>Drag/drop item 2</div> 
        </div> 
        </div> 
    </div>` 
    

    我已經設置了選項來複制我的組件。

    constructor(private dragulaService: DragulaService) { 
    dragulaService.setOptions('first-bag', { 
        copy: true 
    }); 
    

    但是,如果我設置爲false,我無法完全拖動。我怎樣才能從左到右,而不是其他方式。

    回答

    2

    發現後不久我找到了答案!

    constructor(private dragulaService: DragulaService) { 
        dragulaService.setOptions('first-bag', { 
         copy: true, 
         moves: function (el, container, handle) { 
         return container.id !== 'no-drop'; 
         } 
        }); 
    
    0

    我更喜歡使用接受函數,而不是使用移動函數。

    因爲使用移動功能,您可以停止從容器移動項目。接受函數決定目標容器是否有效。

    accepts: function (el, target, source, sibling) { 
           // your condition 
          }, 
    
    0

    默認情況下,dragula將允許用戶在任何容器的拖動元件和在列表中的任何其它容器掉落。如果元素被放置在不是其中一個容器的位置,則根據revertOnSpill和removeOnSpill選項將優先取消該事件。

    下面的例子允許用戶從左到右,從右到左拖動元素。 在HomePage.component.html

    <div class="wrapper"> <div class="container master" [dragula]="'editor-bag'" [dragulaModel]="q1"> 
    
         <div *ngFor="let item of q1" class="box"> 
         {{item}} 
         </div> 
    </div> 
    <div class="container" [dragula]="'editor-bag'"> 
    </div> 
    

    接下來創建代碼,創建HomePageComponent.ts。此外,還有需要設置接受的方法具有以下特徵:(EL,目標,源,兄弟姐妹)

    import { DragulaService, DragulaDirective } from 'ng2-dragula/ng2-dragula'; 
    import { Router, Route, ActivatedRoute } from '@angular/router'; 
    import { Component, OnInit } from '@angular/core'; 
    
    @Component({ 
        selector: 'app-home-page', 
        templateUrl: './home-page.component.html', 
        styleUrls: ['./home-page.component.css'], 
    }) 
    export class HomePageComponent implements OnInit { 
        q1 = []; 
        q2 = []; 
        static _debug: boolean = false; 
        _debug: boolean = HomePageComponent._debug; 
        constructor(private dragulaService: DragulaService, private router: Router, private route: ActivatedRoute) { 
        for (var i = 0; i < 10; i++) { 
         this.q1.push("1. <" + i + ">"); 
         //this.q2.push("2. <" + i + ">"); 
        } 
    
         dragulaService.setOptions('editor-bag', {  
         accepts: function (el, target, source, sibling) { 
         var fn_debug = true; 
         var acceptAll = false; 
    
          if (this._debug || fn_debug) { 
          console.log("accepts() start el, target, source, sibling"); 
          console.log({ el, target, source, sibling }); 
          } 
          if (target.classList.contains('master')) { 
          return false; 
          } 
          if (sibling == null) { 
          return (target.children.length == 0); 
          } 
          var name: string = el.innerText; 
          return false; 
         }, 
    
         direction: 'vertical',    // Y axis is considered when determining where an element would be dropped 
         copy: function (el, source) { 
         if (this._debug) { 
          console.log("copy() start"); 
          console.log(el); 
          console.log(source); 
          console.log("copy() stop"); 
         } 
         return source.classList.contains('master'); 
         },      // elements are moved by default, not copied 
         copySortSource: false,    // elements in copy-source containers can be reordered 
         revertOnSpill: false,    // spilling will put the element back where it was dragged from, if this is true 
         removeOnSpill: true,    // spilling will `.remove` the element, if this is true 
         mirrorContainer: document.body, // set the element that gets mirror elements appended 
         ignoreInputTextSelection: true  // allows users to select input text, see details below 
        }) 
        } 
        ngOnInit() { 
    
         this.dragulaService.drag.subscribe((value: any) => { 
         if (this._debug) { 
          console.log("drag start"); 
          console.log(value); 
          console.log("drag stop"); 
          console.log(`drag: ${value[0]}`); 
         } 
         // this.onDrag(value.slice(1)); 
         }); 
    
         this.dragulaService.drop.subscribe((value: any) => { 
         console.log(`drop: ${value[0]}`); 
         //this.onDrop(value.slice(1)); 
         }); 
    
         this.dragulaService.over.subscribe((value: any) => { 
         if (this._debug) { console.log(`over: ${value[0]}`); } 
         // this.onOver(value.slice(1)); 
         }); 
    
         this.dragulaService.out.subscribe((value: any) => { 
         if (this._debug) { console.log(`out: ${value[0]}`); } 
         //this.onOut(value.slice(1)); 
         }); 
        } 
    
    } 
    

    我張貼我的解決方案,它也可以幫助別人。

    相關問題