Select2 - Selected value is empty on dropdown open

Select2 - Selected value is empty on dropdown open

cent89cent89 Posts: 19Questions: 6Answers: 0

Hi, I have ad editor with this field:

{label: "Lunghezza", name: 'lunghezza', data: "lunghezza", type:'select2', options: [], opts: {"tags": true}},

The options are populated on init:

editor_detail.on( 'initEdit', function ( e, node, data, items, type ) {
    getListaLunghezze(data['id_particolare']);
  });


function getListaLunghezze(id_particolare){
  var lista_lunghezze = [];
  $.ajax({
    type: 'GET',
    dataType: 'json',
    url: "{{ route('ordine_mp.lista_lunghezze') }}",
    data: {
      id_particolare: id_particolare
    },
    success: function(response) {
      $.each(response, function(index, element) {
        lista_lunghezze.push(element);
        option = {};
      });

      editor_detail.field('lunghezza').update( lista_lunghezze );
    },
    error: function(XMLHttpRequest, textStatus, exception) { alert("Ajax failure\n" + XMLHttpRequest.responseText + "\n" + exception); },
    async: false
  });
}

The code works, the Select2 are rightly populated, but when I click to the cell with the select, the option selected is always blank, empty. So I have to reselect the option in the dropdown to save the value.

What's wrong?
Thanks!

Answers

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    It sounds like the value for the ell doesn't match the values for the options in Select2. Can you give me a link to a page showing the issue so I can check into it please?

    Allan

  • cent89cent89 Posts: 19Questions: 6Answers: 0

    I haven't an online version, now.

    Datatable:

    ...
    { data: 'particolare_label', editField: 'id_particolare' },
    ...
    

    The ajax return the 'particolare_label', but in mysql I have the column called 'id_particolare'. This works with standard select.

    Editor (I have made some changes from first post):

    {label: "Particolare", name: "id_particolare", type:"select2", opts:{
            ajax: {
              method: 'POST',
              url: "{{ route('particolare.lista_as_result') }}",
              dataType: 'json'
            }
          }},
    

    When I click the first time to the cell, the select2 appears after 5 seconds (the page is like freezed), 'Particolare' table has 16k rows....

    So, I'm expecting that select2 has only one row (the selected value), not the entire table.
    However, the entire dataset is displayed and the value is not selected (if I click outside the editor, null value is set).

    If I use Select2 outside the editor, like this, it works as expected and the time to open is instant.

    $('#select_particolare').select2({
        ajax: {
          method: 'POST',
          url: "{{ route('particolare.lista_as_result') }}",
          dataType: 'json'
        }
      });
    

    Can you help me?
    Thanks!

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    Is your Ajax script considering the initialValue parameter that is sent? It needs to respond to the initial Ajax request so it can be rendered correctly.

    Allan

  • cent89cent89 Posts: 19Questions: 6Answers: 0

    Thanks for the suggestion. I've added the initialValue and value params but it still doesn't works.
    When I click to the text label, the Select2 appears (not the dropdown), with the option selected (the only option in the select, I have filtered the list with the initialValue param).
    When I re-click to the Select2, the dropdown appears but with the first index selected.
    If I close the select2 dropdown and I click to reopen, now the option is correctly selected.
    What's wrong?

    Thanks!

  • cent89cent89 Posts: 19Questions: 6Answers: 0

    Hi,
    I have build a demo site (I'll send you a pm with the link and login credentials).
    The trouble with Select2 are the following:
    - When I click on a Select2 field, the initial value isn't selected, with or without the initialValue field. If I use the value field, how I can pass in ajax request the current value?
    - On initEdit event, I reload the options (array, not object) of three field, but when I select an option of a field, the other two fields are resetted to null or empty value.

    I think that is the same problem, but after a lot of attempts and hours, I can't resolve the mistakes...
    Can you help me?

    Thanks!

  • cent89cent89 Posts: 19Questions: 6Answers: 0

    Someone how can help me?
    I can't find a solution ...

    Thanks!

  • cent89cent89 Posts: 19Questions: 6Answers: 0

    Perhaps I found the problem...
    On open, the option is selected, but the dropdown doesn't scroll to the option.....

    How to solve?

This discussion has been closed.