2016-04-28 95 views
1

我想顯示選擇2搜索ajax結果與組。但它不顯示任何結果。我爲此使用WordPress Ajax。選擇2組ajax結果

這裏是我的JS代碼,

jQuery('select.select2-group_filters-dropdown').select2({ 
      //placeholder: "Select pages/post/categories", 

      ajax: { 
       url: ajaxurl, 
       dataType: 'json', 
       method: 'post', 
       delay: 250, 
       data: function (params) { 
        return { 
         q: params.term, // search term 
         page: params.page, 
         action: 'cp_get_posts_by_query' 
        }; 
       }, 
       results: function (data, page) { 
         return {results: data}; 
       }, 
       processResults: function (data) { 

        return { 
         results: data 
        }; 
       }, 
       cache: true 
      }, 
      minimumInputLength: 0, 

     }); 

數據我從PHP返回的,

$searchString = $_POST['q']; 
$childdata = array(); 

$query = new WP_Query(array('s' => $searchString)); 

if ($query->have_posts()) { 
    while ($query->have_posts()) { 
     $query->the_post(); 
     $title = get_the_title(); 
     $ID = get_the_id(); 
     $childdata[] = array('id' => "post-".$ID, 'text' => $title); 
    } 
} else { 
    $data[] = array('id' => '0', 'text' => 'No results Found'); 
} 

$data = array(
    "text" => "posts", 
    "children" => $childdata 
); 

wp_reset_postdata(); 

// return the result in json 
echo json_encode($data); 
die(); 

這無法按預期運行。它返回零結果。請幫我解決一下這個。

+0

嘗試的print_r(json_encode($數據));而不是回聲 –

+0

也試過了。但不工作。我的JSON結果是'{「text」:「posts」,「children」:{「id」:「post-39」,「text」:「Nihil voluptatem provident reprehenderit et voluptatem rerum」 :「post-2」,「text」:「Sample Page」},{「id」:「post-99」,「text」:「Rerum quasi odio sed」},{「id」 ,「text」:「Nesciunt iste doloribus exercitationem eligendi」},{「id」:「post-104」,「text」:「Praesentium et dolorem excepturi voluptatibus reiciendis」},{「id」:「post-81」, 「text」:「Error corporis aut commodi」},{「id」:「post-63」,「text」:「et sequi enim delectus」}]}' – patilvikasj

+0

您是否嘗試過console.log收到的數據?它在控制檯上給你0還是什麼都沒有? –

回答

0

如果您從後端獲取數據,則問題出在select2配置中。 嘗試先調用ajax調用,然後用數據填充select2。 Somethong這樣的(不知道這會爲你工作,我不能在這裏進行測試):

jQuery.ajax({ 
url: ajaxurl, 
dataType: 'json', 
method: 'post', 
delay: 250, 
data: function (params) { 
    return { 
     q: params.term, // search term 
     page: params.page, 
     action: 'cp_get_posts_by_query' 
    } 
    } 
    }).done(function(data) { 

jQuery('select.select2-group_filters-dropdown').select2({ data:data, minimumInputLength: 0}); 


    }); 
-1
$('.select2').select2({ 
       allowClear: true, 
       ajax: { 
        url: function (params) { 
         return "api/endpoint/?user=" + params.term; 
        }, 
        dataType: 'json', 
        delay: 500, 
        processResults: function (data) { 
         return { 
          results: $.map(data, function (item) { 
           return { 
    /* NOTE: return in this format i.e. 
    * key = **text** : value = username 
    * key = **id** : value = id 
    */ 
            text: item.username, 
            id: item.id 
           } 
          }) 
         }; 
        }, 
        minimumInputLength: 1, 
        minimumInputLength: 3, 
        cache: true, 
        escapeMarkup: function (markup) { 
         return markup; 
        }, 
        templateResult: function (item) { 
         return item.username; 
        }, 
        templateSelection: function (item) { 
         return item.username; 
        }, 
       } 
      });