Select2 在加载数据后立即关闭

Select2 closes immediately after data loaded

我正在尝试使用 ajax post 单击方法将数据加载到 select2。但在数据加载后,选择框关闭。我必须再次打开它才能看到数据。我需要它保持打开状态。这是我的代码。

$('#newLawyer').select2();
$('#newLawyer').one('select2:opening', function() {
    $.ajax({
        method: 'POST',
        url: API_URL + 'getMemberList',
        async: true
    }).done(function (data) {
        console.log(data)
        result = $.map(data.data, function (obj) {
            return {
                text: obj.name,
                id: obj.id
            };
        });
        $("#newLawyer").select2({
            placeholder: "Bir seçim yapınız",
            data: result,
            cache: true
        });
    })
})

试试这个

$('#newLawyer').select2({
  minimumInputLength : 0,
  ajax: {
    url: API_URL + 'getMemberList',
    method: 'POST',
    async: true,
    dataType: 'json'
    processResults: function (data) {
      // Transforms the top-level key of the response object from 'items' to 'results'
      const results = [];
      data.data.map(obj=> {
        if( /* any search condition put here */ ) {
          results.push({text: obj.name, id: obj.id})
        } 
      });

      return { results };
    }
  }
});