将元素添加到按时间和ID排序的ul列表中

贡加丁

我正在尝试Socket.IO,我发现它非常出色。我正在尝试构建一个非常简单的应用程序,以实时更新列表。

我的Node应用程序生成的数组至少包含一个唯一的id(非数字)键和一个time键,键是每个项目的unix时间戳。现在,我要做的是为该<li>数组中的每个项目生成一个元素(使用MustacheJS),然后将其推送到<ul>HTML中的现有列表中。到现在为止还挺好。

现在,我想通过仅推送新元素并根据time对它们进行排序,而不是一直推送完整列表来使它更好一点

最好的方法是什么?

这是一些虚拟代码来说明我的用例:

// Let's say this is my initial array
array_one = [
                {
                    id: 'one_1',
                    time: '1467982149',
                    content: 'Content one_1'
                },
                {
                    id: 'one_2',
                    time: '1467983149',
                    content: 'Content one_2'
                },
                {
                    id: 'one_3',
                    time: '1467981149',
                    content: 'Content one_3'
                },
                {
                    id: 'one_4',
                    time: '1467482149',
                    content: 'Content one_4'
                }
            ];

/* 
    Here I take that first array, generate a <li></li> element for
    each item in it, obtaining something like this :

    <li data-id="one_1" data-time="1467982149">Content one_1</li>

    I store all of those in an array, let's say "allMyItems"
*/

/* 
    I have all of my <li> elements, so now I push them to my <ul>
    list using a Socket.IO event and jQuery on the client side.
    It looks like this :

    $('ul').html( 'allMyItems' );
*/

// Later fetch the data again, and there's old but also new elements
// and this is where I don't know how to do it. You can see it's
// mostly the same array but with two new elements
array_one = [
                {
                    id: 'one_1',
                    time: '1467982149',
                    content: 'Content one_1'
                },
                {
                    id: 'one_2',
                    time: '1467983149',
                    content: 'Content one_2'
                },
                {
                    id: 'one_3',
                    time: '1467981149',
                    content: 'Content one_3'
                },
                {
                    id: 'one_4',
                    time: '1467482149',
                    content: 'Content one_4'
                },
                {
                    id: 'two_1',
                    time: '1467432149',
                    content: 'Content two_1'
                },
                {
                    id: 'two_2',
                    time: '1467232149',
                    content: 'Content two_2'
                }
            ];

/* 
    What I want to do now, is :
        1. Update the front-end <ul> list with just the two new items
        2. Correctly sort that <ul> using the 'time' key
*/
贡加丁

其实我知道了。这很简单,我不知道为什么一开始它并不明显。这是逻辑:

  • 服务器端(Node应用程序),我跟踪发送给客户端的每个ID
  • 当我有新数据时,请确保此ID尚未发送
  • 我直接在浏览器中进行排序客户端(我已经确定了最佳选择,但是我知道这是可能的)

这里 ...

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

将列表元素动态添加到ul元素

来自分类Dev

将元素添加到列表与在python中设置相比的时间复杂度

来自分类Dev

将元素添加到列表与在python中设置相比的时间复杂度

来自分类Dev

在将元素动态添加到另一个UL的无序列表中的同时添加图像

来自分类Dev

将元素添加到列表中:已添加的元素会无缘无故地随时间变化

来自分类Dev

如何将输入中的值和使用 JS 创建的元素添加到列表中?

来自分类Dev

将元素添加到mvc中的列表中的问题

来自分类Dev

将子项添加到元素为字符串和列表的组中

来自分类Dev

使用SQL查询和.NET将元素添加到列表中

来自分类Dev

将元素添加到列表中的特定位置

来自分类Dev

高效地将元素添加到python中的列表

来自分类Dev

将元素添加到不同位置的列表中

来自分类Dev

Scala将元素添加到Scala的Nil列表中

来自分类Dev

将元素添加到深度嵌套的列表中

来自分类Dev

将元素添加到类列表中

来自分类Dev

将元素添加到列表中的特定位置

来自分类Dev

以正确的顺序将元素添加到python中的列表

来自分类Dev

将某些冗长的元素添加到列表中

来自分类Dev

无法将元素添加到列表中

来自分类Dev

通过指令中的id将Class添加到元素

来自分类Dev

将签名添加到不带“ id”的XML元素中

来自分类Dev

将新元素添加到列表

来自分类Dev

将元素添加到子列表

来自分类Dev

将for循环元素添加到列表

来自分类Dev

将列表元素添加到csv

来自分类Dev

将元素添加到列表Java

来自分类Dev

将元素均匀添加到列表

来自分类Dev

Java将元素添加到列表

来自分类Dev

将元素添加到键值对列表

Related 相关文章

热门标签

归档