如果项目存在于该索引位置,如何更新 javascript 数组?



jQuery和JavaScript来说很新,所以请保持温和...

我正在处理一个 POC 以创建一个"列映射"页面,用户可以在其中将"列标题"列表拖放到新列标题的网格中。我需要构建一个可以发送回 SQL 数据库的数组。我有这部分(大部分(按照我想要的方式运作。

当项目从左侧的列列表拖动到右侧的标题网格时,如果该项目存在,则代码应更新/替换该索引处的数组项。如果该项不存在,则应将该项添加到数组中。

例如:如果将"名字"拖到"标题",则应将其添加到索引位置 0。如果随后将"名字"拖到"with",则应删除索引 0 处的"名字"值,并在位置 1 处添加该值。如果随后将"姓氏"拖到"with",则应使用"姓氏"值更新位置 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/

最终,我将把索引值发送回数据库,因此如果您发现任何危险信号或"陷阱",我会加分。谢谢!

就个人而言,我会使用对象而不是数组,并使用索引作为对象的键

var cols ={};
$('.target').droppable({
.....
drop: function(event, ui) {
var txt = $(ui.draggable).text(),
colIdx = $(this).index();
// clear from any prior position
$.each(cols, function(key, val){
if (val === txt){
cols[key] = null
}
});
// add to new position
cols[colIdx] = txt;           
}
}).each(function(i){
// add each index as key in cols object
cols[i] = null;
});

如果你必须在后端有数组,那么在提交之前将 cols 对象映射到数组很简单

演示

使用 for 循环查找元素是否存在的简单解决方案。如果你对jQuery感到满意,你也可以使用$.each()。如果需要,您可以通过使用$("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);
});
}
});
});
array.push($(dragged).text());

此行将删除的列推送到数组中,而不是更新它。

您应该首先在某处初始化一个数组,每个列标题的值为空。

var arr = Array.apply(null, new Array(10)).map(Number.prototype.valueOf,0);
//console.log(arr) Outputs as [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

将数字更改为您拥有的列数。

要更改给定索引处的值,请执行以下操作:

arr[3]='a new value';
//console.log(arr) Outputs as [0, 0, 0, 'a new value', 0, 0, 0, 0, 0, 0]

最新更新