0

我有一个名为 #usersInRoom 的 div,它是内部 div 的占位符。

目前我在 div 中有 2 行/2 个条目。

但我的问题是,我该如何通过。jQuery 更新条目以按“exp”排序?

现在它首先显示“Stefanie”和“6”exp。和 14 exp 的 Bare Kald Mig Jesper。最后的。

我如何对其进行排序,所以它首先输出具有最高 exp 的条目?

<div id="usersInRoom">
    <div class="entry in_room" id="userInRoom-2" style="background-color: rgb(255, 255, 255);">
        <table style="width:100%">
            <tbody>
                <tr>
                    <td style="width:32px">
                        <img src="https://fbcdn-profile-a.akamaihd.net/hprofile-ak-prn1/c33.33.414.414/s200x200/996882_221295918024677_1913237122_n.jpg" style="height:32px;width:32px">
                    </td>
                    <td style="vertical-align:middle"><strong>Stefanie Pedersen</strong>
                    </td>
                    <td align="right" style="vertical-align:middle;color:#999"><span id="exp-2">6</span> exp.</td>
                </tr>
            </tbody>
        </table>
    </div>
    <div class="entry in_room" id="userInRoom-1" style="background-color: rgb(155, 229, 162);">
        <table style="width:100%">
            <tbody>
                <tr>
                    <td style="width:32px">
                        <img src="https://fbcdn-profile-a.akamaihd.net/hprofile-ak-frc1/c176.49.608.608/s200x200/429356_10151257167066983_1091687280_n.jpg" style="height:32px;width:32px">
                    </td>
                    <td style="vertical-align:middle"><strong>Bare Kald Mig Jesper</strong>
                    </td>
                    <td align="right" style="vertical-align:middle;color:#999"><span id="exp-1">14</span> exp.</td>
                </tr>
            </tbody>
        </table>
    </div>
</div>
4

2 回答 2

0

如果您要显示数据库中的值,您可以在后端查询中使用 sql ORDER BY 'exp' ASC 或 DESC 进行排序以填充结果。然后您可以首先获得排序结果。

OR

如果您想在客户端对其进行排序,请尝试使用 jueery 数据表。例如
https://datatables.net/release-datatables/examples/basic_init/zero_config.html

于 2013-08-23T06:55:06.217 回答
0

您可以通过这种方式使用 jquery 对当前代码执行此操作:首先将一个类添加到体验范围。

<span id="exp-2" class="exp">6</span>

然后是jQuery:

arr=$("#usersInRoom").find(".exp");

现在选择父 div:

$(arr[i]).parents(".entry_in_room")

排序可以通过访问元素的文本来完成。

$(arr[i]).text()

但理想情况下,为什么需要这样的嵌套?数据可以在 Table 元素中轻松处理。不需要单独的表。如果您使用单个表,则有许多 jquery 插件可用。

于 2013-08-23T07:01:02.627 回答