我想制作一个自动完成搜索框,当我尝试实现搜索时,它会逐个字母而不是逐字返回。当我有两个值时,它也会从 db 返回一个值。我正在使用 bootstrap 中的 typeahead.min.js。为什么会这样?
控制器
public function search(){
$search = $this->input->post('search');
$query = $this->products->search($search);
$data = array();
foreach ($query as $key => $value) {
$this->json($value);
}
}
模型
public function search($data){
$this->db->like('description',$data,'both');
$query = $this->db->get('storelte_products');
return $query->result_array();
}
阿贾克斯
$('#search').typeahead({
source: function (query,process) {
$.ajax({
url: URL_SEARCH_PRODUCT,
type: 'POST',
data: {query: query},
dataType: 'json',
async: true,
success: function (data) {
console.log(data);
process(data.description);
}
});
}
});
{"id":"4165","provider_id":"1005","hash_id":"a7f0d2b95c60161b3f3c82f764b1d1c9","code":"BCBF-BE6A","description":"ajg","purchase_price":"7.00","sale_price":"77.00","wholesale_price":"7.00","min_stock":"7","stock":"7","max_stock":"7","created_at":"2017-04-09 22:21:28","status":"1"}