How to modify row data to multi row data using preSubmit

How to modify row data to multi row data using preSubmit

Anthonia JeromeAnthonia Jerome Posts: 12Questions: 6Answers: 0

Link to test case: http://4173fdd5f002.ngrok.io/banana/public/master navigate to Parties->Person
Debugger code (debug.datatables.net): aqejox
Error messages shown:No any error but the data sent to the server is empty, from the 'network' ie. data = { }
Description of problem:
Hi,
How can I modify the data before submitting to the server, from single row data to multi-row data?

My goal here is:
I have an editor fields for first, middle and last name, so when the form is submitted they are taken as one row with three columns to be posted in the database table, but I want them to be posted in the database table within three rows in one column and then the adjacent column of the table should contain the numbers 1, 2 and 3 to represent the first middle and last name. How can I achieve this please?

Note: I use different ajax url for sending data to the server and for retrieving data from the server i.e Master\person for sending and Person\Info for retrieving.

Here is my code:

$('ul .pcoded-submenu li').on('click', function(){    
    $('ul .pcoded-submenu li').removeClass('active');
    $('.card').addClass('d-none');
    $('.card').eq($(this).index()).removeClass('d-none');
    $(this).addClass('active');
})

var editor;

$(document).ready(function() {
    editor = new $.fn.dataTable.Editor( {
        ajax: "Master/Person",
        table: '.table:eq(0)',
        fields: [ 
        {
            label: "Person ID:",
            name: "ID",
            type: 'select'
        },
        {
            label: "First Name:",
            name: "NAME1"
        },
        {
            label: "Second Name:",
            name: "NAME2"
        },
        {
            label: "Third Name:",
            name: "NAME3"
        }]
    } );
tableColums = [
                {data: 'ID'},
                {data: null, render: function ( data, type, row ) {
                // Combine the first and last names into a single table field
                return data.FIRST_NAME+' '+data.MIDDLE_NAME+' '+data.LAST_NAME;}
                }
            ];
tableButtons('.table:eq(0)', 'Person/Info', tableColums);
    editor.on('preSubmit', function(e, data, action) {
        data.data = [
                {0: {'ID': data.data[0].ID,
                        'NAME': data.data[0].NAME1,
                        'PERSON_NAME_TYPE_ID': 1,
                        'NAME_SEQ_ID': 1
                    }
                },
                {1: {'ID': data.data[0].ID,
                        'NAME': data.data[0].NAME2,
                        'PERSON_NAME_TYPE_ID': 2,
                        'NAME_SEQ_ID': 2
                    }
                },
                {2: {'ID': data.data[0].ID,
                        'NAME': data.data[0].NAME3,
                        'PERSON_NAME_TYPE_ID': 3,
                        'NAME_SEQ_ID': 3
                    }
                }                
            ];
    });
} );
function tableButtons(activeTable, tableAjax, tableColums){
    var table = $(activeTable).DataTable( {
        lengthChange: false,
        ajax: tableAjax,
        columns: tableColums,
        select: true
    } ); 
    // Display the buttons
    new $.fn.dataTable.Buttons( table, [
        { extend: "create", editor: editor}, 
        { extend: "edit", editor: editor },
        { extend: "remove", editor: editor }
        ] );
    table.buttons().container().appendTo( $('.col-md-6:eq(0)', table.table().container() ) );
    editor.on( 'submitSuccess', function ( e, json, data ) {
        table.ajax.reload();    
    } );
}

Answers

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

    Doesn't my reply in your other thread resolve this?

    Colin

  • Anthonia JeromeAnthonia Jerome Posts: 12Questions: 6Answers: 0

    It helped to some extent collin, but the problem was: Multi-row doesnt allow to put different data in the same field unless pre defined, when i try to put a data in the input field then all the field were filed by the same data, so I decided to take the row data as they are and convert them to mult-row data before submitting, does this work?

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

    Multi-row doesnt allow to put different data in the same field unless pre defined,

    The example I posted has this:

              editor.field( 'position' )
                .multiSet( 0, 'first' )
                .multiSet( 1, 'second' )
                .multiSet( 2, 'third' );
            }
    

    first, second and third are pre-defined there, but they can be anything, created dynamically.

    What you're doing is shoe-horning in those three records, when they're not expected to be there, so that's a bad approach to go.

    If multiSet isn't for you, you can create additional records in submitComplete - see example here from this thread - additional records are created after the completion of the first.

    Colin

This discussion has been closed.