(数组)如何将一系列元素移动到另一个位置
我有一个动态html表,用php生成,数据来自mariadb
我需要一个算法(最好在sql中,或者在php中,如果没有)来重新排序行并提交...像这样(其中x =复选框,o = radiobutton):
range dest data
---------------
. . one
x . two
x o three
x . four
. . five
[submit move]
思路类似于excel>选择行>移动+插入行...首先选择一个源范围(我用js做的,只需选择第一个,最后一个,范围选择自身),然后选择一个插入目标位置,然后提交...在本例中结果正如
one
five
two
three
four
您所看到的,所选范围[二..四]已前进到指定位置,将覆盖的项目移动到范围左侧的空间,保留长度但仅更改索引...我想像这样的一些sql命令似乎
UPDATE my_table SET index=index+$diff WHERE index>=$start AND index<=$end;
很容易但并没有那么多(至少对我来说),因为有很多具有不同范围长度、位置等的不同情况...我找不到任何本机 sql 或 php 函数或片段来执行此操作
,如果您对某些相关算法有一些知识或想法,我将非常感激...并且原谅我糟糕的英语xD
i have a dynamic html table, generated with php, with data from mariadb
i need an algorithm (preferently in sql, or in php if not) to re-order the rows and submit... like this (where x = checkbox, o = radiobutton):
range dest data
---------------
. . one
x . two
x o three
x . four
. . five
[submit move]
the idea is similar to excel> select rows> move+insert rows... first you select a source RANGE (i did with js, just select first, last, and the range selects itself), then you select an insert destination position, then submit... in this example the result will be
one
five
two
three
four
as you can see the selected range [two..four] has advanced to specified position, shifting the overwriten items to space left by the range, preserving the length but just changing indexes... i imagine some sql commands like
UPDATE my_table SET index=index+$diff WHERE index>=$start AND index<=$end;
seems easy but is not so much (for me at least) since there are many different cases with different range lenghts, positions, etc... i couldn't found any native sql or php function or snippet to do it
please if you have some knowledge or idea about some related algorithm i will be very thankful ... and forgive my bad english xD
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于问题受欢迎xD,我试图自己找到答案...并分享它,希望对某人有用,并希望激励其他人找到更好的解决方案,
考虑这组初始元素(仅显示索引),并且我们想要将元素从 2 移动到 4 到位置 6...我们期望最终结果
我做的第一件事是计算源范围长度,即 4 -2 = 2
然后将源范围保存在临时表中,或者以某种方式标记元素,或者写下来唯一的 id 等...
直到索引可以简单地重复,想象一下我们拿走源范围并在列表中留下一个“长度+1”元素的“洞”
然后关键是移动剩余的将元素移动到另一侧“长度+1”位置,可以通过剩余元素减去“长度”来完成,
注意您移动的剩余项目是4,这是最后一个源元素之后和最后一个元素之间的差异目标位置...最后一个目标位置是目标+“长度” (6 +2 = 8)
如果移动方向相反(即源范围在其左侧),则必须相应地纠正所有范围、移位和操作,
现在您可以通过简单地更正将源范围设置为新位置将“剩余长度”(4) 添加到索引的索引
请注意,目标位置不能超出(第一个)和(最后一个 -“长度”)位置,即这种情况下的(1 和 7)...并且如果第一个源索引等于列表不会的目的地改变
所以得出结论,有 3 种情况的操作略有不同:
现在您可以在下面发布伪代码片段作为补充:) ...谢谢
due the question popularity xD i tried to find an answer myself... and share it with the hope will be useful to somebody, and hopufully motivate someone else to find a better solution
consider this initial set of elements (only indexes shown), and that we want to move elements from 2 to 4 to position 6... we expect a final result
first thing i did was to calculate the source range length, that is 4 -2 = 2
then saved the source range in a temp table, or by marking elements someway, or by writing down unique id's, etc...
until the indexes could simply be duplicated, imagine instead we take away the source range and left a "hole" of "length +1" elements in the list
then the key is to shift the remaining elements to the opposite side "length +1" positions, can be done by substracting "length" to remaining elements
take note the remaining items you shifted is 4, this is the difference between after the last source element and the last destination position... the last destination position is destination + "length" (6 +2 = 8)
in the case the movement is done to the opposite direction (i.e. the source range to their left) all ranges, shifts and operations must be corrected acordingly
now you can set source range to his new position by simply correcting the indices adding "remaining length" (4) to indexes
be aware destination position can't go outside (first) and (last -"length") positions, i.e. (1 and 7) for this case... and if first source index equals destination the list wouldn't change
so to conclude, there 3 cases with slightly different operations:
now you could post a pseudocode snippet below as a complement :) ... thanks