【发布时间】:2017-07-20 20:17:41
【问题描述】:
对 jQuery 和 JavaScript 还很陌生,所以请保持温和...
我正在开发一个 POC 以创建一个“列映射”页面,用户可以在该页面中将“列标题”列表拖放到新列标题的网格中。我需要构建一个可以发送回 SQL 数据库的数组。我有这部分(大部分)按我的意愿运作。
当项目从左侧的列列表拖到右侧的标题网格时,如果项目存在,代码应该更新/替换该索引处的数组项目。如果该项不存在,则应将该项添加到数组中。
例如:如果将“First Name”拖到“Headers”,则应将其添加到索引位置 0。如果随后将“First Name”拖到“with”,则应删除索引 0 处的“First Name”值并在位置 1 添加值。如果然后将“Last Name”拖到“with”,它应该使用“Last Name”值更新位置 1 的数组。
$(document).ready(() => {
$(function() {
$('.item').draggable({
cursor: "crosshair",
cursorAt: {
left: 5
},
distance: 10,
opacity: 0.75,
revert: true,
snap: ".target",
containment: "window"
});
});
$(function() {
var array = [];
var arraytext = '';
$('.target').droppable({
accept: ".item",
tolerance: 'pointer',
activeClass: 'active',
hoverClass: 'highlight',
drop: function(event, ui) {
var dropped = $(this);
var dragged = $(ui.draggable);
$(function(index, item) {
var test = '';
array.push($(dragged).text());
arraytext = JSON.stringify(array);
test += "Index Value = " + $(dropped).index() + ", Text = " + $(dragged).text() + "<br/>";
$('#test').html(test);
$('#array').text(arraytext);
});
}
});
});
});
#container {
border: solid black 1px;
margin: 0 auto;
height: 800px;
}
#gridview {
border: 2px solid #292da0;
border-radius: 5px;
background-color: #7577a3;
display: grid;
grid-template-columns: repeat(auto-fit, 100px);
grid-template-rows: auto;
margin-left: 105px;
}
.target {
border: 2px solid #1c1c3a;
border-radius: 5px;
background-color: #a7a7ef;
padding: 1em;
}
#flex {
border: 2px solid #292da0;
border-radius: 5px;
width: 100px;
background-color: #7577a3;
display: flex;
flex-flow: column wrap;
justify-content: flex-end;
float: left;
}
.item {
border: 2px solid #1c1c3a;
border-radius: 5px;
background-color: #a7a7ef;
padding: 1em;
}
.active {
background-color: blue;
color: white;
border: 2px solid black;
}
.highlight {
background-color: yellow;
color: black;
border: 2px solid blue;
}
.table {
border: solid black 1px;
width: 86px;
padding: 5px;
}
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.0/jquery-ui.min.js"></script>
<div id="container">
Test:
<div id="test">
</div>
Array:
<div id="array">
</div>
<div id="gridview">
<div class="target">Headers</div>
<div class="target">with</div>
<div class="target">different</div>
<div class="target">column</div>
<div class="target">names</div>
</div>
<span>
<div id="flex">
<div class="item">First Name</div>
<div class="item">Last Name</div>
<div class="item">Code</div>
<div class="item">Date</div>
<div class="item">Amount</div>
</div>
<table>
<thead>
<tr>
<td class="table">Some</td>
<td class="table">Column</td>
<td class="table">Data</td>
<td class="table">Goes</td>
<td class="table">Here</td>
</tr>
</thead>
<tr>
<td class="table">Other Data</td>
<td class="table">Other Data</td>
<td class="table">Other Data</td>
<td class="table">Other Data</td>
<td class="table">Other Data</td>
</tr>
</table>
</span>
</div>
JSFiddle:https://jsfiddle.net/qxwzcn9w/4/
最终,如果您发现任何危险信号或“陷阱”,我会将索引值发送回数据库,以便加分。谢谢!
【问题讨论】:
-
看起来您只是将值推送到数组中。如果你改为添加这样的值:array[index] = value,那应该会给你你想要的。分配值之间的未分配索引将自动设置为未定义,因此数据库需要能够处理该问题,以及比预期短的数组。
-
想法:什么是,如果您检查放置了哪个元素并在数组上设置(fe First Element)“名字”值为0,如果“名字”是拖动到“with”,“First Name”的数组值设置为 1。毕竟,提交按钮从数组中读取位置并将其发送到数据库。我希望这可以理解^^
-
@Chairs 感谢您的回复。我认为您正在解决问题。有没有办法根据gridview div中的元素数量创建具有多个索引/空字符串的数组?
-
@FalkeDesign 是的,完全正确。
标签: javascript jquery css json