【问题标题】:How to sort one column depend on the value of another : jquery tablesorter如何对一列进行排序取决于另一列的值:jquery tablesorter
【发布时间】:2017-11-21 10:07:35
【问题描述】:

我正在使用 motti 的 tablesorter 对 html 表进行排序。我无法找到对某个列进行排序的方法取决于另一个列值。 在我的情况下,想要对 FULLNAME 列进行排序取决于基本上隐藏的 LASTNAME 列值。这可能吗?谢谢:-)

  <table id="tblDetails">
<thead>
    <tr>
        <th>FULL NAME</th>
        <th style="display:none;">LAST NAME</th>
        <th>GENDER</th>
        <th>ADDRESS</th>
    </tr>
</thead>
<tbody>
    <tr>
        <td>James</td>
        <td style="display:none;">Peter</td>
        <td>male</td>
        <td>washington dc</td>
    </tr>
    <tr>
        <td>jennifer</td>
        <td style="display:none;">lopez</td>
        <td>female</td>
        <td>New york</td>
    </tr>
    <tr>
        <td>Harrison</td>
        <td style="display:none;">Ford</td>
        <td>male</td>
        <td>washington dc</td>
    </tr>
</tbody>

【问题讨论】:

  • 你是说当用户点击名字列时,姓氏应该先排序,然后是名字?还是您总是希望表格按姓氏排序,然后单击任何列?
  • 嗨,Mottie,当我点击全名列时,我总是希望表格按姓氏排序。
  • 然后设置一个sortForce value

标签: jquery html sorting tablesorter


【解决方案1】:

简单的解决方案是在列 (FULLNAME) 之前添加一个隐藏的跨度或标签。它将根据第一个添加的值进行排序

      <table id="tblDetails">
    <thead>
        <tr>
            <th>FULL NAME</th>
            <th>GENDER</th>
            <th>ADDRESS</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td><span style="display:none;">Peter</span>James</td>
            <td>male</td>
            <td>washington dc</td>
        </tr>
   <tr>
        <td><span style="display:none;">lopez</span>jennifer</td>
        <td>female</td>
        <td>New york</td>
    </tr>
    </tbody>
    </table>

【讨论】:

    【解决方案2】:

    查找文档:https://mottie.github.io/tablesorter/docs/#methods 似乎您可以在所需列上处理 click 事件(作为任何 HTML 元素单击),然后触发对另一列的排序:

    $('table').find('th:eq(2)').trigger('sort');
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-01
      • 2018-01-01
      • 1970-01-01
      • 2017-06-10
      相关资源
      最近更新 更多