【问题标题】:How can I use jQuery (or CSS) to sort HTML tables by column?如何使用 jQuery(或 CSS)按列对 HTML 表格进行排序?
【发布时间】:2014-08-30 16:44:42
【问题描述】:

我希望能够取一张 HTML 表格,比如here:

<table border="1">
    <tbody>
        <thead>
        <tr>
            <th></th>
            <th><a href="http://translate.com" target="_blank">translate.com &amp; https://translate.google.com</a></th>
            <th><a href="http://www.bing.com/translator/" target="_blank">Bing Translator</a></th>
            <th></th>
        </tr>
        </thead>
        <tr>
            <td>I eat tacos</td>
            <td>Yo como tacos</td>
            <td>Comer tacos</td>
        </tr>
        <tr>
            <td>I will eat a taco</td>
            <td>Voy a comer un taco</td>
            <td>Me voy a comer un taco</td>
        </tr>
        <tr>
            <td>I will eat tacos</td>
            <td>Voy a comer tacos</td>
            <td>Me voy a comer tacos</td>
        </tr>
        <tr>
            <td>I would eat a taco</td>
            <td>Me volvería a comer un taco</td>
            <td>Comer un taco</td>
        </tr>
        <tr>
            <td>I would eat tacos</td>
            <td>Me volvería a comer tacos</td>
            <td>Comer tacos</td>
        </tr>
        <tr>
            <td>I am eating a taco</td>
            <td>Me estoy comiendo un taco</td>
            <td>Estoy comiendo un taco</td>
        </tr>
        <tr>
            <td>I am eating tacos</td>
            <td>Estoy comiendo tacos</td>
            <td>Voy a comer tacos</td>
        </tr>
            . . .
        <tr>
            <td>They were eating tacos</td>
            <td>Estaban comiendo tacos</td>
            <td>Estaban comiendo tacos</td>
        </tr>
    </tbody>
</table>

(或任何 HTML 表格)并允许在单击列时对其进行排序。

jQuery 或 CSS 解决方案都可以。

【问题讨论】:

  • 使用 jquery 数据表插件
  • 你不会得到一个 css 解决方案,你最好只是在谷歌上搜索像 DataTables 这样的东西 - 那里有大量的 javascript 表格库。

标签: jquery css html-table


【解决方案1】:

jQuery 表格排序器 (http://tablesorter.com/docs/) 可以工作。有许多不同的 jQuery 插件来处理这个问题。如果尚未使用 jQuery,也有纯 Javascript 选项。

33 Javascript 表格排序解决方案 (http://tympanus.net/codrops/2009/10/03/33-javascript-solutions-for-sorting-tables/)。

CSS 无法对数据进行排序;必须使用 Javascript。

【讨论】:

  • CDN 上是否提供 jQuery 表格排序器?我想在 jsfiddle 中使用它。
  • 是的。 CDNJS 有它。 (//cdnjs.cloudflare.com/ajax/libs/jquery.tablesorter/2.17.4/js/jquery.tablesorter.min.js)
猜你喜欢
  • 2021-05-28
  • 1970-01-01
  • 2020-03-12
  • 1970-01-01
  • 2011-11-25
  • 1970-01-01
  • 2018-12-06
  • 2017-07-19
  • 1970-01-01
相关资源
最近更新 更多