Jump to content

[Suggestions] - Ordering Items Dynamically


Adamhumbug
Go to solution Solved by Barand,

Recommended Posts

Hi All,

I am looking for some suggestions here on a good way to order items.

I have a table that has an id, name and display order columns.

I am looking for suggestions on the best way (in a ui) to set the display order of each item without having the same value as any other item.

So the items table looks like this.

Item Name    -     Display Order

Item 1.            -     1

Item 2.           -     2

Item 3.           -     3

I want to be able to change the display order and automatically rearrange everything else.

I can think of many crude ways to change the order but none that would update the rest.

I want to be able to add Item 4 and it have display order 2 - item2 and item3 would have their display orders increased to move down.

As always, appreciate your input.

Link to comment
Share on other sites

  • Solution
3 minutes ago, Adamhumbug said:

I want to be able to add Item 4 and it have display order 2 - item2 and item3 would have their display orders increased to move down.


 

-- first
UPDATE thetable
       SET display_order = display_order + 1
       WHERE display_order >= 2;

-- then
INSERT INTO thetable (item_name, display_order)
       VALUES ('Item 4', 2);

 

Link to comment
Share on other sites

It gets a little more complicated if you want to automatically reorder things when a new item is put into a display slot that already contains an item, but honestly not a whole lot. If this is a one-off though, yeah - you're done now.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.