gpt4 book ai didi

angularjs - Angular 拖放(使用 jQuery UI)- 禁用交换

转载 作者:行者123 更新时间:2023-12-02 23:40:42 26 4
gpt4 key购买 nike

我在交换拖/放元素时遇到问题。

DOM/ Angular 结构:

angular.module('app', ['ngDragDrop'])
.controller('controller', function($scope) {
$scope.listItems = [
{name: "some name", title: "title"},
{name: "some name2", title: "title2"},
];
$scope.input = {};
$scope.draggableOprions = {
revert: 'invalid'
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<script src="https://code.angularjs.org/1.4.9/angular.js"></script>
<script src="https://cdn.jsdelivr.net/angular.dragdrop/1.07/angular-dragdrop.min.js"></script>
<div ng-app="app">
<div ng-controller="controller">
<div class="container">
<ul>
<li data-drag="true"
data-jqyoui-options="draggableOprions"
jqyoui-draggable="{animate:true}"
ng-model="item" ng-repeat="item in listItems track by $index">
{{item.title}}
</li>
</ul>
</div>

<div class="container"
data-drop="true"
data-jqyoui-options jqyoui-droppable
ng-model="input">
<input ng-model="input.name">
</div>
</div>
</div>

问题:

当我拖放列表项 1 时,列表项 1name 属性来到 input 模型。但在列表项 1 中不再可用。实际上,list item 1 值变为 undefined ||将其放入输入后为null。如果我现在尝试在 input 中拖放 list item 2 项 - 值交换 (list item 1 = undefined || null ?list item 2 = list item 1 valueinput model equal to list item 2 value`。所以一切都会随机播放。

我需要什么:我需要拖放输入中的列表项,避免丢失列表项中的值。每次我在输入中放置列表项时,我都需要将其值绑定(bind)到输入。

开箱即用
我可以更改拖放库,甚至使用源代码,但库是更好的选择。我接受几乎所有没有违反任何良好代码标准的良好工作答案(我的意思是我需要不会破坏其他代码并且具有良好结构的代码)。

最佳答案

我建议使用 ngDraggable ,一个 Angular 模块,不依赖 jQuery 或 jQuery-ui。

下面是一个工作片段或查看我的 Codepen :

angular.module('app', ['ngDraggable'])
.controller('controller', function($scope) {

$scope.listItems = [{
name: "some name",
title: "title1"
}, {
name: "some name2",
title: "title2"
}, {
name: "some name3",
title: "title3"
}, ];

$scope.droppedObjects = [];
$scope.input = {};

// drag complete over drop area
$scope.onDragComplete = function(data, evt) {
console.log("drag success, data:", data);
var index = $scope.droppedObjects.indexOf(data);
if (index > -1) {
$scope.droppedObjects.splice(index, 1);
}
}

// drop complete over drop area
$scope.onDropComplete = function(data, evt) {
console.log("drop success, data:", data);
var index = $scope.droppedObjects.indexOf(data);
if (index == -1)
$scope.droppedObjects.push(data);
}

// drop complete over input box
$scope.onDropCompleteInput = function(data, evt) {
console.log("drop on input success, data:", data);
$scope.input = data;
}

// drop complete over items area (remove from dropped list)
$scope.onDropCompleteRemove = function(data, evt) {
console.log("drop success - remove, data:", data);
var index = $scope.droppedObjects.indexOf(data);
if (index != -1)
$scope.droppedObjects.splice(index);
}

// other draggable events handlers

var onDraggableEvent = function(evt, data) {
console.log("128", "onDraggableEvent", evt, data);
}
$scope.$on('draggable:start', onDraggableEvent);
//$scope.$on('draggable:move', onDraggableEvent);
$scope.$on('draggable:end', onDraggableEvent);

});
* {
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box;
box-sizing: border-box;
}

[ng-drag] {
-moz-user-select: -moz-none;
-khtml-user-select: none;
-webkit-user-select: none;
-ms-user-select: none;
user-select: none;
}

.item {
width: 100px;
height: 60px;
background: rgba(255, 0, 0, 0.5);
color: white;
text-align: center;
padding-top: 5%;
display: inline-block;
margin: 0 10px;
cursor: move;
}

ul.draggable-objects:after {
display: block;
content: "";
clear: both;
}

.draggable-objects li {
float: left;
display: block;
width: 120px;
height: 100px;
}

[ng-drag].drag-over {
border: solid 1px red;
}

[ng-drag].dragging {
opacity: 0.5;
}

.drop-container {
background: rgba(0, 255, 0, 0.5);
text-align: center;
width: 600px;
height: 200px;
padding-top: 90px;
display: block;
margin: 20px auto;
position: relative;
}

.drop-input {
width: 200px;
height: 40px;
}

.drag-enter {
border: solid 5px red;
}

.drop-container span.title {
display: block;
position: absolute;
top: 10%;
left: 50%;
width: 200px;
height: 20px;
margin-left: -100px;
margin-top: -10px;
}

.drop-container div {
position: relative;
z-index: 2;
}
<script src="//code.angularjs.org/1.4.8/angular.js"></script>
<script src="//rawgit.com/fatlinesofcode/ngDraggable/master/ngDraggable.js"></script>

<body ng-app="app">
<div ng-controller="controller">

<div class="row">
<h1>ngDraggable Example</h1>
</div>

<div ng-drop="true" ng-drop-success="onDropCompleteRemove($data,$event)">
<ul class="draggable-objects">
<li ng-repeat="obj in listItems">
<div ng-drag="true" ng-drag-data="obj" data-allow-transform="true" class="item"> {{obj.title}} </div>
</li>
</ul>
</div>

<hr/>
<div ng-drop="true" ng-drop-success="onDropComplete($data,$event)" class="drop-container">
<span class="title">Drop area</span>

<div ng-repeat="obj in droppedObjects" ng-drag="true" ng-drag-data="obj" ng-drag-success="onDragComplete($data,$event)" class="item">
{{obj.title}}
</div>
</div>

<hr/>
<div class="container">
Drop on input:
<input ng-model="input.name" class="drop-input" ng-drop="true" ng-drop-success="onDropCompleteInput($data,$event)">
</div>

<br>
<hr/>
<pre>listItems = {{listItems|json}}</pre>
<pre>input = {{input|json}}</pre>
<pre>droppedObjects = {{droppedObjects|json}}</pre>
</div>
</body>

关于angularjs - Angular 拖放(使用 jQuery UI)- 禁用交换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35049150/

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