2012-08-30 4 views
1

은 제가자동 완성 : 결과를 포맷

내가 데이터 소스의 각 제목을 포함하도록 JQuery와 자동 완성을 포맷하기 위해 노력하고있어 및을 강조하기 위해 시도 것을 가진 코드로 다시 물어 봅시다 기간.

JS :

$(".auto-search").autocomplete({ 
     source: '/json/autocomplete_search', 
     minLength: 2, 

    }); 


PHP (CodeIgniter를)

를 나는 내가 그것을 다시 보내기 전에 아마 가장 쉬운 포맷하는 것입니다 CodeIgniter의를 사용하고 생각하고 있습니다
public function autocomplete_search() 
{ 
    $term = $this->input->get('term'); 

    //load model and get results 
    $this->load->model("mymodel"); 
    $results1= $this->mymodel->search_one($term); 
    $results2= $this->mymodel->search_two($term); 

    //Start JSON string 
    $json='['; 

    //first source 
    if ($result1->num_rows() > 0) 
    { 
     $json .= '{"value":"<h3>Heading One<h3>"}'; 
     foreach ($results1->result_array() as $r1) 
     { 
      $result = str_replace($term,"<strong>".$term."</strong>",$r1['title']); 
      $json .= ',{"value":"'.$result.'"}'; 
     } 
    } 

    //second source 
    if ($result2->num_rows() > 0) 
    { 
     if ($result1->num_rows() > 0){$json .= ',';} 
     $json .= '{"value":"<h3>Heading Two<h3>"}'; 

     foreach ($results2->result_array() as $r2) 
     { 
      $result = str_replace($term,"<strong>".$term."</strong>",$r2['location']); 
      $json .= ',{"value":"'.$result.'"}'; 
     } 
    } 

    //Close JSON string 
    $json .= ']'; 
    echo $json; 
}` 

불행히도 형식화 된 출력을 얻지 못하고 있습니다. 실제로 < h1> 및 < strong>이라는 단어를 출력에 추가합니다. 다음 샘플 출력은 다음과 같습니다

enter image description here

+0

그것은 JSON을이다 : 또한 결과를 다시 포맷하려면 다음 이벤트를 추가했다? 당신은 그것을 분석하고 어떤 시점에서 값을 출력하려고 했습니까? –

+0

나는 이미지에서 하나와 같은 출력을 얻고 있기 때문에 그것을 파싱해야한다고 생각하지 않았다. 솔직히 말하면, json 문자열을 수동으로 생성 할 때 어디에서 파싱 할 지 확신 할 수 없습니다. 그 자바 스크립트 스 니펫은 내가 사용하고있는 유일한 JavaScript이며 에코를 반환하고 자동 완성을 채우는 php 함수를 호출합니다. –

+1

이 기능을 위해 사용중인 js 파일을 연결할 수 있습니까? 근원을 통해 분류 한 후에, 해결책을 찾았을 수도 있습니다. 여기에서 당신의 희망을 얻지 마십시오, 단지 도우려고 노력하십시오. – Daedalus

답변

1

좋아 내가 할 수있는 방법을 발견했습니다 있도록.

자바 스크립트 :

$(".auto-search").autocomplete({ 
     source: '/hotpepper/json/autocomplete_search2', 
     minLength: 2, 
     open: function(event, ui) { 
      $(".ui-autocomplete .ui-menu-item a").each(function (i) { 
       var row = $(this).html(); 
       row=row.replace(/&lt;/g,"<"); 
       row=row.replace(/&gt;/g,">"); 
       $(this).html(row); 
       }); 
     }, 

    }); 


PHP (CodeIgniter의) : 여기에 내가 그것을 어떻게입니다

public function autocomplete_search2() 
{ 
    $term = $this->input->get('term'); 

    //load model and get results 
    $this->load->model("establishment_model"); 
    $results1= $this->establishment_model->search_autocomplete_est($term); 
    $results2= $this->establishment_model->search_autocomplete_loc($term); 

    //Start JSON string 
    $json='['; 

    //first source 
    if ($results1->num_rows() > 0) 
    { 
     $header= "<h3 style='font-weight:bold'>Accommodation:</h3>"; 
     $json .= '{"value":"'.$header.'"}'; 
     foreach ($results1->result_array() as $r1) 
     { 
      $result = str_replace($term,"<strong style='color:#C00'>".$term."</strong>",$r1['establishment_name']); 
      $json .= ',{"value":"'.$result.'"}'; 
     } 
    } 

    //second source 
    if ($results2->num_rows() > 0) 
    { 
     if ($results1->num_rows() > 0){$json .= ',';} 
     $header= "<h3 style='font-weight:bold'>Destinations:</h3>"; 
     $json .= '{"value":"'.$header.'"}'; 

     foreach ($results2->result_array() as $r2) 
     { 
      $result = str_replace($term,"<strong style='color:#C00'>".$term."</strong>",$r2['establishment_location']); 
      $json .= ',{"value":"'.$result.'"}'; 
     } 
    } 

    //Close JSON string 
    $json .= ']'; 
    echo $json; 
} 

자동 완성 난, 난 그냥 언 이스케이프를 통해 보내 내 HTML을 탈출 때문에 자동 완성 상자를 열었을 때 &lt;&gt;을 <>으로 바꿉니다. 편집

:

close: function(event, ui) { 
       var result = $(this).val(); 
       if (result.search("</h3>") ==-1) 
       { 
        result=result.replace(/<strong style='color:#C00'>/g,""); 
        result=result.replace(/<\/strong>/g,""); 
       } 
      else 
      { 
       result=""; 
      } 
      $(this).val(result); 
     } 
+1

글쎄, 당신의 솔루션이 나보다 나은, 소스 파일을 편집했다. 답을 수락하는 것을 잊지 마십시오. – Daedalus

+0

감사합니다. 2 일 만에 받아 들일 수 있습니다. –

관련 문제