set default value in select, based on previously submitted value

set default value in select, based on previously submitted value

crush123crush123 Posts: 417Questions: 126Answers: 18

I have an editor page for which i add a user reference from a select list.

What i would like to do is set the default of the select to the last user reference id selected, as often the data is input in batches, so it would be one less thing to fill in.

So far I have tried setting a session value on create, and use this session value as the default, but it doesn't seem to work unless the page is reloaded.

json source

if ( Editor::action( $_POST ) === Editor::ACTION_CREATE ) {
                $_SESSION['sessionpatron'] = $_POST['data']['tblitem']['ItemPatronID'];
....

php page

 fields: [ {
            label: "Patron Name:",
            name: "tblitem.ItemPatronID",
            def: <?php echo (isset($_SESSION['sessionpatron'])?$_SESSION['sessionpatron']:1);?>,
            type:  "select"
        },

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin
    Answer ✓

    The best way I can think of doing this is to use the dependent() method to know when the field's value changes, and the field().def() method to set the default when it does change. The one missing piece of that puzzle then is to have this operation on create only - there currently isn't an API method which will tell us which mode Editor is in (I'll add one in the next release) but we can use events to get that information and set a local variable.

    Here is an example:

        var mode;
    
        editor
            .on( 'initCreate', function () {
                mode = 'create';
            } )
            .on( 'initEdit', function () {
                mode = 'edit';
            } )
            .on( 'initEdit', function () {
                mode = 'remove';
            } )
            .on( 'close', function () {
                mode = 'closed';
            } )
            .dependent('first_name', function (val) {
                if ( mode === 'create' ) {
                    editor.field( 'first_name' ).def( val );
                }
            } );
    

    That will run on this page for the first field in the form if you want to see it in action.

    Allan

  • crush123crush123 Posts: 417Questions: 126Answers: 18

    Oh yeah !

    I never would have worked that out, so I copied it and pasted it, changed the editor field name, and it works !!

    Respect

This discussion has been closed.