editor: select2 and focus

editor: select2 and focus

olemoolemo Posts: 3Questions: 0Answers: 0

Hello,

I'd like to activate focus for a select2 editor field by opening it for creation of a new entry (but not when editing the entry) - so the onFocus option, which is set globally, is not suitable.
I tried this - without success:

        editor.on( 'initCreate', function (e) {
             editor.field( 'job_id' ).focus();              
        });

Any further ideas?

Thank you
Ole

Replies

  • rf1234rf1234 Posts: 2,801Questions: 85Answers: 406
    edited July 2020

    The syntax you are using looks correct. You would need to post a test case.

    You can try this - but it should be an equivalent of "initCreate":

    editor.on( 'open', function ( e, mode, action ) {
        if ( action === 'create' ) {
            editor.field( 'job_id' ).focus();
        }      
    });
    

    and check the console of your browser please.

    This event triggers the focus even later: after the form has been opened:

    editor.on( 'opened', function ( e, mode, action ) {
        if ( action === 'create' ) {
            editor.field( 'job_id' ).focus();
        }      
    });
    
  • olemoolemo Posts: 3Questions: 0Answers: 0

    Thank you, @rf1234 - but this still does not work:
    http://live.datatables.net/daqipade/1/edit
    but it works with the "onFocus: 'focus'" option (but this will trigger the focus not just for the create action)

  • olemoolemo Posts: 3Questions: 0Answers: 0

    this does the trick:

            editor.on( 'opened', function ( e, mode, action ) {
                        if (action === 'create') {              
                            $('#DTE_Field_job_id').select2('open');
                        }
              });
    

    thank you, @rf1234 for setting me on the right path

  • rf1234rf1234 Posts: 2,801Questions: 85Answers: 406

    Glad you got it working. Select2 seems to behave differently than the Select or Selectize field types. With Selectize the field opens on focus - even if you don't want it to :). Had to set focus to null to make sure the dropdown didn't open on opening the form.

This discussion has been closed.