2014-02-25 44 views
1

我試圖創建CI實時搜索功能(這是我從來沒有做過),生活搜索。我對網絡開發很陌生,現在仍在學習。我發現瞭如何做到這一點http://www.blog.orionwebtech.net/codeigniter-jquery-ajax-live-search/?codekitCB=415037771.748888創建具有AJAX和笨

我有翻譯從該教程我的應用程序的代碼問題,這個小教程。我有一個名爲屬性的表,我希望將輸入值與slug列和名稱列進行比較。然後我希望它在實時搜索結果中返回slug和名稱,其中輸入值與這些列中的任何一個匹配。它不會匹配,因爲slu contains只包含數字而名稱包含字母。

這是我想出了嘗試做到這一點的代碼。

的觀點:

<div class="something"> 

    <input name="search_data" id="search_data" class="" value="" data-label="Search for a property, a unit, or an resident..." type="text" /> 

    <div id="suggestions"> 

     <div id="suggestionslist"> 

     </div> 

    </div> 

</div> 

中的JavaScript:

<script type="text/javascript"> 
     function ajaxSearch() { 
      var input_data = $('#search_data').val(); 
      if (input_data.length === 0) { 
       $('#suggestions').hide(); 
      } else { 

       var post_data = { 
        'search_data': input_data, 
        '<?php echo $this->security->get_csrf_token_name(); ?>': '<?php echo $this->security->get_csrf_hash(); ?>' 
       }; 

       $.ajax({ 
        type: "POST", 
        url: "<?php echo base_url(); ?>search/autocomplete", 
        data: post_data, 
        success: function(data) { 
         // return success 
         if (data.length > 0) { 
          $('#suggestions').show(); 
          $('#autoSuggestionsList').addClass('auto_list'); 
          $('#autoSuggestionsList').html(data); 
         } 
        } 
       }); 

      } 
     } 
</script> 

控制器:

<?php if (! defined('BASEPATH')) exit('No direct script access allowed'); 

class Search extends CI_Controller 
{ 
    public function __construct() 
    { 
     parent::__construct(); 
    } 

    public function autocomplete() 
    { 
     $search_data = $this->input->post('search_data'); 
     $query = $this->Search_model->get_autocomplete($search_data); 

     foreach ($query->result() as $row): 
      echo "<li><a href='" . base_url('association/'.$row->slug) . "'>" . $row->name . "</a></li>"; 
     endforeach; 
    } 
} 
/* End of file search.php */ 
/* File location: application/controllers */ 

的型號:

<?php if (! defined('BASEPATH')) exit('No direct script access allowed'); 

class Search_model extends CI_Model 
{ 
    public function __construct() 
    { 
     $this->load->database(); 
    } 

    public function get_autocomplete($search_data) 
    { 
     $this->db->select('slug, name'); 
     $this->db->like('slug', $search_data); 
     $this->db->like('name', $search_data); 
     return $this->db->get('properties', 10); 
    } 
} 

?> 

當我測試它時,我沒有得到任何結果。我的測試數據是有效的,因爲db中有一個匹配的行。我究竟做錯了什麼?

+0

你不要在你的控制器加載Search_model。你應該使用Firebug或其他東西來觀察你的服務器響應。錯誤可能是別的。當你打電話給你的服務 看看你的Javascript控制檯。如果你沒有結果顯示呢?你不會處理錯誤。 –

回答

0

根據你的鏈接你缺少onkeyup=ajaxSearch();

<input name="search_data" id="search_data" class="" value="" data-label="Search for a property, a unit, or an resident..." type="text" onkeyup="ajaxSearch();" />

0
<?php if (! defined('BASEPATH')) exit('No direct script access allowed'); 

class Search_model extends CI_Model 
{ 
    public function __construct() 
    { 
     parent::__construct(); 
    } 

    public function get_autocomplete($search_data) 
    { 
     $this->db->select('slug, name'); 
     $this->db->like('slug', $search_data); 
     $this->db->like('name', $search_data); 
     $query = $this->db->get('properties', 10); 
     return $query->result(); 
    } 
} 

?>