How do you reverse the rowOrdering sequence so that the new item get added as a last item?

How do you reverse the rowOrdering sequence so that the new item get added as a last item?

koniahinkoniahin Posts: 186Questions: 39Answers: 7

My form code:

  {
    label: "Order",
    name: "articles.rowOrder",
    type: "hidden",
    default: 1,
  },

How do you reverse the ordering sequence so that when you add a new item it gets the rowORder # as the highest number rather than #1 (which pushes everything else down)?

  // ROWORDER BLOCK
  ->on('preCreate', function ($editor, $values) {
    if (!$values['articles']['rowOrder']) {
      $next = $editor->db()->sql('select IFNULL(MAX(rowOrder)+1, 1) as next FROM articles')->fetch();
      $editor->field('articles.rowOrder')->setValue($next['next']);
    } else {
      $editor->db()
      ->query('update', 'articles')
      ->set('rowOrder', 'rowOrder+1', false)
      ->where('rowOrder', $values['articles']['rowOrder'], '>=')
      ->exec();
    }
  })
  ->on('preRemove', function ($editor, $id, $values) {
    $order = $editor->db()
    ->select('articles', 'rowOrder', array('id' => $id))
    ->fetch();

    $editor->db()
    ->query('update', 'articles')
    ->set('rowOrder', 'rowOrder-1', false)
    ->where('rowOrder', $order['rowOrder'], '>')
    ->exec();
  })
  // ROWORDER BLOCK

Answers

  • allanallan Posts: 61,972Questions: 1Answers: 10,160 Site admin

    That looks like it should basically do what you are looking for. When $values['articles']['rowOrder'] is submitted as 1 (which it is by default there) then all others will have their order index incremented by 1.

    Is that not happening for you? What does happen? What happens when you reload the page, is it then correct?

    Allan

  • koniahinkoniahin Posts: 186Questions: 39Answers: 7
    edited May 2022

    I think you misunderstand. The way it works now when the new row is added it gets assigned rowOrder ID of 1 and all others items are incremented by 1. That is fine for most forms but I have a couple where that doesn't fit the need. Say there are 20 rows in the table. Rather than getting assigned rowOrder ID 1 we need the new row to be added as # 21. Place it at the bottom of the list and do not disturb the numbers of the others.

    The problem this creates is that I have to then manually drag/drop the new row to the bottom. With a few rows that is not an issue but with lots of rows that is just a bit more time-consuming and seemingly unnecessary.

  • colincolin Posts: 15,177Questions: 1Answers: 2,590

    You could use preOpen to trigger when a new record is created (action will be "create") - and then set the field with set() - the issue is what you set the field with. You could either count the rows on the client (rows() with count()), or issue an ajax call back to the server to get the next value.

    Would that do the trick?

    Colin

  • koniahinkoniahin Posts: 186Questions: 39Answers: 7

    Honestly what you are saying goes way way over my skill level. If you can provide a working example then I can possibly follow that.

  • allanallan Posts: 61,972Questions: 1Answers: 10,160 Site admin

    If we take this example as the basis, could you describe how yours is different please? For example if I were to add a new row with an order one more than the max there, then it will work as you describe. Are you looking for that to be automatic if a new row is created with an empty value or something else? I'm just not sure how what you are looking for isn't done in that example.

    Allan

Sign In or Register to comment.