Editor Select2 Wrong Value on Edit

Editor Select2 Wrong Value on Edit

vincmeistervincmeister Posts: 136Questions: 36Answers: 4

Link: https://rabbit.solusiprogram.top/
Debugger code (debug.datatables.net): ebayog
Error messages shown: No Error Message
Description of problem:
I have 2 related field, Dept & Position.
When user select Dept, then the options for Position are based on Dept value. This is not a problem.
The problem is after data submit, when user edit the data:
- on first modal open: Position value is missing
- on next modal open: Position value is wrong, it change to 1st option.

var editor = new $.fn.dataTable.Editor( {
                ajax: {
                    url: "model/datatables_demo.php",
                    type: 'POST',
                    data: function (d){}
                },
                table: "#tblexample",
                fields: [ 
                    {
                        label: "First Name",
                        name: "datatables_demo.first_name"
                    },  {
                        label: "Department",
                        name: "datatables_demo.id_dept",
                        type: "select2"
                    },  {
                        label: "Position",
                        name: "datatables_demo.id_pos",
                        type: "select2"
                    }
                ]
            } );

            editor.dependent( 'datatables_demo.id_dept', function ( val, data, callback ) {
                id_dept = val;
                $.ajax( {
                    url: "model/opt_id_pos.php",
                    dataType: 'json',
                    type: 'POST',
                    data: {
                        id_dept: id_dept
                    },
                    success: function ( json ) {
                        editor.field('datatables_demo.id_pos').update(json);
                    }
                } );

                return {}
            }, {event: 'keyup change'});

opt_id_pos.php

$id_dept = $_POST['id_dept'];

    $qs_position = $db
        ->query('select', 'position')
        ->get([
            'id as value',
            'name as label'
        ])
        ->where('id_dept', $id_dept )
        ->exec();
    
    $rss_position = $qs_position->fetchAll();
    echo json_encode($rss_position);

Need advise please,

Regard,
Danny

Sign In or Register to comment.