重新排序项目算法
我有一个 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为我倾向于剪切和粘贴方法,因为 js 框架使得移动 DOM 片段相对容易。
取决于方向以及我们是否已经处于极端)
I think I'm leaning toward a cut-and-paste approach, since the js frameworks make moving pieces of DOM around relatively easy.
depending on direction and whether or not we're at the extreme end already)
这看起来像是一个多键排序任务。 您可以使用 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.