jquery-sortable 使用链表的行为

发布于 2024-09-04 10:20:13 字数 516 浏览 4 评论 0原文

我怀疑我没有以正确的方式看待这个问题,所以就这样吧。

我本质上在网页上有一个数据的 LinkedList (http://en.wikipedia.org/wiki/ Linked_list),出于性能原因,我想使用传统的链接列表行为进行操作(即仅更新“下一个”对象的引用/id)。

有点棘手的地方是我理想地希望使用 Jquery 的可排序来做到这一点。就像用户向上/向下拖动某些东西一样,我可以使用移动的对象的 id 和该对象的新父 id 对服务器进行 Ajax 调用(然后在幕后我可以弄清楚如何重新连接事物) ..也许需要更多的数据......)。

但是我见过的每个使用可排序的示例都将整个重新索引的列表发送到数据库进行更新,这对我来说似乎没有必要。使用链接列表来更改元素的“索引”,我只需要进行 3 次更新,这取决于列表的大小,这可能会节省很大的性能。有人有我想做的事情的例子吗……我在左外野太远了吗?

I suspect I'm not looking at this issue in the right way so here goes.

I have essentially a LinkedList of data on a web page (http://en.wikipedia.org/wiki/Linked_list) that I'd like to manipulate using traditional Linked List behavior (i.e. just updating the reference/id of the "next" object) for performance reasons.

Where this gets a bit tricky is I'd ideally like to use Jquery's sortable to do this. Like the user would drag something up/down and I could just do an Ajax call to the server with the id of the object that moved and the new parent id of that object (and then behind the scenes I could figure out how to reconnect things..maybe need more data than that...).

But every example I've seen where sortable is used they were sending the whole re-indexed list to the database to update which seems unnecessary to me. With a linked list to change an element's "index" I only need to make 3 updates which depending on the size of the list could be a big performance savings. Anyone have an example of what I'm trying to do...am I too far in left field?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

能怎样 2024-09-11 10:20:13

我通过连接可排序上的“开始”和“更新”事件来解决这个问题,以找出哪个项目已移至何处,然后仅将该索引发送到服务器。

var items;

$('ul#sortable').sortable({
    start: function() {
        items = $('li.item:not(.ui-sortable-placeholder)', this);
    },
    update: function() {
        $('li.item:not(.ui-sortable-placeholder)', this).each(function(i) {
            var order = items.index(this);
            if (order > i + 1 || order < i) {
                // do ajax request here
                console.log('item %d was moved to %d', order, i);
                return false;
            }
        });
    }
});

结果用于对存储在服务器上的列表进行重新排序,这在 Python 中很简单:

items.insert(index2, items.pop(index1))

I solved this by hooking onto the 'start' and 'update' events on sortable to figure out which item had moved to where, and then only sending that index to the server.

var items;

$('ul#sortable').sortable({
    start: function() {
        items = $('li.item:not(.ui-sortable-placeholder)', this);
    },
    update: function() {
        $('li.item:not(.ui-sortable-placeholder)', this).each(function(i) {
            var order = items.index(this);
            if (order > i + 1 || order < i) {
                // do ajax request here
                console.log('item %d was moved to %d', order, i);
                return false;
            }
        });
    }
});

The result was used to reorder a list stored on the server, which in Python is as simple as

items.insert(index2, items.pop(index1))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文