Update cell with value from javascript

Update cell with value from javascript

r-daddyr-daddy Posts: 3Questions: 1Answers: 0

I'm trying to add "updated_by" as hidden column that pulls a value from a div ($('#fullName').innerHTML;) when the row is updated.

I tried adding
"type": "hidden", and "visible": "false" to the updated_by field but it was still showing in the front end.

To get the value from the div in the "updated_by" column I tried adding a render function to the field, but it was showing in all the rows and not in the one that was just edited.

Below my current code.

`(function($){

$(document).ready(function() {
    var editor = new $.fn.dataTable.Editor( {
        ajax: 'php/table.MyTable.php',
        table: '#MyTable',
        fields: [
            {
                "label": "full_name:",
                "name": "full_name",
                "type": "display",
            },
            {
                "label": "phone_number:",
                "name": "phone_number",
                "type": "display"
            },
            {
                "label": "comments:",
                "name": "comments",
            },
            {
                "label": "updated:",
                "name": "updated_by"
            }
        ]
    } );
    $('#MyTable').on( 'click', 'tbody td:not(:first-child)', function (e) {
        editor.inline( this, {
            onBlur: 'submit'
        } );    } );
    var table = $('#MyTable').DataTable( {
        dom: 'Bfrtip',
        ajax: 'php/table.MyTable.php',
        columns: [
            {
                "data": "full_name"
            },
            {
                "data": "phone_number"
            },
            {
                "data": "comments"
            },
            {
                "data": "updated_by",
            }
        ],
        select: false,
        lengthChange: false,
        buttons: [
        ],
        pageLength: 20,

    } );
} );

}(jQuery));

`

Any help will be appreciated.

This question has accepted answers - jump to:

Answers

  • colincolin Posts: 15,240Questions: 1Answers: 2,599
    Answer ✓

    You could do something like this here. In that example, it's changing the case of a string before submitting, you could use the same structure but add your div value into the field prior to the submission,

    Colin

  • r-daddyr-daddy Posts: 3Questions: 1Answers: 0
    edited August 2021

    Thanks @colin The following did the trick:

    $('#MyTable').on( 'click', 'tbody td:not(:first-child)', function (e) {
            editor.inline( this, {onBlur: 'submit'} ); 
    
            editor.on( 'preSubmit', function ( e, o, action ) {
                    o.data[Object.keys(o.data)].updated_by = $('#fullName').innerHTML;
            } ); 
    
    });
    

    Still trying to hide the column.

  • colincolin Posts: 15,240Questions: 1Answers: 2,599
    Answer ✓

    Ah sorry, I missed that. To hide it in the table, you can use columns.visible, and to remove it from the Editor form, you can do something like:

      editor.on('preOpen', function(e, mode, action) {
          editor.field('field_name').hide();
      });
    

    Colin

  • r-daddyr-daddy Posts: 3Questions: 1Answers: 0

    Thanks again Colin. I managed to get it working with
    columnDefs: [ { "visible" : false, "targets": 4, "searchable": false} ]

    As for some reason it was not working with
    editor.field('updated_by').hide();

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    Yep, field().hide() is only for the Editor form, columns.visible is for the table itself. Glad all sorted,

    Colin

Sign In or Register to comment.