2012-05-30 33 views
3

我試圖用我從site.But它得到的代碼來執行自動完成在codeignter不工作me.Can人發現問題自動完成的笨

,我用下面

給出的視圖功能
<html> 

    <head> 
     <title>Autocomplete</title> 

     <script src="<?php echo base_url().'js/jquery.autocomplete.js'?>"></script> 
     <script src="<?php echo base_url().'js/jquery-1.6.4.js'?>"></script> 

     <script type="text/javascript"> 

      $(function() { 
      $("#username").autocomplete({ //the recipient text field with id #username 
      source: function(request, response) { 
      $.ajax({ 
       url: "http://localhost/autocomplete/index.php/autocontroller/search_username", 
       dataType: "json", 
       data: request, 
       success: function(data){ 
        if(data.response == 'true') { 
         response(data.message); 
        } 
       } 
      }); 
     } 
    }); 
}); 

     </script> 
    </head> 

    <body> 

     <h1>Autocomplete</h1> 

     <input type="text" id="username" value="" /> 
    </body> 

</html> 

,我已經使用的控制器下面

<?php 
/* 
* To change this template, choose Tools | Templates 
* and open the template in the editor. 
*/ 

/** 
* Description of autocontroller 
* 
* @author Sivam 
*/ 
class autocontroller extends CI_Controller { 

    public function __construct() { 
     parent::__construct(); 
     $this->load->helper(array('form', 'url')); 
     $this->load->database(); 
    } 

    function index() 
    { 
     $this->load->view('autoview'); 
    } 

    function search_username() 
{ 
     $username = trim($this->input->get('term', TRUE)); //get term parameter sent via text field. Not sure how secure get() is 

     $this->db->select('fname'); 
     $this->db->from('users'); 
     $this->db->like('fname', $username); 
     $this->db->limit('5'); 
     $query = $this->db->get(); 

     if ($query->num_rows() > 0) 
     { 
      $data['response'] = 'true'; //If username exists set true 
      $data['message'] = array(); 

      foreach ($query->result() as $row) 
      { 
       $data['message'][] = array(
        'label' => $row->fname, 
        'value' => $row->fname 
       ); 
      } 
     } 
     else 
     { 
      $data['response'] = 'false'; //Set false if user not valid 
     } 

     echo json_encode($data); 
} 

} 
?> 
+0

這行看起來很可疑'response(data.message);'如果有任何錯誤 – allen213

回答

3

給出我無法理解爲什麼你用

$data['message'][] = array(
        'label' => $row->fname, 
        'value' => $row->fname 
       ); 

您可以嘗試一維數組。

$data['message'] = array(
         'label' => $row->fname, 
         'value' => $row->fname 
        ); 

我也使用自動完成按照這個。 http://www.jamipietila.fi/codeigniter-and-autocomplete-with-jquery/請試試這個......它的作品適合我..很好,它也適用於你。

+0

我認爲他使用$ data ['message'] [],因爲查詢返回了多行。你的建議只會存儲最後一行。 – allen213

+0

ya @ allen213,我認爲你是對的..... :) –

0

這是在codeigniter中使用自動完成的簡單方法。它爲我工作。

在View:

<script src="http://code.jquery.com/jquery-1.10.2.min.js"></script> 

<script type="text/javascript"> 
function lookup(inputString) { 
if(inputString.length == 0) { 
    $('#suggestions').hide(); 
} else { 
    $.post("<?php echo base_url() ?>your_controller/your_action/"+inputString, function(data){ 
     if(data.length > 0) { 
      $('#suggestions').show(); 
      $('#autoSuggestionsList').html(data); 
     } 
    }); 
} 

}

function fill(thisValue) { 
$('#id_input').val(thisValue); 
    setTimeout("$('#suggestions').hide();", 200); 
} 
</script> 


<?php echo form_input('company_name', '', "id='id_input' autocomplete='off' onKeyUp='lookup(this.value)'") ?> 

<div id="suggestions"> 
    <div class="autoSuggestionsList_l" id="autoSuggestionsList"></div> 
</div> 

這裏的第一行着,當然,調用jQuery庫。

接下來我們有2個功能。 lookup()接受用戶輸入,然後調用相關的URL來完成必要的數據庫調用。

下一個函數fill()將卸載將包含結果輸出的div。

最後在視圖中,我添加了文本輸入框,並且在用戶給出框中的輸入時還鉤住了查找功能。在輸入框下面,我們有一個div,它會顯示結果,如果有的話。只有當用戶的輸入產生結果時,該div纔可見。

在控制器,我們有:

function autocomplete($a) 
{ 
    $i = 0; 
    $this->load->model('company'); 
    $companyList = $this->company->get_companies($a); 

    if(count($companyList) > 0): 
    echo "<ul>"; 
    foreach($companyList as $comp): 
     echo "<li id='".$companyList[$i]['id']."'><a href='#'>".$companyList[$i]['name']."</a></li>"; 
     $i++; 
    endforeach; 
    echo "</ul>"; 
    endif; 
} 

在這裏,在上面的函數,它接受的公司名稱(或它的一部分),然後我打電話的功能get_companies()的字符串。這個函數會調用數據庫並將結果作爲數組返回。

在型號:

public function get_companies($name) 
{ 
    //$this->db->_compile_select(); 
    $this->db->like('company_name', $name, 'after'); 

    //$this->db->where('id', $name); 

    $query = $this->db->get('companies'); 

    //echo $this->db->last_query(); 
    //echo $query->num_rows(); 
    $companies = array(); 
    $i = 0; 
    if($query->num_rows() > 0) 
    { 
     foreach ($query->result() as $row) 
     { 
      $companies[$i]['id'] = $row->id; 
      $companies[$i]['name'] = $row->company_name; 
      $i++; 
     } 


    } 

    //print_r($companies); 
    return $companies; 


} 

此上述功能是使數據庫調用和返回2D陣列,一個單元是所述ID和另一個名稱。