Can the table *not* refresh after an edit?

Can the table *not* refresh after an edit?

globalplaneglobalplane Posts: 70Questions: 12Answers: 0
edited May 2023 in Free community support

I have a DataTables+Editor table with rows that have three inline editable fields. I can think of two situations where I would not want the table to refresh after making an edit:

  • I have a filter on one of the columns through SearchPanes. I edit that column for one row. That row disappears after the edit because it no longer belongs to the current filter. But I still wanted to make an edit on a different column of that row. So now I have to change the filter to go find the row again.

  • Again, filtered through SearchPanes. I make an edit, but click the wrong option in select by mistake. The row disappears, so I have to change the filter to go find it again to fix the mistake.

Ideally, after the edit, the Datatables table would receive confirmation that the edit went through so that it can show the new value, but the row wouldn't disappear or be sorted to a new location until the user click a reload button or changed the filter.

Is this possible?

Answers

  • kthorngrenkthorngren Posts: 20,777Questions: 26Answers: 4,857

    Check out this thread to see if it helps.

    Kevin

  • globalplaneglobalplane Posts: 70Questions: 12Answers: 0
    edited May 2023

    Hmm, it seems like that's the option I need, but it isn't working. drawType: none does do something in that the database is updated with the new value, while the old value is shown on the browser and the row stays put.

    But I want the new value to be shown and the row to stay put. When I try drawType: page it's the old behavior, where it re-sorts after an edit.

    Here's the page with drawType: page enabled, if you want to look at the code. (I'm on a phone so it's tough to copy here.) Editing won't work, though, unless you're logged in.

    https://comprehensibleinputwiki.com/ciwlibrary/vidadmin.php

    Also, I couldn't find details on the page: current that the other thread mentioned.

  • globalplaneglobalplane Posts: 70Questions: 12Answers: 0

    Wait, that's weird. With drawType: none, it works how I want, but only after I edit the cell to the new value twice. Then it shows the row with the new value but still in the same position.

  • allanallan Posts: 62,448Questions: 1Answers: 10,251 Site admin
    edited May 2023

    You have server-side processing enabled, so any display of data needs to come from the server. The drawType: 'none' option for Editor will simply disable Editor writing to the client-side fields, since the next thing that would happen would be a round trip to the server to get the new data.

    edit This paragraph is wrong - see next post. Perhaps in server-side processing mode and when drawType is none then it should update the table... I've got that noted for the next release. If you want to get that working locally, in the Editor source search for:

            // No point in doing anything when server-side processing - the commit
            // will redraw the table
            if (! _dtIsSsp(dt, this) || this.s.editOpts.drawType === 'none') {
    

    and remove the first part of that condition. That isn't exactly what I described, but it will do in your use case.

    Regards,
    Allan

  • allanallan Posts: 62,448Questions: 1Answers: 10,251 Site admin

    Hmmm - I was editing the code, when I realised that the second part of the condition should actually handle this case already - so ignore what I said above.

    Can you give me write access to your page and also use the non-min version of DataTables.js please? I'll see what is going on here.

    Allan

  • globalplaneglobalplane Posts: 70Questions: 12Answers: 0

    Sure, let me code up something to give you write access.

  • globalplaneglobalplane Posts: 70Questions: 12Answers: 0

    Ok, you can just make edits without logging in now. Feel free to edit anything, I'll just undo the edits from non-logged in users later.

  • allanallan Posts: 62,448Questions: 1Answers: 10,251 Site admin

    Something else going on here. If I take the "Portuguese Digs" channel and have it with difficulty not set when I load the page, then edit the difficulty:

    {
        "data[row_90209][difficulty_id]": "1",
        "action": "edit"
    }
    

    However, the response from the server contains:

                "difficulty": "* Not Set",
                "difficulty_id": -1,
    

    However, if I then reload the page I can see that the difficulty was set.

    So the issue is that the difficulty is being set either after the server-side libraries have read the value, or it is being set in a different transaction.

    I'd need to see your PHP code to know for certain, but a quick thing you could try (if it is a transaction issue) is to add ->transaction(false) just before the ->process(...) call. It isn't the correct fix, but it would let us check without me seeing the code.

    Allan

  • globalplaneglobalplane Posts: 70Questions: 12Answers: 0

    Oh, ok, so it's probably because I'm using functionality similar to the logging changes example to edit the rows, instead of directly through the library.

    I set transaction to false, and here is the php code:

    <?php
    ini_set('display_errors', 1);
    ini_set('display_startup_errors', 1);
    error_reporting(E_ALL);
    
    header('Content-Type: application/json; charset=utf-8');
    
    session_start();
    
    if(!isset($_SESSION['mw_id'])) {
        $_SESSION['mw_id'] = 1111;
    }
    
    include( "/var/www/comprehensibleinputwiki.com/html/editor-php/lib/DataTables.php" );
    
    include_once('./dbconn.php');
    include_once('./account.php'); 
    
    function logChange ( $db, $action, $id, &$values ) {
    
        if(isset($values['difficulty_id'])){
            $db->insert( 'mc_revs_level', array(
                'mw_id'   => $_SESSION['mw_id'],
                'row_index' => $id,
                'value' => $values['difficulty_id']
            ) );
        }
    
        if(isset($values['language_id'])){
            $db->insert( 'mc_revs_language_id', array(
                'mw_id'   => $_SESSION['mw_id'],
                'row_index' => $id,
                'value' => $values['language_id']
            ) );
        }
    
        if(isset($values['enabled_id'])){
            $db->insert( 'mc_revs_enabled', array(
                'mw_id'   => $_SESSION['mw_id'],
                'row_index' => $id,
                'value' => $values['enabled_id']
            ) );
        }
    }
    
    
     
    use 
        DataTables\Editor,
        DataTables\Editor\Field,
        DataTables\Editor\Format,
        DataTables\Editor\Mjoin,
        DataTables\Editor\Options,
        DataTables\Editor\Upload,
        DataTables\Editor\Validate,
        DataTables\Editor\ValidateOptions,
        DataTables\Editor\SearchPaneOptions;
    
    $editor = Editor::inst( $db, 'main_collection', 'main_index' )
        ->fields(
            Field::inst( 'main_collection.main_index' ),
            Field::inst( 'main_collection.id', 'vid')
                ->getFormatter( function ( $val, $data ) {
                    return '<iframe height="200" src="https://www.youtube-nocookie.com/embed/' . $val . '" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen></iframe>';
                } ),
            Field::inst( 'yt_channel_ids.channelTitle', 'channelTitle' )
                ->searchPaneOptions( SearchPaneOptions::inst() ),
            
    
            // Language
            Field::inst( 'IFNULL(languages.language_name, IFNULL(DL.language_name, "* Not Set"))', 'language_name')            
            ->searchPaneOptions( SearchPaneOptions::inst() ),
    
            Field::inst( 'IFNULL(mc_revs_language_id.value, IFNULL(DL.language_id, -1))', 'language_id')
                ->validator( Validate::numeric() )
                ->options( Options::inst()
                    ->table( 'languages' )
                    ->value( 'languages.language_id' )
                    ->label( 'languages.language_name' )
                ),
    
    
            // Title
            Field::inst( 'main_collection.title', 'title' ),
    
    
            // Difficulty
            Field::inst( 'IFNULL(difficulty.full, "* Not Set")', 'difficulty')
            ->searchPaneOptions( SearchPaneOptions::inst() ),
    
            Field::inst( 'IFNULL(mc_revs_level.value, -1)', 'difficulty_id')
                ->validator( Validate::numeric() )
    
                ->options( Options::inst()
                    ->table( 'difficulty' )
                    ->value( 'difficulty.difficulty_index' )
                    ->label( 'difficulty.full' )
                    ->order('difficulty.list_order')
                ),
    
            // Enabled
    
            Field::inst( 'IFNULL(status_labels.status_label, "Unreviewed")', 'enabled' )
            ->searchPaneOptions( SearchPaneOptions::inst()),
    
            Field::inst( 'IFNULL(mc_revs_enabled.value, -1)', 'enabled_id' )
                ->validator( Validate::numeric() )
                ->options( Options::inst()
                    ->table( 'status_labels' )
                    ->value( 'status_labels.status_id' )
                    ->label( 'status_labels.status_label' )
                ),
    
        )
        ->validator( function ( $editor, $action, $data ) {
            if ( $action !== Editor::ACTION_READ && $_SESSION['loggedin'] == false) {
                return true;
                return 'Please <a href="/oauth.php?prev=/ciwlibrary/vidadmin.php">log in</a> first.';
            } else {
                return true;
            }
        } )
    
        ->leftJoin(
            'mc_revs_language_id',
            'mc_revs_language_id.row_index = main_collection.main_index AND mc_revs_language_id.timestamp = (SELECT MAX(LIZ.timestamp) FROM mc_revs_language_id LIZ WHERE LIZ.row_index = main_collection.main_index)'
        )
        ->leftJoin(
            'mc_revs_level',
            'mc_revs_level.row_index = main_collection.main_index AND mc_revs_level.timestamp = (SELECT MAX(LZ.timestamp) FROM mc_revs_level LZ WHERE LZ.row_index = main_collection.main_index)'
        )
        ->leftJoin(
            'mc_revs_enabled',
            'mc_revs_enabled.row_index = main_collection.main_index AND mc_revs_enabled.timestamp = (SELECT MAX(EZ.timestamp) FROM mc_revs_enabled EZ WHERE EZ.row_index = main_collection.main_index)'
        )
        ->leftJoin(
            'yt_channel_ids',
            'yt_channel_ids.channelId = main_collection.channel'
        )
        ->leftJoin(
            'languages',
            'languages.language_id = mc_revs_language_id.value'
        )
        ->leftJoin(
            'difficulty',
            'difficulty.difficulty_index = mc_revs_level.value'
        )
        ->leftJoin(
            'status_labels',
            'mc_revs_enabled.value = status_labels.status_id'
        )
        ->leftJoin(
            'languages DL',
            'DL.language_id = yt_channel_ids.default_channel_language'
        )
    
        ->on( 'postEdit', function ( $editor, $id, &$values, &$row ) {
            logChange( $editor->db(), 'insert', $id, $values );
        } )
        
        
        ->write( true )
        ->debug(true, './debug.log')
        ->transaction(false)
        ->process( $_POST )
        ->json();
    
  • globalplaneglobalplane Posts: 70Questions: 12Answers: 0

    The data for the table is coming from the secondary tables which are updated in logChange().

Sign In or Register to comment.