Jac*_*b H 7 javascript css jquery json
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);
});
}
});
});
});Run Code Online (Sandbox Code Playgroud)
#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;
}Run Code Online (Sandbox Code Playgroud)
<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>Run Code Online (Sandbox Code Playgroud)
JSFiddle:https://jsfiddle.net/qxwzcn9w/4/
最后,如果您发现任何红旗或"陷阱",我会将索引值发送回数据库,以便获得奖励积分.谢谢!
使用 for 循环查找元素是否存在的简单解决方案。$.each()如果您熟悉 jQuery,也可以使用。如果需要,您可以通过计算表头列来计算数组长度$("thead").find("td")。然后照常迭代以查看该元素是否存在,如果存在则将其删除,然后插入到适当的索引中。
$(function() {
$('.item').draggable({
cursor: "crosshair",
cursorAt: {
left: 5
},
distance: 10,
opacity: 0.75,
revert: true,
snap: ".target",
containment: "window"
});
});
$(function() {
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 = '';
for(var i = 0; i < array.length; i++)
{
if($(dragged).text() == array[i])
{
array[i] = "";
}
}
array[$(dropped).index()] = $(dragged).text();
arraytext = JSON.stringify(array);
test += "Index Value = " + $(dropped).index() + ", Text = " + $(dragged).text() + "<br/>";
$('#test').html(test);
$('#array').text(arraytext);
});
}
});
});
Run Code Online (Sandbox Code Playgroud)