1. 程式人生 > 實用技巧 >記一次使用select2 完成ajax非同步搜尋渲染結果

記一次使用select2 完成ajax非同步搜尋渲染結果

    $('#mySelect2').select2({
        placeholder: "請選擇",
        minimumInputLength: 1,
        maximumInputLength: 50,
        ajax: {
            url: "http://localhost:8080/search",
            type: 'get',
            delay: 250,
            data: function (params) {
                console.log('search keyword:' + params)
                
return { 'keyword': params // 請求url: http://localhost:8080/search?keyword= }; }, results: function (data) { // 結果格式要遵循 [{id: '', text: ''}] console.log('search result:' + JSON.stringify(data)) return {'results': JSON.parse(JSON.stringify(data))} }, },
// initSelection: function (element, callback) { // 初始選中 // callback({ // id: $("#keyword").val(), // text: $("#keywordText").val() // }); // }, // formatSelection : function formatRepoSelection(repo) { // 選中項 // return repo.text; //
}, formatResult: function format(item) { return item.text; // 選中時,顯示的值 }, dropdownCssClass: "bigdrop" //樣式 });