How do I adapt select fields dependant on the value of another field in Editor?

How do I adapt select fields dependant on the value of another field in Editor?

rheinertprheinertp Posts: 23Questions: 4Answers: 0

Hi,
I have two tables: job and step.
In step there is a reference to jobid (shown as select , which works great)
In step you can define a "follow-up" step. When clicking on that field (I chose "online editing")
there should be a select field - but only with those "steps" (stepnames) that have the same jobid as
the one that you just click on.

I tried it that way - but the "where" for followup_stepid does not work (it work if I i.e. define a fixed number - i.e. 1)

    Editor::inst( $this->editorDb, 'tbladib_step', 'stepid' ) ->debug(true)
     ->field(
Field::inst( 'tbladib_step.jobid' )->setFormatter( 'Format::ifEmpty', null )
        ->options( Options::inst()
            ->table( 'tbladib_job' )
            ->value( 'jobid' )
            ->label( 'jobname' )
        ),
Field::inst( 'tbladib_job.jobname' ),        // wichtig: Im Fall von foreignKeys immer BEIDE Werte nehmen!            

Field::inst( 'tbladib_step.followup_stepid' )->setFormatter( 'Format::ifEmpty', null )
        ->options( Options::inst()
            ->table( 'tbladib_step' )
            ->value( 'stepid' )
            ->label( 'stepname' )                
            ->where( function ($q) {
                    $q->where( 'tbladib_step.jobid', $post['data']['tbladib_step']['jobid'] );
            })
        ),
Field::inst( 'refstep.stepname' )        // wichtig: Im Fall von foreignKeys immer BEIDE Werte nehmen!            

)
->leftJoin( 'tbladib_job', 'tbladib_job.jobid', '=', 'tbladib_step.jobid' )
->leftJoin( 'tbladib_step as refstep', 'refstep.stepid', '=', 'tbladib_step.followup_stepid' )
->process($post)
->json();
}

How can I solve that??

Thanks a lot,
Pascal

Answers

  • allanallan Posts: 63,479Questions: 1Answers: 10,467 Site admin

    Hi Pascal,

    You need to use a cascading select as described here.

    The issue here is that each row could have different options for the second select list, so the list of options must be available on a per row basis. Either you could modify the data to include that for each row, or use an Ajax request like in the blog post.

    Allan

  • tangerinetangerine Posts: 3,365Questions: 39Answers: 395

    Hi Allan.
    On the page you linked to, "Contient" should be "Continent".

    I just can't help myself....

  • rheinertprheinertp Posts: 23Questions: 4Answers: 0

    @allan: Thank you so much - it worked...

        public function getStepsOfJob($post)
        {
                include_once( dirname(__FILE__).'/../libraries/Editor/lib/DataTables.php' );
                $steps = $this->editorDb
                    ->select( 'tbladib_step', ['stepid as value', 'stepname as label'], ['jobid' =>$jobid] )
                    ->fetchAll();
    
                echo json_encode( [
                    'options' => [
                        'tbladib_step.followup_stepid' => $steps
                    ]
                ] );                    
        }
    

    BUT:
    What is the best way to avoid that the function is only called once when/if the user clicks on that selection field (currently it is called all the time).
    I addded an event - but "mouseup" does not seem to be the best...

    `editor.dependent( 'tbladib_step.followup_stepid', 'step_jobs', {  event: 'mouseup' });     `   
    

    Thank you!
    PAscal

This discussion has been closed.