2017-01-06 41 views
5

我有一個angular2應用程序與typescript。我正在使用ng2-dragula進行拖放應用程序。Dragula:恢復在ng2-dragula下降

我向需要選用,檢驗一個條件,如果條件爲假恢復的阻力,我從here知道,revertOnSpillrevertOnSpill:true可以把元素回到其第一的位置。

但是,我不知道怎麼可能在ng2-dragula。 我在onDrop中實現它。這裏是代碼

constructor() { 

       dragulaService.drop.subscribe((value) => { 
        this.onDrop(value.slice(1)); 
       }); 

       dragulaService.setOptions('second-bag', { 
        removeOnSpill: true 
       }); 
} 

private onDrop(args) { 
    bla 
    bla 
    bla 
    if(err.status=="404")                
      this.dragulaService.removeModel; 
     // this.dragulaService.cancel; also tried but did not work 
} 

這裏是HTML代碼:

<div id="toPlay" class="playBox roundedBox" [dragula]="'second-bag'"> 
    <img class="w3-animate-top" [src]="sax_path" alt="sax" id="saxsophone"/> 
    <img class="w3-animate-top" [src]="drum_path" alt="drum" id="drum"/> 
</div> 
<div id="scene" [dragula]="'second-bag'"> 

</div> 

的package.json是:

"dependencies": { 
    "dragula": "^3.7.2" 
    }, 
    "peerDependencies": { 
    "@angular/common": "^2.0.0", 
    "@angular/core": "^2.0.0", 
    "@angular/compiler": "^2.0.0", 
    "@angular/forms": "^2.0.0" 
    }, 
    "devDependencies": { 
    "angular-cli": "1.0.0-beta.22-1", 
    "typescript": "2.0.10" 
    } 

問題是,我不知道如何取消下降?

+0

是否使用dragula或NG2-dragula? – Meir

+0

你也有ng2-dragula嗎?如果沒有,他們有一個專門用於angular2的包:https://github.com/valor-software/ng2-dragula – Meir

+0

從來沒有做過,但我認爲'accept'方法(請參閱https:// github .com/bevacqua/dragula)就是你之後的 – Meir

回答

4

有一種叫做財產布爾moves,控制當且僅當一個元素是可移動的或不

this.dragulaService.setOptions('second-bag', { 

     moves: (el, container, handle) =>{ 

          if(YourCondition) 
            //return true; 
          else 
            //return false; 
         })) 
+0

非常感謝你,我從這件事中拉掉了我的頭髮。 – trichetriche

+0

如果api返回false,我該如何取回放置。 我在onDrop()函數中調用api,如果api函數失敗,那麼我想將被丟棄的元素移動到原始容器中。 – hkg328