Can't access element after drag and drop

Viewed 111

I've made a drag and droppable table for sorting products.

My table looks like this:

<table class="table table-sortable" id="mytab">

    <tbody>

    [{foreach from=$mylist item=listitem}]
    [{assign var="_cnt1" value=$_cnt1+1}]

        <tr draggable="true" id="test_variant.[{$_cnt1}]">
            <td class="idcount">[{$_cnt1}]</td>
            <td class="[{$listclass}]">Productname</td>
            <input id="sortcount[{$_cnt1}]" class="sortcount" type="hidden" value="[{$_cnt1}]"/>
        </tr>

    [{/foreach}]
    </tbody>
</table>

My jQuery looks like this:

$(function() {
        $('.table-sortable tbody').sortable({
            handle: 'span'
        });

    $('.table-sortable tbody').sortable().bind('sortupdate', function(e, ui) {
        function updateTable(){
        var table = document.getElementById("mytab");
            for (var i = 0, row; row = table.rows[i]; i++) {
                $( row ).attr("id","test_variant."+i);
                $( row ).find(".idcount").text(i);  
                $( row ).find(".sortcount").val(i+10);
            }
        }
        setTimeout(updateTable, 100);
    });
});

The problem is, after I drag and drop my items the dom or something doesn't exist anymore.

Like my system won't save the dragged and dropped entries (But the others which are updated but not dragged).

And what leads me here because I don't understand it is when i dragged an item and try to access it like this

document.getElementById("sortcount1").value = "200";

it just says

Uncaught TypeError: Cannot set property 'value' of null like it's not existing

I've no problem accessing the other items.

When I open the dom with clicking on elements in chrome devtools, then back to console and try it again its working again like it needs to initialize or something.

Hope someone can give me a hint.

Thank you!

1 Answers

You might consider the following example.

$(function() {
  function updateTable(e, ui) {
    var t = $(e.target).closest("table");
    $("tbody tr", t).each(function(i, el) {
      $(el).attr("id", "test_variant." + i);
      $(".idcount", el).html(i);
      $(".sortcount", el).val(i + 10);
    });
  }

  $('.table-sortable tbody').sortable({
    handle: 'span',
    update: updateTable
  });
});

This will reset the items in the table after the Sort has updated.

Update

$(function() {
  function updateTable(e, ui) {
    var t = $(e.target).closest("table");
    $("tbody tr", t).each(function(i, el) {
      var c = i + 1;
      $(el).attr("id", "test_variant." + c);
      $(".idcount", el).html(c);
      $(".sortcount", el).val(c + 10);
    });
  }

  $('.table-sortable tbody').sortable({
    handle: 'span',
    items: "> tr",
    update: updateTable
  });
});
table {
  width: 340px;
}

table tr {
  margin: 0 3px 3px 3px;
  padding: 0.4em;
  padding-left: 1.5em;
  font-size: 1.4em;
  height: 18px;
}
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

<table class="table-sortable">
  <thead>
    <tr>
      <td>&nbsp;</td>
      <td>ID</td>
      <td>Product Name</td>
    </tr>
  </thead>
  <tbody>
    <tr id="test_variant.1">
      <td><span class="ui-icon ui-icon-arrowthick-2-n-s"></span></td>
      <td class="idcount">1</td>
      <td class="products">Item Name 1
        <input id="sortcount-1" class="sortcount" type="hidden" value="11" />
      </td>
    </tr>
    <tr id="test_variant.2">
      <td><span class="ui-icon ui-icon-arrowthick-2-n-s"></span></td>
      <td class="idcount">2</td>
      <td class="products">Item Name 2
        <input id="sortcount-2" class="sortcount" type="hidden" value="12" />
      </td>
    </tr>
    <tr id="test_variant.3">
      <td><span class="ui-icon ui-icon-arrowthick-2-n-s"></span></td>
      <td class="idcount">3</td>
      <td class="products">Item Name 3
        <input id="sortcount-3" class="sortcount" type="hidden" value="13" />
      </td>
    </tr>
    <tr id="test_variant.4">
      <td><span class="ui-icon ui-icon-arrowthick-2-n-s"></span></td>
      <td class="idcount">4</td>
      <td class="products">Item Name 4
        <input id="sortcount-4" class="sortcount" type="hidden" value="14" />
      </td>
    </tr>
    <tr id="test_variant.5">
      <td><span class="ui-icon ui-icon-arrowthick-2-n-s"></span></td>
      <td class="idcount">5</td>
      <td class="products">Item Name 5
        <input id="sortcount-5" class="sortcount" type="hidden" value="15" />
      </td>
    </tr>
  </tbody>
</table>

Tested the code and it appears to work properly.

Related