Button to move row up or down in table element

Hello, I’m trying to create a simple “arrow up” and “arrow down” button function to reorder rows in a table element and need some help with how I’m trying to do it or advice on a better way.

So each row is created with the entry of a new thing in the database, in the case of my app that thing is called a content block. On creation, each content block is given an index number and the table element is then sorted by index number. This then gives me my list of things on the app and the things which I would like to be able to reorder up or down in the list.

The first part of the workflow in which I’m moving a row is quite straight forward I think in that I simply make a change to the thing (content block) by doing a +1 or -1 on the index number as can be seen below:

For the second part of the workflow however, I need to make a change to either the row above the row being moved or the row below the row being moved, again I just need to make a change to the index number.

My problem is that I can’t work out the expression to use in order to select the row above or below the one which I am moving, for me to be able to change that records index number in the database.

So I’ve used the below for the second part of the workflow to move the row below the row that I’m moving. Not sure if the best way to do it but it works. Index numbers for the row I’m moving and the row below it basically swap.

However what I’m not understanding is that when I duplicate this for the second part of the workflow when moving a row up (instead of down), it doesn’t work.

Hello,
You can try to create a field indexNumber and juste sort your table with that field? Then you can use the expression “this current things”

So, I already have a data field on each thing called “Index Number” and thats why its not really an issue to change the index number of a particular row in the table element, its changing the index number of the row above or below that, in the same workflow, is the issue I’m having.

I wrote a step by step tutorial on doing that for repeating groups a long time ago. I hope it helps you. you’re right the tricky part are the rows around.

1 Like

Perfect, works like a charm, thanks so much!

1 Like

awesome, glad it helped!