Auto populate datetimestamp and username if adding notes to cell

Auto populate datetimestamp and username if adding notes to cell

YoDavishYoDavish Posts: 123Questions: 46Answers: 3

Running Editor 1.9.4 using AutoFill/Keys. I was wonder if there a way to auto populate a date timestamp and username in a cell when a user clicks into a specific editable cell to add notes/comments. For example, when a user double clicks into "Note" cell (AutoFill/Keys) it generates today's date timestamp along with their username. The user would then only need to add their note/comment. Or if the user clicks on the edit button and then clicks into the "Note" input field which then triggers the same auto date timestamp/ username?

This question has accepted answers - jump to:

Answers

  • tangerinetangerine Posts: 3,342Questions: 35Answers: 394
    edited November 2020

    If the timestamp is always NOW(), just let the database take care of it. It needn't appear on the table at all. Alternatively, give it a read-only default value equal to NOW().
    Regarding the user id, are you keeping track of users in the session?

  • YoDavishYoDavish Posts: 123Questions: 46Answers: 3
    edited November 2020

    @tangerine yep PHP Session, only problem is this field can be alter once or multiple times and we want to keep all timestamps. For Example:

    11/01/2020 [username] Note 1
    11/02/2020 [username] Note 2
    11/07/2020 [username] Note 3

  • colincolin Posts: 15,112Questions: 1Answers: 2,583
    Answer ✓

    This example from this thread should help. It's showing how to change a cell's value when editing starts.

    Colin

  • YoDavishYoDavish Posts: 123Questions: 46Answers: 3

    @colin Thanks for the example link, I've got it working partially, only when I select the first column, hit the edit button, and make changes, then hit the update button will it persist the changes. However, using AutoFill/Key it will not persist the changes to the database. This is the js I've added:

    editor.on( 'initEdit', function ( e, node, data, items, type ) {
        editor.one('open', function () {
            editor.displayed().forEach(function(a) {
                var oldNotes = editor.field('note').val();
                var currentUser = "<?php echo $_SESSION['user']; ?>";
                var userInitials = currentUser.substr(0,2);
                var m = new Date();
                var dateString =
                    m.getUTCFullYear() + "/" +
                    ("0" + (m.getUTCMonth()+1)).slice(-2) + "/" +
                    ("0" + m.getUTCDate()).slice(-2) + " " +
                    ("0" + m.getUTCHours()).slice(-2) + ":" +
                    ("0" + m.getUTCMinutes()).slice(-2) + ":" +
                    ("0" + m.getUTCSeconds()).slice(-2);
                if (editor.field('completed').val() == 0 && a == 'note') {
                    editor.field('note').val(oldNotes + String.fromCharCode(13) + dateString + ' ' + userInitials.toUpperCase());
                }
            });
        });
    });
    
    $('#table').on( 'click', 'tbody td', function () {
        editor.inline( this );
    });
    

    What else am I missing?

  • YoDavishYoDavish Posts: 123Questions: 46Answers: 3
    Answer ✓

    @colin Looks like I found the solution, I've tested it a handful of times, but I removed this portion of the code:

    $('#table').on( 'click', 'tbody td', function () {
        editor.inline( this );
    });
    

    And I've been able to save edits both via edit buttons or using AutoFill/Key on the two editable fields

  • colincolin Posts: 15,112Questions: 1Answers: 2,583

    Excellent, thanks for reporting back,

    Colin

This discussion has been closed.