rowReorder question

rowReorder question

MarkoRvMarkoRv Posts: 2Questions: 1Answers: 0

Hi there,
I saw that the rowReorder has a common issue since it's limited to reorder only within one results page.
I think I have a good idea on how to make it possible to reorder within the whole table.
If you need to move one row outside the current page, you could select the row and click on EDIT button and then you could change the ORDER field value to the needed one.
This is possible if we remove code that is disabling this field.

The only problem is that when we edit the ORDER value we have a duplicate value since the "make space" functions are not triggered so the values of the affected rows remain the same.

Is there a way to trigger those functions when I edit the value of somehow simulate the reorder event that is triggered when we do a drag and drop ?

I think this would be a really functional fix since the drag and drop reorder can still be there if you need to move a row somewhere close (withing the page).
Thanks
Marko

Answers

  • MarkoRvMarkoRv Posts: 2Questions: 1Answers: 0
    edited March 2020

    I think I managed to solve it.
    I updated:
    1. The php file that updated/gets the data - which is called via ajax.
    2. The "inline" javascript used to initialise the table

    PHP changes:
    Added the preEdit function that "makes space" for the "new order value".

    Editor::inst( $db, 'products_test' )
    ->fields(
    Field::inst( 'order_seq' )
    ->validator( Validate::notEmpty() )
    ->validator( Validate::minNum( 1 ) ),
    Field::inst( 'pr_name' )
    ->set( false ),
    Field::inst( 'active' )
    ->set( false )
    )

    ->on( 'preEdit', function ( $editor, $id, $values ) {
        $order = $editor->db()
            ->select( 'products_test', 'order_seq', array('id' => $id) )
            ->fetch();
    
        $old_order = $order['order_seq'];
        $new_order = $values['order_seq'];
    
        if ($new_order > $old_order){
            $editor->db()
            ->query( 'update', 'products_test' )
            ->set( 'order_seq', 'order_seq-1', false )
            ->where( 'order_seq', $old_order, '>' )
            ->where( 'order_seq', $new_order, '<=' )
            ->exec();
        }
        else if ($new_order < $old_order){
            $editor->db()
            ->query( 'update', 'products_test' )
            ->set( 'order_seq', 'order_seq+1', false )
            ->where( 'order_seq', $new_order, '>=' )
            ->where( 'order_seq', $old_order, '<' )
            ->exec();
        }
    })
    

    JAVASCRIPT CHANGES:
    Added the postEdit event to refresh the table data.

    editor
    .on( 'postEdit', function () {
    // After create or edit, a number of other rows might have been effected -
    // so we need to reload the table, keeping the paging in the current position
    table.ajax.reload( null, false );
    })

            });
    

    I tested and for my scenarios it works.
    Additionally I will have to code that the value you type for the new order value is within the current order range.
    Do you think this is a good fix for the common problem "no possibility to move outside
    the current page" ?

  • allanallan Posts: 61,726Questions: 1Answers: 10,109 Site admin

    Yes, that looks like a nice workaround thanks for sharing that.

    What I'd been thinking of was a way to hover over the left or right of the table and have that move the paging while still dragging. But that is far from ideal and doesn't work with every layout.

    Regards,
    Allan

This discussion has been closed.