Sort table by class in TH

2019-09-18 19:05发布

I have a table with 10 columns, every column has a different class, for example the last has: data-tot

How to sort the table by this class and in descending order?

A full jsFiddle is available here, but a small sample of the HTML looks like this (the odd and even rows continue for the full data).

<table class="sp-league-table sp-data-table sp-sortable-table sp-scrollable-table sp-paginated-table dataTable no-footer" data-sp-rows="30" id="DataTables_Table_9" role="grid">
  <thead>
    <tr role="row">
      <th class="data-rank sorting" tabindex="0" aria-controls="DataTables_Table_9" rowspan="1" colspan="1" aria-label="Pos">Pos</th>
      <th class="data-name sorting" tabindex="0" aria-controls="DataTables_Table_9" rowspan="1" colspan="1" aria-label="Squadra">Squadra</th>
      <th class="data-gia sorting" tabindex="0" aria-controls="DataTables_Table_9" rowspan="1" colspan="1" aria-label="GIA">GIA</th>
      <th class="data-ros sorting" tabindex="0" aria-controls="DataTables_Table_9" rowspan="1" colspan="1" aria-label="ROS">ROS</th>
      <th class="data-gio sorting" tabindex="0" aria-controls="DataTables_Table_9" rowspan="1" colspan="1" aria-label="6GIO">6GIO</th>
      <th class="data-giotwo sorting" tabindex="0" aria-controls="DataTables_Table_9" rowspan="1" colspan="1" aria-label="5GIO">5GIO</th>
      <th class="data-giothree sorting" tabindex="0" aria-controls="DataTables_Table_9" rowspan="1" colspan="1" aria-label="4GIO">4GIO</th>
      <th class="data-giofour sorting" tabindex="0" aria-controls="DataTables_Table_9" rowspan="1" colspan="1" aria-label="3GIO">3GIO</th>
      <th class="data-tav sorting" tabindex="0" aria-controls="DataTables_Table_9" rowspan="1" colspan="1" aria-label="TAV">TAV</th>
      <th class="data-tot sorting" tabindex="0" aria-controls="DataTables_Table_9" rowspan="1" colspan="1" aria-label="TOT">TOT</th>
    </tr>
  </thead>
  <tbody>
    <tr class="odd" role="row">
      <td class="data-rank">1</td>
      <td class="data-name"><a href="http://www.quartuleague.com/squadra/consulenza-impresa/">CONSULENZA IMPRESA</a></td>
      <td class="data-gia">0.5</td>
      <td class="data-ros">0</td>
      <td class="data-gio">0.5</td>
      <td class="data-giotwo">1</td>
      <td class="data-giothree">0</td>
      <td class="data-giofour">0</td>
      <td class="data-tav">0</td>
      <td class="data-tot">2.0</td>
    </tr>
    <tr class="even" role="row">
      <td class="data-rank">2</td>
      <td class="data-name"><a href="http://www.quartuleague.com/squadra/new-team/">NEW TEAM</a></td>
      <td class="data-gia">0.5</td>
      <td class="data-ros">2</td>
      <td class="data-gio">1.0</td>
      <td class="data-giotwo">1</td>
      <td class="data-giothree">0</td>
      <td class="data-giofour">0</td>
      <td class="data-tav">0</td>
      <td class="data-tot">4.5</td>
    </tr>
  </tbody>
</table>

2条回答
不美不萌又怎样
2楼-- · 2019-09-18 19:44

As from you link you are not using DataTables. you need to simple give an id to you table. And the rest will do DataTables.
Here is updated link

查看更多
贼婆χ
3楼-- · 2019-09-18 19:56

SOLUTION

Use the code below to sort table by column specified by jQuery selector .data-tot in descending order.

$('#myTable').DataTable().column('.data-tot').order('desc').draw();

See columns().order() for more details.

DEMO

See updated jsFiddle for code and demonstration.

查看更多
登录 后发表回答