按列表特定项的字段对多个列表中的 d'n'ed 列表项进行排序

Sorting of d'n'd'ed list items in multiple lists by list-specific item's field

应用程序(AngularJS 1.5.11,https://codepen.io/loenko/pen/qPOWRp) is the group of lists with draggable items (implemented with dnd-list directive from corresponding package https://github.com/marceljuenemann/angular-drag-and-drop-lists)。

所有三个列表中的项目都来自同一个项目对象数组。它们被item的'list'属性.

过滤到相应的列表中

问题是在拖放项目后对列表内的项目进行排序。它们应该(我希望它们)准确地放置在它们被丢弃的位置,但它们是自动排序的(丢弃的项目被推到列表的末尾)。

有没有一种方法可以在不创建任何额外的排序数组、更改 d'n'd 实现包和更改数据模型(它是预定义的)的情况下实现这样的功能?

无论如何,我会感谢所有替代解决方案!

列表结构如下:

<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 = [];
 }
});
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;
}
<!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>

你好,我知道这不是你问的,但我试过你的代码,如果你对代码调整和示例等一些更改感到满意,可以像这样在 JSON 中组合你的 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 更好一些每一次。