fnRowCallback when one field is edited make one cell read only/ non-editable

fnRowCallback when one field is edited make one cell read only/ non-editable

YoDavishYoDavish Posts: 123Questions: 46Answers: 3

Running Datatable Editor v1.9.4

I'm trying to disable a cell from being editable once the completed column has been set to 1. I figured changing the desired cell to "Read only" might work, but it did not.

I have this code:

        fnRowCallback: function( nRow, aData, iDisplayIndex ) {
            var completedField = aData['completed'];
            if(completedField == 1){
                $('td:eq(14)', nRow).addClass("read_only");
            }
            return nRow;
        }

Which will make a "Note" column read only if the completed field is marked as 1. When I inspect a row with a completed field = 1, it does show in the browser elements that this specific cell has a "class = read_only", however, I'm still able to edit this cell via edit button or by AutoFill/Keys. Is there a way to do this?

This question has an accepted answers - jump to answer

Answers

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

    This example from this thread should help. It's disabling a field - the logic is different to what you want, but it should set you off in the right direction,

    Colin

  • YoDavishYoDavish Posts: 123Questions: 46Answers: 3

    @colin I almost got it to work. The only problem is that it disables all of the rows. Here is my current code,

    editor.on('preOpen', function(e, mode, action) {
        if (editor.field('completed').val() == 1) {
            editor.field('note').disable();
        }
    });
    

    How can I make it look at each row to disable?

  • YoDavishYoDavish Posts: 123Questions: 46Answers: 3

    Nvm I found out what it was:

    editor.on('preOpen', function(e, mode, action) {
        if (editor.field('completed').val() == 1) {
            editor.field('note').disable();
        }else{
            editor.field('note').enable();
        }
    });
    
  • YoDavishYoDavish Posts: 123Questions: 46Answers: 3

    @colin I'm running an issue where AutoFill/Keys isn't allowing me to change the completed field. Not sure why. It works just fine when I hit the "edit" button and change a completed field from 1 to 0 or vice versa.

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

    Is that the same issue you reported on that other thread?

    Colin

  • YoDavishYoDavish Posts: 123Questions: 46Answers: 3

    @colin I think they are related

  • YoDavishYoDavish Posts: 123Questions: 46Answers: 3

    @colin, if remove the

    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['currentUser']; ?>";
                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 );
    });
    

    I'm able to make changes to the editable fields via the Autofill/Key with no issues.

This discussion has been closed.