我正在使用一些nice和simple drag-and-drop library from github,并且我有一个 View ,其中包含3个不同的按钮,每个按钮在同一位置但不在同一时间单击时都会显示一个列表。
由于某种原因,只能对第一个列表进行排序,而对其他两个列表则不能排序...我可以移动对象,但它们不能放下,因此无法更改顺序。
这是我的HTML:
<div>
<!-- list 1 button -->
<button md-button
(click)="showFirstList()"
class="md-primary">List 1
</button>
<!-- list 2 button -->
<button md-button
(click)="showSecondList()"
class="md-primary">List 2
</button>
<!-- list 3 button -->
<button md-button
(click)="ThirdList()"
class="md-primary">List 3
</button>
</div>
<md-content>
<div *ngIf="showingListOne">
<div dnd-sortable-container [dropZones]="['zone-one']" [sortableData]="listOneToDisplay | async">
<div class="list-bg" *ngFor="#item of listOneToDisplay | async; #i = index" dnd-sortable [sortableIndex]="i">
ID: {{item.id}} <p></p> name: {{item.name}}
</div>
</div><br><br>
</div>
<div *ngIf="showingListTwo">
<div dnd-sortable-container [dropZones]="['zone-two']" [sortableData]="listTwoToDisplay | async">
<div class="list-bg" *ngFor="#item of listTwoToDisplay | async; #i = index" dnd-sortable [sortableIndex]="i">
ID: {{item.id}} <p></p> age: {{item.age}}
</div>
</div>
</div>
<div *ngIf="showingListThree">
<div dnd-sortable-container [dropZones]="['zone-three']" [sortableData]="listThreeToDisplay | async">
<div class="list-bg" *ngFor="#item of listThreeToDisplay | async; #i = index" dnd-sortable [sortableIndex]="i">
ID: {{item.id}} <p></p> age: {{item.age}}
</div>
</div>
</div>
</div>
</md-content>
这是我的sorting-lists.component.ts:
@Component({
selector: 'sorting-lists',
styles: [require('./sorting-lists.css')],
directives: [DND_DIRECTIVES],
providers: [DND_PROVIDERS],
template: require('./sorting-lists.component.html')
})
@Injectable()
export class MyCmp implements OnInit {
listOneToDisplay = this._myService.getFirstListData();
listTwoToDisplay = this._myService.getSecondListData();
listThreeToDisplay = this._myService.getThirdListData();
showingListOne = false;
showingListTwo = false;
showingListThree = false;
constructor(private _myService: MyService) {
};
public showFirstList(): void {
this.showingListOne = true;
this.showingListTwo = false;
this.showingListThree = false;
}
public showSecondList(): void {
this.showingListTwo = true;
this.showingListOne = false;
this.showingListThree = false;
}
public showThirdList(): void {
this.showingListThree = true;
this.showingListTwo = false;
this.showingListOne = false;
}
}
如果有人可以帮我弄清楚为什么只有第一个是可排序的,那将是有福的!!
谢谢 :)
最佳答案
EDIT2 :(根据您的问题的新版本):
尝试在应用程序的 bootstrap 中提供DND_PROVIDERS,而不是在组件中提供它。有关更多详细信息,请查看here。
bootstrap(AppComponent, [
DND_PROVIDERS // It is required to have 1 unique instance of your service
]);
您还可以使用ng2-dragula,它是dragula的官方Angular2包装器。
通过npm安装
编辑:要使用ng2-dragular和 angular 2.0.0-beta.15 ,请添加ng2-dragula包和github链接,并带有最后一次提交,然后在
package.json
中切换到RC.1:ng2-dragula": "git://github.com/valor-software/ng2-dragula.git#0cdcd52b1a486609ed4b4a43465b1dac2bb1b007
> npm install
用法
将DragulaService添加到您的组件,并将Dragula添加到您的组件指令。
@Component({
selector: 'sample',
directives: [Dragula],
viewProviders: [DragulaService],
template:`
<div>
<div class='wrapper'>
<div class='container' [dragula]='"first-bag"'>
<div>You can move these elements between these two containers</div>
<div>Moving them anywhere else isn't quite possible</div>
<div>There's also the possibility of moving elements around in the same container, changing their position</div>
</div>
<div class='container' [dragula]='"first-bag"'>
<div>This is the default use case. You only need to specify the containers you want to use</div>
<div>More interactive use cases lie ahead</div>
<div>Make sure to check out the <a href='https://github.com/bevacqua/dragula#readme'>documentation on GitHub!</a></div>
</div>
</div>
</div>
`
})
class Sample {}