应用程序(AngularJS 1.5.11,https://codepen.io/loenko/pen/qPOWRp)是具有可拖动项目的列表组(使用来自相应程序包https://github.com/marceljuenemann/angular-drag-and-drop-lists的dnd-list指令实现)。
所有三个列表中的项目来自同一个项目对象数组。通过过滤项目列表'将它们放入相应的列表中。属性。
在拖放项目后对列表中的项目进行排序时出现问题。他们应该(我希望他们)准确地放置在他们被丢弃的地方,但他们会自动排序(被删除的项目被推到列表的末尾)。
有没有办法在不创建任何其他排序数组的情况下实现此类功能,更改实施包和更改数据模型(它是预定义的)?
无论如何,我会感谢所有其他解决方案!
列表结构如下:
<div ng-repeat="list in lists">
<ul dnd-list="list"
dnd-drop="callback({targetName: list.name})">
<li ng-repeat="item in items | filter:item.list=list.name"
dnd-draggable="item"
dnd-callback="item.list = targetName">
{{item.name}}
</li>
</ul>
</div>
数据模型是:
$scope.lists=[
{name:"List 1"}, {name:"List 2"}, {name:"List 3"}
];
$scope.items=[
{name:"Item 1",list:"List 1"}, {name:"Item 2",list:"List 2"}, {name:"Item 3",list:"List 3"}
];
angular.module('app', ['dndLists'])
.controller('mainCtrl', function($scope){
$scope.lists=[
{name:"List 1"}, {name:"List 2"}, {name:"List 3"}
];
$scope.items=[
{name:"Item 1",list:"List 1"},{name:"Item 2",list:"List 2"},{name:"Item 3",list:"List 3"}
];
$scope.addItem = function() {
var newItemIndex = $scope.items.length+1;
var newItemName = prompt("Enter new item name", "Item "+newItemIndex);
if (newItemName == null || newItemName == "") {
console.log("User cancelled the prompt.");
} else {
$scope.items.push({name:newItemName,list:"List 2"});
}
};
$scope.clear = function() {
var confClear = confirm("Are you sure you want to delete all the items?")
if (confClear = true) $scope.items = [];
}
});
&#13;
body {
background-color: whitesmoke;
}
.toolbar {
width: 100%;
background: lightgray;
height: 60px;
margin-bottom: 15px;
}
.toolbar button {
height: 40px;
width: 160px;
margin-top: 8px;
margin-left: 10px;
border-radius: 8px;
border: none;
box-shadow: 2px 2px 3px green;
outline-style: none;
background-color: lightgreen;
font-weight: bold;
font-size: 18px;
color: white;
}
.toolbar button:hover {
background-color: #64e764;
}
ul[dnd-list] {
min-height: 40px;
list-style-type: none;
padding: 5px;
margin: 10;
position: relative;
border: 2px solid lightgray;
box-sizing: border-box;
width: 70%;
}
ul[dnd-list] .dndDraggingSource {
display: none;
}
ul[dnd-list] .dndDragging {
opacity: 0.7;
}
ul[dnd-list] .dndPlaceholder {
min-height: 40px;
box-sizing: border-box;
border: 2px dotted gray;
}
.list {
margin-left: 10px;
}
.item {
background-color: lightgreen;
color: white;
width: 100%;
height: 40px;
margin-top: 5px;
margin-bottom: 5px;
}
.item .item-name {
line-height: 1.9;
margin-left: 5px;
}
.list-name {
color: gray;
}
&#13;
<!DOCTYPE html>
<html >
<head>
<meta charset="UTF-8">
<title>Sorting items in lists (d'n'd issue)</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css">
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<div ng-app="app" ng-controller="mainCtrl">
<div class="toolbar">
<button ng-click="addItem()">ADD ITEM</button>
<button ng-click="clear()">CLEAR LIST</button></div>
<div class="list" ng-repeat="list in lists">
<div class="list-name">{{list.name}}</div>
<ul dnd-list="list"
dnd-drop="callback({targetName: list.name})">
<li class="item"
ng-repeat="item in items | filter:item.list=list.name"
dnd-draggable="item"
dnd-callback="item.list = targetName">
<div class="item-name">{{item.name}}</div>
</li>
</ul>
</div>
</div>
<script src='https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.11/angular.min.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/angular-drag-and-drop-lists/2.1.0/angular-drag-and-drop-lists.min.js'></script>
<script src="js/index.js"></script>
</body>
</html>
&#13;
答案 0 :(得分:1)
您好我知道这不完全是您提出的问题,但我使用了您的代码,如果您对某些更改感到满意,例如代码调整和示例将您的2个列表组合在一起,如下所示:
[
{
name: "Test List One",
items: [
{
name: 'Some item 1',
value: 1000
},
{
name: 'Some item 2',
value: 2000
},
]
},
{
name: "Test List Two",
items: [
{
name: 'Some item 3',
value: 3000
},
{
name: 'Some item 4',
value: 4000
},
]
}
];
看看这里,https://codepen.io/anon/pen/WZQbPm?editors=1010 如果你需要从新的JSON结构中获取一些项目,我还添加了一些辅助函数。
很抱歉,如果这不是您提出的问题,但我个人认为JSON更改更好,在函数中编写代码并将它们暴露给$ scope然后每次都写$ scope.someFunction会更好。 / p>