重新排序项目算法

发布于 2024-07-15 05:21:45 字数 528 浏览 15 评论 0原文

我有一个 html 表:

up | dn
[ ]  Item 1
[x]  Item 2
[ ]  Item 3
[ ]  Item 4
[x]  Item 5
[ ]  Item 6
[ ]  Item 7

If 2 & 5 被勾选,我点击up,结果是:

up | dn
[x]  Item 2
[x]  Item 5
[ ]  Item 1
[ ]  Item 3
[ ]  Item 4
[ ]  Item 6
[ ]  Item 7

如果我点击dn,结果是:

up | dn
[ ]  Item 1
[ ]  Item 3
[ ]  Item 4
[ ]  Item 6
[x]  Item 2
[x]  Item 5
[ ]  Item 7

换句话说,项目按照选择的方向分组,然后在该方向移动一行。 有人对此有好的算法吗? 我确信我可以毫无困难地写出一些东西,但这似乎是那种应该“在那里”的东西......

I've got an html table thus:

up | dn
[ ]  Item 1
[x]  Item 2
[ ]  Item 3
[ ]  Item 4
[x]  Item 5
[ ]  Item 6
[ ]  Item 7

If 2 & 5 are checked and I click up, the result is:

up | dn
[x]  Item 2
[x]  Item 5
[ ]  Item 1
[ ]  Item 3
[ ]  Item 4
[ ]  Item 6
[ ]  Item 7

If I click dn, the result is:

up | dn
[ ]  Item 1
[ ]  Item 3
[ ]  Item 4
[ ]  Item 6
[x]  Item 2
[x]  Item 5
[ ]  Item 7

In other words, the items are grouped in the chosen direction, and then moved one row in that direction. Anyone have a good algorithm for this? I'm sure I can write something without much difficulty, but it seems like the kind of thing that should be "out there"....

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

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

发布评论

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

评论(2

忆沫 2024-07-22 05:21:45

我认为我倾向于剪切和粘贴方法,因为 js 框架使得移动 DOM 片段相对容易。

  • 获取已检查项目的行
  • 获取{top}最已检查项目的索引
  • 删除行& 将它们插入到表中的{index-1}处
  • (当然顶部可能是底部,而index-1更复杂
    取决于方向以及我们是否已经处于极端)

I think I'm leaning toward a cut-and-paste approach, since the js frameworks make moving pieces of DOM around relatively easy.

  • get checked items' rows
  • get index of {top}-most checked item
  • remove rows & insert them into table at {index-1}
  • (of course top might be bottom, and index-1 is more complex
    depending on direction and whether or not we're at the extreme end already)
笑忘罢 2024-07-22 05:21:45

这看起来像是一个多键排序任务。 您可以使用 jQuery UI 的 Sortable 界面。 您的排序条件类似于:a.selected <=> b.选定&& a.index <=>; b.索引。

忽略:您想要使用Fisher Yates Shuffle 算法

This looks like a multi-keyed sorting task. You could use something like jQuery UI's Sortable interface. Your sort condition would be something like: a.selected <=> b.selected && a.index <=> b.index.

Disregard: You want to use the Fisher Yates Shuffle algorithm.

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