繁体   English   中英

按列对表行进行排序

[英]Sorting table rows by a column

我在表格中有一个使用地理位置API计算距离的城市列表。 我想按距离列自动对表格行进行排序。 我试过几个表排序插件,但都失败了。

有什么建议吗? 这是我的代码: jsfiddle.net/vLLsLLrb/

表格的外观如下:

在此处输入图片说明

为了快速做到这一点,我通过执行以下步骤来使用sorttable.js

  • 包括sorttable.js
  • 将“可排序”类添加到表中
  • 从数字中删除“ km”(您可以稍后使用css:after再次添加)

基本上,主要是更改表格标签:

        <table id="nearest" class="table table-striped sortable">
            <thead>
                <tr>
                    <th>Destination</th>
                    <th>Distance to</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <th>London</th>
                    <td><span id='london'></span>
                    </td>
                </tr>
                <tr>
                    <th>Newcastle</th>
                    <td><span id='newcastle'></span>
                    </td>
                </tr>
                <tr>
                    <th>Cardiff</th>
                    <td><span id='cardiff'></span>
                    </td>
                </tr>
                <tr>
                    <th>Cambridge</th>
                    <td><span id='cambridge'></span>
                    </td>
                </tr>
                <tr>
                    <th>Reading</th>
                    <td><span id='reading'></span>
                    </td>
                </tr>
            </tbody>
        </table>

就是这样,现在您的距离列旁边还有一个排序三角形! 是jsfiddle!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM