Skip to content

Commit 5202179

Browse files
jbonorclaude
andcommitted
Fix field drag-and-drop reorder with 0-based contiguous rankings
JS now reads data-ranking from table rows instead of using DOM indices, decoupling REDIPS row positions from backend ranking values. Migration normalizes rankings to contiguous 0-based values per container. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 67e146d commit 5202179

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

inc/field.class.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,30 @@ public static function installBaseData(Migration $migration, $version)
153153
$migration->addConfig(['stable_search_options' => 'yes'], 'plugin:fields');
154154
}
155155

156+
// Normalize rankings to be contiguous (0-based) per container
157+
if (Config::getConfigurationValue('plugin:fields', 'contiguous_rankings') !== 'yes') {
158+
$containers = $DB->request([
159+
'SELECT' => 'plugin_fields_containers_id',
160+
'DISTINCT' => true,
161+
'FROM' => $table,
162+
]);
163+
foreach ($containers as $row) {
164+
$cid = $row['plugin_fields_containers_id'];
165+
$fields = $DB->request([
166+
'SELECT' => 'id',
167+
'FROM' => $table,
168+
'WHERE' => ['plugin_fields_containers_id' => $cid],
169+
'ORDER' => 'ranking ASC',
170+
]);
171+
$rank = 0;
172+
foreach ($fields as $field) {
173+
$DB->update($table, ['ranking' => $rank], ['id' => $field['id']]);
174+
$rank++;
175+
}
176+
}
177+
$migration->addConfig(['contiguous_rankings' => 'yes'], 'plugin:fields');
178+
}
179+
156180
return true;
157181
}
158182

@@ -653,7 +677,7 @@ public function showSummary($container)
653677

654678
foreach ($iterator as $data) {
655679
if ($this->getFromDB($data['id'])) {
656-
echo "<tr class='tab_bg_2' style='cursor:pointer'>";
680+
echo "<tr class='tab_bg_2' style='cursor:pointer' data-ranking='" . $this->fields['ranking'] . "'>";
657681

658682
echo '<td>';
659683
$label = empty($this->fields['label']) ? NOT_AVAILABLE : $this->fields['label'];

public/js/drag-field-row.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,16 @@ redipsInit = function () {
4141
rd.event.rowDroppedBefore = function (sourceTable, sourceRowIndex) {
4242
var pos = rd.getPosition();
4343

44-
var old_index = sourceRowIndex;
45-
var new_index = pos[1];
46-
var container = document.getElementById('plugin_fields_containers_id').value;
44+
var old_ranking = sourceTable.rows[sourceRowIndex].dataset.ranking;
45+
var new_ranking = sourceTable.rows[pos[1]].dataset.ranking;
46+
var container = document.getElementById('plugin_fields_containers_id').value;
4747

4848
jQuery.ajax({
4949
type: "POST",
5050
url: "../ajax/reorder.php",
5151
data: {
52-
old_order: old_index,
53-
new_order: new_index,
52+
old_order: old_ranking,
53+
new_order: new_ranking,
5454
container_id: container
5555
}
5656
})

0 commit comments

Comments
 (0)