I have created a code snippet on CodePen that showcases a feature of the website I am currently developing: http://codepen.io/barrychapman/pen/BzJGbg?editors=1010
Upon loading the page, you will notice 6 boxes. The first box is active, while the remaining 5 are disabled. By clicking the Add to Metric 2
button, you can enable the second box.
After enabling, click the Resort
button to observe what happens. The field headers remain unlabeled to show you the behavior firsthand. The fields will now be rearranged as follows: [1,2,6,5,4,3].
Repeating the click on Resort
will correctly sort the fields as [1,2,3,4,5,6]. Another click reverses the order back to the initial state. Here is the specific code segment responsible for this functionality:
$('#target_metrics > div.disabled').each(
function() {
var $this = $(this);
$('#target_metrics').find('div.metric-template:not(.disabled)').last().after($this);
}
);
I am facing a challenge here. My goal is to maintain the field order consistent after any modifications such as adding/enabling/disabling/removing a field. This consistency is crucial due to various data elements associated with these nodes.
Update
To demonstrate an undesired behavior, follow these steps:
- Click
Resort
- Click
Add to Metric 2
Click
Resort
Observe that Metric 2 appears at the end of the row.
Click
Resort
Now, Metric 2 is in the correct position.
Click
Resort
once more.The other fields have now been resorted.