Form Repeat sortable support for drag/drop events

The sort option of a form repeat is great, but it could be enhanced by supporting the events that sortable provides, such as onEnd which triggers an event every time the user finishes dragging an element to a new position. I deal with this today, by simply turning off the sort option, and manually setting it up in js, but having in the UI would obviously be much better.

The use case is that when adding a new item into a form repeat, I need to prefill values on the inputs, so I use a data store as the source for the form repeat and add new entries as needed -- works great. But if the items are re-ordered, and then a new item is added, it will naturally revert to the original sort. So if I use an onEnd event to keep all of that in order.

@mebeingken, do you have an example of js code you used to grab the onEnd event? I’ve tried to using the sortableJs docs but nothing I’ve tried has worked.

Have a look at this @ben’s video here:
sorting - YouTube
after the sorting tutorial, at about 14’:30’’

1 Like

@famousmag thank you! Exactly what I was looking for and much easier than what I was trying. You rock @ben!

-Twitch

1 Like

Another example here

2 Likes

Hi Brian,

I have used this method on a variety of pages and find it works really well.

Do you or anyone else know if it would be possible to not have the need for the 'Sort' button, I'm thinking the form would get updated on the release of the mouse from the sorter button?

Thanks once again for all your great ideas and videos!

CK

Check for a "drag end" (or similar) dynamic event but i dont think there is one. Not near Wappler at present.

OK, will take a look

Mouse Out; set the action to submit the sort-order-form, works like a charm!

There is no need for a sorter button, you can use any element as long as it has a handle. As an example, set the handle on an image.

Instead of mouse-up, use dmx-on:end="..." as per the sorting-YouTube video

I really don't understand why we don't have this yet in Wappler, we've been asking for something like drag and drop for years now, why is it so complicated?

Why do we still have to do something as useful as this manually?

I am sorry, but I seem to be missing the point.

What is it that you are wanting the drag/drop events for that is not already catered for in Wappler?

I really wish the team would put a bit more love into extensions.

Something like this should be very easy to extend with custom extensions, especially now we have AI to do most of the coding for us.

Case in point: it took me 5 minutes with Claude to implement this feature request (excluding the UI)

  1. I asked Claude Opus 4 to extend the existing dmxFormRepeat.js
  2. I just copied the OP from Ken
  3. I replaced the code
  4. It works:
    CleanShot 2025-05-24 at 11.02.23

by adding: dmx-on:end="layoutNotifications.danger('onend event from the form repeat element')" to the formrepeat element.

Now where it becomes more work is to make this into a proper custom extension.

@George If we have easy access to the .hjson files of the existing components and improve the process a bit of making a custom appconnect extenion it would literally take me 15 minutes now to release a custom extension that fixes this feature request.

Then all the feature requests around extending existing components can basically be ignored as it's trivial to extend it ourselves. And you can focus on the stuff we can't implement.

PS @mebeingken here's the code: /*! Form Repeat Version: 2.0.3 (c) 2025 Wappler.io @build 2025-02-03 11: - Pastebin.com

@patrick will have a look of possibilities to implement this

The Sortable plugin already triggers the events in the DOM and dmx-on:end will work without needing to modify the extension.

The code you posted added a new attribute onend which is not the same as an actual event handler and it doesn't trigger the dmx-on:end. It will however overwrite the default onend attribute which would normally be the event handler for vanilla JavaScript. With your AI generated code you would add it like onend="layoutNotifications.danger('onend event from the form repeat element')".

1 Like

I've created an update that adds the following custom events: added, moved, removed and duplicated. They trigger after a user interaction like dragging or when one of the methods is called but not when data bindings are updated.

You can add them like dmx-on:moved="expression" for dynamic expressions or like onmoved="javascript" when using vanilla javascript.

Updated extension: dmxFormRepeat.zip (5.5 KB)

The end event from the Sortable (and all the other Sortable events) will also work since they are dispatched as DOM events.

For adding custom events on an App Connect component you do not register them as attributes like the AI did. Simply register the events like:

  events: {
    added: Event,
    moved: Event,
    removed: Event,
    duplicated: Event,
  },

and then dispatch them like

this.dispatchEvent('removed', null, { index });

That is all the code needed to dispatch custom event on an App Connect component.

2 Likes

@George @patrick
Any thoughts on making it easier to extend existing components?

Boilerplate for extending App Connect components is:

dmx.Component('form-repeat-ext', {
  extends: 'form-repeat',

  // extended properties and methods here
});

The above will create a new component named form-repeat-ext and will extend all properties/methods from form-repeat. With the above code it is simply a clone of form-repeat since no properties are extended.

With the new component events will become available in the UI. We will not add the end event or any of the other Sortable events to the UI, but they can be used when you add the event handler manually.