Dependant for editor - Separate script not populated select list

Dependant for editor - Separate script not populated select list

peterbrownepeterbrowne Posts: 314Questions: 54Answers: 0

I have two dependant selects on my Editor. One is working fine, the new one (assessments) is not working:

This is the separate script to get assessments when the learning_event.unit_group_fk select is selected:

<?php
require_once( '../../../maps/cm_mjd_plmed/connection/connect.php' );

$unit = $_POST['values']['learning_event.unit_group_fk'];

$data = array();

$query = "SELECT * FROM assessment WHERE unit_group_fk = '$unit'";
$result = $connection->query( $query );

while ($row = mysqli_fetch_array($result)) {
    $data[] = array("label"=>$row['assessment'], "value"=>$row['assessment_pk']);
}

$temp = array('assessment[].assessment_pk'=>$data);
$json = array('options'=>$temp);
echo json_encode($json);

<?php
>
```
?>



The client side:

    <div id="learning_event_form">
        <editor-field name="learning_event.learning_event_name"></editor-field>
        <editor-field name="learning_event.outline"></editor-field>
        <editor-field name="learning_event.length"></editor-field>
        <editor-field name="program_outcome[].program_outcome_pk"></editor-field>
        <editor-field name="learning_event.unit_group_fk"></editor-field>
        <editor-field name="learning_event.week_fk"></editor-field>
        <editor-field name="assessment[].assessment_pk"></editor-field>
        <editor-field name="learning_event_type[].learning_event_type_pk"></editor-field>
    </div>


var editor = new $.fn.dataTable.Editor( {
ajax: "program_data/learning_event_data.php",
table: "#learning_event_table",
template: '#learning_event_form',
fields: [ {
label: "Learning Event:",
name: "learning_event.learning_event_name"
}, {
label: "Outline:",
name: "learning_event.outline",
type: "ckeditor"
},{
label: "Length (minutes):",
name: "learning_event.length"
}, {
label: "Program Outcome:",
name: "program_outcome[].program_outcome_pk",
type: "select",
placeholder: 'No selection',
placeholderDisabled: false,
placeholderValue: 0,
multiple: true
}, {
label: "Unit Group:",
name: "learning_event.unit_group_fk",
type: "select",
placeholder: "Select Unit Group..."
}, {
label: "Week:",
name: "learning_event.week_fk",
type: "select",
placeholder: "Select Week..."
},{
label: "Assessment:",
name: "assessment[].assessment_pk",
type: "select",
placeholder: "Select Assessments...",
placeholderDisabled: false,
placeholderValue: 0,
multiple: true
}, {
label: "Type:",
name: "learning_event_type[].learning_event_type_pk",
type: "select",
placeholder: 'No selection',
placeholderDisabled: false,
placeholderValue: 0,
multiple: true,
attr: {
size: 1
}
} ]
} );

            editor.dependent('learning_event.unit_group_fk', 'program_data/get_weeks.php');

            editor.dependent('learning_event.unit_group_fk', 'program_data/get_assessments.php');



        var table = $( '#learning_event_table' ).DataTable( {
            responsive: true,
            "autoWidth": false,
            "lengthMenu": [
                [ 5, 10, 25, 50, -1 ],
                [ 5, 10, 25, 50, "All" ]
            ],
            columnDefs: [ {
                targets: 1,
                render: $.fn.dataTable.render.ellipsis( 150, true )
            } ],
            ajax: "program_data/learning_event_data.php",
            dom: "Blfrtip",
            columns: [ {
                data: "learning_event.learning_event_name",
            }, {
                data: "learning_event.outline",
            }, {
                data: "learning_event.length",
            }, {
                data: "program_outcome",
                render: "[, ].program_outcome"
            }, {
                data: "unit_group.unit_group"
            }, {
                data: "week.week_full_name"
            }, {
                data: "assessment.assessment",
                render: "[, ].assessment"
            }, {
                data: "learning_event_type",
                render: "[, ].learning_event_type_name"
            }, {
                data: "learning_event.modified"
            }, {
                data: "learning_event.modified_by"
            }, {
                data: null,
                className: "center",
                defaultContent: '<a href="" class="editor_edit">Edit</a>'
            } ],
            select: {
                style: 'os',
                selector: 'td:first-child'
            },
            buttons: []
        } );

```

