Combining two fields to insert/update column - preSubmit?

Combining two fields to insert/update column - preSubmit?

peterbrownepeterbrowne Posts: 314Questions: 54Answers: 0

I want to combine two fields to insert/update:

program_outcome.program_outcome + program_outcome.type => program_outcome.program_outcome_full

I want this to be done invisibly based on the values of program_outcome and type in the editor. I'm assuming that I will need to use preSubmit, but not sure if this is correct or how to use it in this context.

Server side:

Editor::inst( $db_cm_dopt, 'program_outcome', 'program_outcome_pk' )->fields(
    Field::inst( 'program_outcome.program_outcome' ),
    Field::inst( 'program_outcome.type' ),
    Field::inst( 'program_outcome.modified' ),
    Field::inst( 'program_outcome.modified_by' )->setValue( $user ),
    Field::inst( 'program_outcome.program_outcome_group_fk' )
            ->options( Options::inst()
                ->table( 'program_outcome_group' )
                ->value( 'program_outcome_group_pk' )
                ->label( 'program_outcome_group' )
            ),
        Field::inst( 'program_outcome_group.program_outcome_group' )
    )
     ->leftJoin( 'program_outcome_group', 'program_outcome_group.program_outcome_group_pk', '=', 'program_outcome.program_outcome_group_fk' )
->process( $_POST )
->json();

Client side:

var editor = new $.fn.dataTable.Editor( {
                ajax: "program_data/program_outcome_data.php",
                table: "#program_outcome_table",
                template: '#program_outcome_form',
                fields: [ {
                    label: "Program Outcome:",
                    name: "program_outcome.program_outcome"
                }, {
                    label: "Type:",
                    type:  "select",
                    placeholder: 'Select a Type...',
                    name:  "program_outcome.type",
                    options: [
                        "OCANZ",
                        "UWA"
                    ]
                }, {
                    label: "Program Outcome Group:",
                    name: "program_outcome.program_outcome_group_fk",
                    type: "select",
                    placeholder: 'No selection',
                    placeholderDisabled: false,
                    placeholderValue: 0,
                    multiple: false
                } ]
            } );
            
            
            editor.on( 'preSubmit', function ( e, d, action ) {
                if ( action === 'create' || action === 'edit' ) {
                    $.each( d.data, function ( key, row ) {
                        row.program_outcome_full = row.type +'-'+ row.program_outcome;
                    } );
                }
            }


            var table = $( '#program_outcome_table' ).DataTable( {
                responsive: true,
                columnDefs: [ {
                    targets: 0,
                    render: $.fn.dataTable.render.ellipsis( 100, true )
                } ],
                ajax: "program_data/program_outcome_data.php",
                dom: "Blfrtip",
                columns: [ {
                    data: "program_outcome.program_outcome"
                }, {
                    data: "program_outcome.type"
                }, {
                    data: "program_outcome_group.program_outcome_group"
                }, {
                    data: "program_outcome.modified"
                }, {
                    data: "program_outcome.modified_by"
                } ],
                select: {
                    style: 'os',
                    selector: 'td:first-child'
                },
                buttons: []
            } );

This question has an accepted answers - jump to answer

Answers

  • peterbrownepeterbrowne Posts: 314Questions: 54Answers: 0

    An example using the above would be great and save me from asking follow-up questions!

  • allanallan Posts: 61,645Questions: 1Answers: 10,093 Site admin
    edited July 2021

    The Field->setValue() method on the server-side with a server-side event is how I would approach this*.

    For example (for preEdit - preCreate would be similar):

    ->on('preEdit', function ($editor, $id, $values) {
      $editor->field('program_outcome.program_outcome_full')->setValue(
        $values['program_outcome']['program_outcome'] .
        $values['program_outcome']['type']
      );
    })
    
    • Actually, if I had the option, I wouldn't store this field in the database at all, since the it is derived from two other values that we have stored in the database already. I'd use a client-side renderer to combine the fields on-the-fly. Makes mass updates easier and reduces storage space and data to transfer.

    Regards,
    Allan

  • peterbrownepeterbrowne Posts: 314Questions: 54Answers: 0
    edited July 2021

    Okay, but I'm asking this question based on your answer from yesterday, re not having a left join in Mjoin to concatenate two columns in the Mjoin.

    The solution just requires showing in the options in the select list for program_outcome in that previous question, as concatenated fields from the two columns program_outcome.type and program_outcome.program_outcome .

  • allanallan Posts: 61,645Questions: 1Answers: 10,093 Site admin
    Answer ✓

    Oh I see! I misunderstood the question yesterday in that case. If this is for the list of options, then you can use a renderer for that. The third example there shows how that can be done.

    Allan

  • peterbrownepeterbrowne Posts: 314Questions: 54Answers: 0
    edited July 2021

    Thanks Alan, that did the trick easily!

Sign In or Register to comment.