【问题标题】:Sorting specific rows in a table + HTML + jQuery对表格中的特定行进行排序 + HTML + jQuery
【发布时间】:2013-05-04 21:54:00
【问题描述】:

我需要仅对表中的特定行进行排序。我正在使用 tablesorter 插件进行排序 (http://mottie.github.io/tablesorter/js/jquery.tablesorter.js)

我在 jsbin 中编写的示例。

http://jsbin.com/igijer/1/edit

当用户选择一个复选框时,相应的行会被添加到表格的顶部。 当未选中选定的复选框时,将相应的行附加到表上。

现在,未选择的行不排序。我的要求是,每当未选中复选框并将行附加到表中时,必须单独对未选中的行按字母顺序排序。

【问题讨论】:

  • 您能否澄清一下。未选择的行是否已排序?
  • 我希望未选中复选框的行始终被排序。

标签: jquery html css tablesorter


【解决方案1】:

这应该适合你 (demo):

<table id="ex" class="tablesorter">
  <thead>
    <th class="sorter-false"></th>
    <th>Name</th>
    <th>Contribution</th>
  </thead>
  <!-- add an "info" only (non-sorting) tbody -->
  <tbody class="tablesorter-infoOnly"></tbody>

  <tbody class="names">
    <tr>
      <td><input type="checkbox" /></td>
      <td>Warren Buffet</td>
      <td>business</td>
    </tr>
    <!-- etc -->
  </tbody>
</table>

代码

$(function() {

  $("#ex").tablesorter({ sortList: [[0,0], [1,0]] });

  $('#ex').on('click', 'input:checkbox', function() {
    var $this = $(this);
    if ($this.is(':checked')) {
      $this.closest('tr').prependTo("#ex .tablesorter-infoOnly");
    } else {
      $this.closest('tr').appendTo("#ex .names");
    }
    $this.trigger('update');
  });
});

“names”tbody 中未选中行的实际位置(附加)无关紧要,因为它将被添加回来并且当前排序将被更新。

【讨论】:

  • 非常感谢莫蒂!! :) 有用!好吧,如果我得到任何其他查询,将再次打扰您。请不要介意!! :)
  • 如果这个答案解决了你的问题,请点击复选标记接受它作为答案:)
  • 嗨,莫蒂。我试图通过在单击另一个表中的标题时触发单击一个表中的标题来进行排序。但是,我做不到。请帮我解决这个问题。这是我更改的代码jsbin.com/igijer/10/edit
  • 不是在标题上触发click,而是触发sort (ref) - updated demo
  • 谢谢莫蒂!你摇滚!! :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-27
  • 2012-07-22
  • 1970-01-01
  • 2021-12-21
  • 2012-12-25
相关资源
最近更新 更多