javascript - 如果该索引位置存在元素,如何更新 javascript 数组?

标签 javascript jquery css json

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 对象映射到数组

DEMO

关于javascript - 如果该索引位置存在元素,如何更新 javascript 数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45224435/

相关文章:

php - 将缩略图连接到使用 php 提取的图像

css - 如何使用 scss 删除 bootstrap 输入框边框?

javascript - 如何在 Javascript 事件中检查 Chrome 中仅存在于 DOM 中的元素

javascript - 在javascript数组中搜索最接近的下一个值

javascript - Grails单选按钮显示带有onclick的隐藏div

javascript - 使用 jQuery 在循环内追加。一定会有更好的办法

javascript - 如何将 Django 模板中的内容替换为其他内容?

css - 多行垂直居中 <div>

javascript - 为什么需要 Math.floor 来生成 x 和 y 之间的随机数?

javascript - 一侧颜色为 'lineargradient' 的 Canvas 动画较暗并且无法正确调整大小?