CI中的自动完成搜索

问题描述:

海兰,我是笨使自动完成搜索领域,CI中的自动完成搜索

这是我的观点:

<!doctype html> 
<html> 
<head> 
<meta charset="utf-8"> 
<title>Untitled Document</title> 
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" /> 
<script src="http://code.jquery.com/jquery-1.9.1.js"></script> 
<script src="http://code.jquery.com/ui/1.10.2/jquery-ui.js" ></script> 

<script> 
$(document).ready(function() { 
    $(function(){ 
     $("#text").autocomplete({ 
      source: function(request, response) { 
       $.ajax({ 
       url: "http://localhost/new/index.php/travels/search_fields", 
       data: { term: $("#text").val()}, 
       dataType: "json", 
       type: "POST", 
       success: function(data){ 
        response(data); 
       } 
      }); 
     }, 
     minLength: 2 
     }); 
    }); 
}); 
</script> 
</head> 

<body> 
<form method="post"> 
<input type="text" name="text" id="text" autocomplete="off" > 
</form> 
</body> 
</html> 

这里是我的控制器:

function search_fields(){ 
    $term = $this->input->post('term', TRUE); 
    $data['var']= $this->Travel->search_field($term); 
    echo json_encode($data['var']); 

} 

,这里是我的型号:

function search_field($term){ 
    $this->db->distinct(); 
    $this->db->select("destination"); 
    $this->db->from('travels_detail'); 
    $this->db->like('destination', $term); 
    $this->db->group_by('travels_detail.destination'); 
    $query = $this->db->get(); 
    return $query->result(); 

} 

我得到一个空的下拉列表,我做错了什么? ajax或library等有什么问题吗?请任何人帮助我。

+1

之前'响应(数据);'线使用'控制台.log(data)'并显示出来。让我知道 –

在模型中的返回result_array

return $query->result_array(); 

在控制器:

$search_data = $this->Travel->search_field($term); 
echo json_encode($search_data); 

AJAX成功后,某些变化:

success: function(data){ 
    var resp = $.map(data,function(obj){ 
     return obj.destination; 
    }); 
    response(resp); 
} 
+0

thanx @razib al mamun –