gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 03:27:35 25 4
gpt4 key购买 nike

应用程序(AngularJS 1.5.11, https://codepen.io/loenko/pen/qPOWRp )是一组具有可拖动项目的列表(使用相应包 https://github.com/marceljuenemann/angular-drag-and-drop-lists 中的 dnd-list 指令实现)。

所有三个列表中的项目都来自同一项目对象数组。它们通过项目的“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>

最佳答案

您好,我知道这并不完全是您所要求的,但我尝试了您的代码,如果您对代码调整等一些更改感到满意,并且示例将您的 2 个列表组合在 JSON 中,如下所示:

[
{
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 会更好。

关于javascript - 按列表特定项目的字段对多个列表中的 d'n'd 列表项目进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46251532/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com