2016-09-19 5 views
0

내 Symfony 프로젝트에서 select2를 사용하려고하지만 여전히 결과를 표시하는 방법을 모르겠습니다. 프론트 엔드에 대한 나의 코드는select2 데이터를 표시하는 방법

$(".js_search").select2({ 
       ajax: { 
        url: "{{ path('search') }}", 
        dataType: 'json', 
        delay: 250, 
        data: function (params) { 
         return { 
          q: params.term, // search term 
          page: params.page 
         }; 
        }, 
        processResults: function (data, params) { 
         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; 
       }, // let our custom formatter work 
       minimumInputLength: 1, 
       templateResult: formatRepo, // omitted for brevity, see the source of this page 
       templateSelection: formatRepoSelection // omitted for brevity, see the source of this page 
      }); 
     }); 
     function formatRepo (repo) { 
      if (repo.loading) return repo.text; 

      var markup = '<div class="clearfix">' + 
        '<div class="col-sm-1">' + 
        '<img src="' + repo.owner.avatar_url + '" style="max-width: 100%" />' + 
        '</div>' + 
        '<div clas="col-sm-10">' + 
        '<div class="clearfix">' + 
        '<div class="col-sm-6">' + repo.full_name + '</div>' + 
        '<div class="col-sm-3"><i class="fa fa-code-fork"></i> ' + repo.forks_count + '</div>' + 
        '<div class="col-sm-2"><i class="fa fa-star"></i> ' + repo.stargazers_count + '</div>' + 
        '</div>'; 

      if (repo.description) { 
       markup += '<div>' + repo.description + '</div>'; 
      } 
      markup += '</div></div>'; 
      return markup; 
     } 

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

그램 내가 응답을 $ 방법을 잘 모릅니다 백엔드

public function searchAction() 
{ 
    $response = new Response(); 
    $response->setContent(json_encode(
     [ 
      'items' => 
       [ 
        'id' => 1, 
        'text' => 'test' 
       ] 
     ] 
    )); 
    $response->headers->set('Content-Type', 'application/json'); 
    return $response; 
} 

위해 같은 방법이나 뷰에서 반환 된 데이터가 보여 보인다. 누군가 대답을 알고 있습니까?

나는 선택 2 드롭 다운에 대한 옵션 태그를 생성하기 위해 사용하는 요소와 JSON 배열해야이 https://select2.github.io/

+0

당신은'JsonResponse' 클래스를 시도 할 수 있습니다. 아마도 ti가 문제를 해결할 것입니다. http://symfony.com/doc/current/components/http_foundation.html#creating-a-json-response 이렇게하면 문제가 "마술처럼"수정되지 않으므로 개발자 콘솔에 더 많은 돈을 투자하십시오. 귀하의 브라우저를 선택하십시오. 자주 F12를 누를 때 열립니다 – Joshua

+0

어떤 오류가 있습니까? – Matteo

답변

0

귀하의 응답을 사용하고 있습니다. 귀하의 경우에는

당신의 AJAX 요청에 reposnse의 내용은 다음과 같습니다

[{"id": 1, "text":"test"}] 

선택 2는 다음 JQuery와 선택기 $에 의해 식별 요소를 업데이트합니다. (JS-검색) 옵션과 값과 당신의 응답.

+0

[{ "id": 1, "text": "테스트"}] 을 사용하고 jsonResponse를 사용했지만 모두 f.e 한 글자를 쓸 때 다시 결과를 찾지 못했습니다. – Tomek

관련 문제