按列对表行进行排序

时间:2015-09-27 03:55:29

标签: javascript jquery sorting geolocation

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

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

以下是表格的外观:

enter image description here

1 个答案:

答案 0 :(得分:3)

为了快速完成,我按照以下步骤使用sorttable.js

  • 包括sorttable.js
  • 添加课程"可排序"到你的桌子
  • 删除" km"从数字(您可以稍后再添加它们,使用 CSS:后)

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

        <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>

那就是它,现在你的距离栏除了它之外还有一个排序三角形! Here是jsfiddle!

相关问题