2017-04-02 33 views
0

我想用選擇二來篩選我的「供應商」這樣的:Laravel選擇二框在我Laravel 5.4的應用程序沒有過濾

https://paste.laravel.io/RowKK

的選擇二框顯示正確的內容,但是當我開始輸入它不會過濾正確的項目的提供程序的名稱。

在示例頁面上使用相同的功能時,此功能正在工作。

任何想法我做錯了什麼?

問候

kay899

+0

你應該通過你的任期過濾。閱讀更多關於select2 https://select2.github.io/options.html#results – Roljhon

+0

你究竟是什麼意思? – kay899

回答

0

它仍然是你的API後端,將做過濾,所有你需要做的就是通過在你的Ajax data屬性通過查詢詞。示例params.term已傳遞給q屬性,並從API後端拖出。

例GitHub的庫拉:

function formatRepo (repo) { 
 
    if (repo.loading) return repo.text; 
 
    return repo.full_name 
 
} 
 

 
function formatRepoSelection (repo) { 
 
    return repo.full_name || repo.text; 
 
} 
 

 
$(".js-data-example-ajax").select2({ 
 
    placeholder: 'Select an item', 
 
    ajax: { 
 
    url: "https://api.github.com/search/repositories", 
 
    dataType: 'json', 
 
    delay: 250, 
 
    data: function (params) { 
 
      return { 
 
      q: params.term, // search term 
 
      page: params.page 
 
      }; 
 
     }, 
 
    processResults: function (data, params) { 
 
      // notice we return the value of more so Select2 knows if more results can be loaded 
 
      //console.log(data.items) 
 
      params.page = params.page || 1; 
 
      return { 
 
      results: data.items, 
 
      pagination: { 
 
       more: (params.page * 30) < data.total_count 
 
      } 
 
      }; 
 
     }, 
 
    cache: true 
 
    }, 
 
    escapeMarkup: function (markup) { return markup; }, 
 
    minimumInputLength: 1, 
 
    templateResult: formatRepo, 
 
    templateSelection: formatRepoSelection 
 

 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.1/js/select2.min.js"></script> 
 
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.1/css/select2.min.css" rel="stylesheet"/> 
 

 
<select class="js-data-example-ajax" style="width:500px;"> 
 
    <option selected="selected"></option> 
 
</select>