(数组)如何将一系列元素移动到另一个位置

发布于 2025-01-13 23:29:17 字数 735 浏览 0 评论 0原文

我有一个动态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 技术交流群。

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

发布评论

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

评论(1

你没皮卡萌 2025-01-20 23:29:18

由于问题受欢迎xD,我试图自己找到答案...并分享它,希望对某人有用,并希望激励其他人找到更好的解决方案,

考虑这组初始元素(仅显示索引),并且我们想要将元素从 2 移动到 4 到位置 6...我们期望最终结果

orig  1 2 3 4 5 6 7 8 9
src     2 3 4
dest            6
final 1 5 6 7 8 2 3 4 9

我做的第一件事是计算源范围长度,即 4 -2 = 2

然后将源范围保存在临时表中,或者以某种方式标记元素,或者写下来唯一的 id 等...

直到索引可以简单地重复,想象一下我们拿走源范围并在列表中留下一个“长度+1”元素的“洞”

  2 3 4
  ^ ^ ^
1 . . . 5 6 7 8 9

然后关键是移动剩余的将元素移动到另一侧“长度+1”位置,可以通过剩余元素减去“长度”来完成,

          < < <
1 5 6 7 8 . . . 9

注意您移动的剩余项目是4,这是最后一个源元素之后和最后一个元素之间的差异目标位置...最后一个目标位置是目标+“长度” (6 +2 = 8)

如果移动方向相反(即源范围在其左侧),则必须相应地纠正所有范围、移位和操作,

现在您可以通过简单地更正将源范围设置为新位置将“剩余长度”(4) 添加到索引的索引

  > > > > 2 3 4
1 5 6 7 8 . . . 9

请注意,目标位置不能超出(第一个)和(最后一个 -“长度”)位置,即这种情况下的(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

orig  1 2 3 4 5 6 7 8 9
src     2 3 4
dest            6
final 1 5 6 7 8 2 3 4 9

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

  2 3 4
  ^ ^ ^
1 . . . 5 6 7 8 9

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

          < < <
1 5 6 7 8 . . . 9

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

  > > > > 2 3 4
1 5 6 7 8 . . . 9

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:

  • move the source range to their right
  • move it to same position (do nothing)
  • move it to left

now you could post a pseudocode snippet below as a complement :) ... thanks

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