Search code examples
jqgridjquery-select2jquery-select2-4

select2 does not autoselect when using optgroup


I am using select2 with jqgrid. something is not working correctly, these are the cases when I edit a row.

WITHOUT OPTGROUP

  1. standard select (no select2) without optgroup: select will show the selected item (the one i’m editing)
  2. select2 without optgroup: select will show the selected item (the one i’m editing)

WITH OPTGROUP

  1. standard select (no select2) with optgroup: select will show the selected item (the one i’m editing)
  2. select2 with optgroup: select will NOT show the selected item (the one i’m editing) but when I open it il will focus the selected item.

So as you see #2 works, my problem is #4. Is it a bug or am I missing something?

Thank you, Lorenzo


Solution

  • Not sure what you search, but the problem when navigating row is described here

    In your case I have tested and the following code work:

    $('#grid'). jqGrid('navGrid', pager, {params},
    ...
      {  // edit params
         { afterclickPgButtons : function(button, form, id ) { 
            $("#name").trigger('change.select2');
         },...
      }
    );
    

    Note the difference of the link above.

    UPDATED

    In order to solve the problem you will need to use setTimeout within dataInit function. Try with one of the following settings:

    {
          name: "name",
          index: "name",
          autosize: true,
          searchoptions:{clearSearch:false},
          width:200,
          editable:true,          
          edittype: 'select', 
          editoptions: {         
            // Simulo la risposta di una chiamata
            // Per funzionare deve esistere più sotto 
            // ajaxSelectOptions
            dataUrl:'/echo/html/',
            postData: {
                          html: serverResponce
                      }, 
            //            
            dataInit: function(element) { 
            setTimeout(function(){
                  $(element).select2({
                    allowClear: false,
                    // Imposto il tema bootstrap
                    theme: "bootstrap4",
                    dropdownAutoWidth : true,
                            width: 'auto',                  
                    placeholder: 'Search...'
                  });
              }, 500);
                }
          }
       }
    

    I expect to have a demo with optgroup