【发布时间】:2015-10-09 04:38:09
【问题描述】:
我正在为我的 AngularJS 项目使用 angular-drag-and-drop-lists (https://github.com/marceljuenemann/angular-drag-and-drop-lists) 来创建两个允许我执行以下操作的列表:
- 将项目从列表 A 拖到列表 B
- 将项目从列表 B 拖到列表 A
- 对列表 A 中的项目重新排序
- 重新排序列表 B 中的项目
使用图书馆网站 (http://marceljuenemann.github.io/angular-drag-and-drop-lists/demo/#/simple) 上的一个简单示例,我可以看到这四个条件很容易实现。然而,当我想引入稍微复杂一点的行为时,事情就开始变得棘手了:
- 当将项目从列表 A 拖到列表 B 时,我希望发生一些事情(通过 dnd-drop 回调很容易实现。这目前适用于我
- 当将项目从列表 B 拖到列表 A 时,我不希望发生除常规放置之外的任何事情(即项目在列表 A 中结束并且没有任何花哨的事情发生)。 目前对我有用
- 对列表 A 中的项目进行重新排序时,将项目重新放入列表中的新位置时不会发生任何事情(即使它被放回原来的位置)这是我遇到问题的地方- 更多解释见下文
- 对列表 B 中的项目进行重新排序时,将项目重新放入列表中的新位置时不会发生任何事情(即使它被放回其原始位置)。 目前对我有用
我在很大程度上调整了上面提供的示例链接中使用的示例代码。我遇到的问题是,当我将列表 A 中的项目重新排序时,我想要执行的操作也发生在列表 A 中。
我目前的设置/伪代码如下:
我的清单:
$scope.models.lists = {
"A": [],
"B": []
}
我用从数据库中提取的信息填充这些列表。列表中的每个项目还有一个属性,用于跟踪该项目有多少 children。
我的标记如下所示:
<ul dnd-list="list"
dnd-allowed-types="['itemType']"
dnd-drop="myCallback(event, index, item, external, type, 'itemType')">
<li ng-repeat="item in list | filter:searchText"
dnd-draggable="item"
dnd-moved="list.splice($index, 1)"
dnd-effect-allowed="move"
dnd-selected="models.selected = item"
dnd-type="'itemType'"
ng-class="{'selected': models.selected === item}"
ng-dblclick="editProperties();">
{{item.label + ' - ' + item.description}}
</li>
</ul>
我的回调如下所示:
$scope.myCallback= function(event, index, item, external, type, allowedType) {
// If the item in question has no children then we don't need to do anything special other than move the item.
if (item.children.length == 0) {
return item;
}
// If moving items around list B then just move them.
for (var i = 0; i < $scope.models.lists.B.length; i++) {
if (item.label === $scope.models.lists.B.[i].label) {
return item;
}
}
// I only want this code to execute if we know we are moving from list A to list B and the item in question has > 0 children.
if (item.children.length > 0) {
// Code that I want to execute only when moving from list A to list B goes here
}
如果有人能够帮助我,我将非常感激。
谢谢!
【问题讨论】:
标签: javascript jquery angularjs drag-and-drop