问题
I am working on a website in the codeigniter framework. I am stuck at a point where I need to implement the auto complete feature. I have tried a lot but I am not able to find proper solution so far. Here is what my actual requirements are.
There is a page on website that has few search filters. When a person lands on this page all the users of the website are shown on that page. Here the real game starts. There is a filter or an input box that filters out the results on the basis of their first or last name.
Say I have a database in which i have 3 users.
Ahmad Nawaz John Azaar Monica Finlay
When a person starts typing "Ah" in that search box I want that the sugesstions start to appear showing him "Ahmad"... Please tell me how to do that? I have searched a lot out there but i could not find a proper answer in reference to codeignitor. this is what my code looks like at the moment...
<input type="text" placeholder="Persons Name" name="individual_name" id="individual_name">
<script>
$(function() {
$( "#individual_name" ).autocomplete({
source: ('autocomplete_individual_name'),
select: function () {
testing()
}
});
});
</script>
just under the input I wrote the script....It goes to my mentioned controller. Here is what the controller looks like...
$individual_name = $this->input->post('individual_name');
$where = "first_name LIKE '".$individual_name."%' OR last_name LIKE '".$individual_name."%'";
$users_array = $this->user_profile_model->findByCondition($where);
First Problem $individual_name is not getting populated.
Second Problem When I receive the results in users_array, what should I do next? How to pass it back to show suggestions??
Third Problem I use to call a filter function onkeyup(). Now when a person selects through the suggestion how to call the filter?
P.S->Also kindly let me know how can i reply to the person who replies me on this question...I have used @ sign with user but it seems they dont get my reply thats why they never returned....
Any help would be highly highly appreciable...
Thanks and waiting Ahmad
回答1:
Try something like this, You may need to change the code slightly to suit you,
In your script part:
$("#individual_name").autocomplete({
source : base_url+"controller_name/suggest_names",
minLength : 1,
select: function( event, ui ) {
alert('id :'+ui.item.value) ;
//document.location.href = base_url+"controller_name/search?keyword="+ui.item.value; do something or redirect
},
success : function(resp){
//alert("auto");
},
error : function(){
alert("Oops, that didn't work. Please try again.");
}
});
In your controller:
function suggest_names(){
print_r ( $this->model_name->suggest_names($_REQUEST['term']) );
}
In your model part:
function suggest_names($term){
$data = array();
$term = strtolower( addslashes( trim( urldecode($term) ) ) );
$temp = $this->db->select('name as label, id as value')->like('name', $term, 'LEFT')->get('table_name')->result_array();
$data = json_encode($temp);
//echo "<pre>";print_r($data);echo "</pre>";die;
return $data;
}
Let me know if you face any problem. Hope it works for you.
来源:https://stackoverflow.com/questions/18655026/suggestions-on-the-search-filter