This question has an accepted answers - jump to answer

Answers

  • peterbrownepeterbrowne Posts: 314Questions: 54Answers: 0

    Server side:

    Editor::inst( $db_cm_mjd_plmed, 'learning_event', 'learning_event_pk' )    
        ->field(
            Field::inst( 'learning_event.learning_event_name' ),
            Field::inst( 'learning_event.outline' ),
            Field::inst( 'learning_event.length' ),
            Field::inst( 'learning_event.modified' ),
            Field::inst( 'learning_event.modified_by' )->setValue( $user),
            Field::inst( 'learning_event.unit_group_fk' )
                ->options( Options::inst()
                    ->table( 'unit_group' )
                    ->value( 'unit_group_pk' )
                    ->label( 'unit_group' )
                    ->where( function ($q) {
                $q->where( 'unit_group.type', 'learning_event' );
                    })
                ),
            Field::inst( 'unit_group.unit_group' ), 
            Field::inst( 'learning_event.week_fk' )
                ->options( Options::inst()
                    ->table( 'week' )
                    ->value( 'week_pk' )
                    ->label( 'week_full_name' )
                ),
            Field::inst( 'week.week_full_name' )    
        )
         ->leftJoin( 'unit_group', 'unit_group.unit_group_pk', '=', 'learning_event.unit_group_fk' )
         ->leftJoin( 'week', 'week.week_pk', '=', 'learning_event.week_fk' )
            ->join(
            Mjoin::inst( 'program_outcome' )
                ->link( 'learning_event.learning_event_pk', 'learning_event_program_outcome_lookup.learning_event_fk' )
                ->link( 'program_outcome.program_outcome_pk', 'learning_event_program_outcome_lookup.program_outcome_fk' )
                ->order( 'program_outcome.program_outcome asc' )
                ->fields(
                    Field::inst( 'program_outcome_pk' )
                        ->options( Options::inst()
                            ->table( 'program_outcome' )
                            ->value( 'program_outcome_pk' )
            ->label( array('type', 'program_outcome') )
            ->render( function ( $row ) {
                return $row['type'] . ' - ' . $row['program_outcome'];
            } )
                            ->order( 'type' )
                        ),
                    Field::inst( 'program_outcome' )
                )
        )
        ->join(
            Mjoin::inst( 'assessment' )
                ->link( 'learning_event.learning_event_pk', 'learning_event_assessment_lookup.learning_event_fk' )
                ->link( 'assessment.assessment_pk', 'learning_event_assessment_lookup.assessment_fk' )
               ->order( 'assessment.assessment asc' )
                ->fields(
                    Field::inst( 'assessment_pk' )
                        ->options( Options::inst()
                            ->table( 'assessment' )
                            ->value( 'assessment_pk' )
                            ->label( 'assessment' )       
                        ),
                    Field::inst( 'assessment' )
                )
        )
        ->join(
            Mjoin::inst( 'learning_event_type' )
                ->link( 'learning_event.learning_event_pk', 'learning_event_type_lookup.learning_event_fk' )
                ->link( 'learning_event_type.learning_event_type_pk', 'learning_event_type_lookup.learning_event_type_fk' )
               ->order( 'learning_event_type.learning_event_type_name asc' )
                ->fields(
                    Field::inst( 'learning_event_type_pk' )
                        ->options( Options::inst()
                            ->table( 'learning_event_type' )
                            ->value( 'learning_event_type_pk' )
                            ->label( 'learning_event_type_name' )         
                        ),
                    Field::inst( 'learning_event_type_name' )
                )
        )
        ->process($_POST)
        ->json();
    
  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    Answer ✓

    In what way does it not work? Does it not make the second Ajax request, or is there an error or something else? If you could link to a test case showing the issue, that would be really useful.

    Thanks,
    Allan

  • peterbrownepeterbrowne Posts: 314Questions: 54Answers: 0

    The problem was with the script that populates the assessment select. I was sending data that needed to be used sub queries in that script to get the correct data. Fixed!

Sign In or Register to comment.