jQuery ui可排序的表格刷新数字位置

bob*_*rus 14 jquery jquery-ui html-table jquery-ui-sortable

在html表中,我在每一行都有一个具有自己位置编号的单元格.在使用jQueryUI进行排序后,如何正确刷新此位置编号?

这是我的简单html:

<table border="0">
<thead>
      <tr>
        <th scope="col">Position number</th>
        <th scope="col">Name</th>
      </tr>
  </thead>
  <tbody>
      <tr>
        <td>1</td>
        <td>Text</td>
      </tr> 
      <tr>
        <td>2</td>
        <td>Text</td>
      </tr>
  </tbody>        
</table>
Run Code Online (Sandbox Code Playgroud)

这是我的js:

    var fixHelper = function(e, ui) { ui.children().each(function() { $(this).width($(this).width()); }); return ui; };

    $("table tbody").sortable({ 
        helper: fixHelper
    }).disableSelection();
Run Code Online (Sandbox Code Playgroud)

现在我想在完成排序后正确更改订单的正确位置编号值.

我该怎么做?

任何帮助,将不胜感激.

Gau*_*hah 24

排序后执行以下操作..

$("table tbody").sortable({ 
    update: function(event, ui) {  
        $('table tr').each(function() {
            $(this).children('td:first-child').html($(this).index())
        });
    },
    helper: fixHelper
}).disableSelection();
Run Code Online (Sandbox Code Playgroud)

你可以尝试(未经测试):而不是$('table tr').each使用$(this).parents('table').find('tr').each

说明..它循环遍历tr表中的每个标记,然后td-child使用索引值更改第一个内容tr

  • 非常感谢,好的答案和良好的解释,它完美无缺.我接受并投票给你答案,如果你愿意,你可以为我的问题做同样的事情.非常感谢! (2认同)
  • 帮助我 - 很好的回应和问题 (2认同)