Part of the EllisLab Network
   
 
CI and scriptaculous autocomplete
Posted: 08 August 2006 11:27 AM   [ Ignore ]  
Summer Student
Total Posts:  6
Joined  08-01-2006

So I am wrestling with scriptaculous’s autocomplete form and trying to get hat up adn running. I have it working, but because I have CI’s pretty mod rewrite urls on, it requires a hack to hit the controller directly as well as discard the extra search prameters that you no longer need in order to get it to work. Has anyone else got it up and running with CI’s pretty urls? The hack isn’t that bad, but it is a hack.

Original instructions:
http://wiseguysonly.com/2006/04/14/ajax-autocompletion-for-the-impatient/


scriptaculous hack to make the URL mesh with CI’s pretty rewrite:
in getUpdatedChoices: function() inside controls.js:

new Ajax.Request(this.url, this.options);

becomes

new Ajax.Request(this.url + encodeURIComponent(this.getToken()), this.options);

 

My controller with a hack (explode line) to discard all of the scrptiaculous (search=xxx&_=...):

function autocomplete(){

        $type
= $this->uri->segment(3,'');
        
$table = $this->uri->segment(4,'');
        
$field = $this->uri->segment(5,'');
        
$prefix = $this->uri->segment(6,'');
        
$tmp_arr = explode(' search', $prefix);
        
$prefix = $tmp_arr[0];

        
$this->load->helper('autocomplete');
        
$this->data['Val'] = autocomplete($type,$table,$field,$prefix);
        
$this->load->show_smarty('generic_value.tpl',$this->data);
    
}


finally, to call the auto updater I changed the url to accept the controller path as well as the prefix to search for:

<form>
<
input type="text" name="search" id="new_tag"/>    <input type="submit" />
            <
div id="hints_tag"></div>
            new
Ajax.Autocompleter("new_tag","hints_tag","/assets/autocomplete/tag/tags/name/"+$F('new_tag'));</script>
</form>
Profile
 
 
Posted: 08 August 2006 12:49 PM   [ Ignore ]   [ # 1 ]  
Summer Student
Avatar
Total Posts:  25
Joined  04-24-2006

Thanks for the contribution.  I’m sure a lot of us will find your hack helpful down the road (or even right now!)

Profile
 
 
Posted: 06 September 2006 05:31 AM   [ Ignore ]   [ # 2 ]  
Administrator
Avatar
RankRankRankRankRankRank
Total Posts:  7321
Joined  03-23-2006

easement, let me second this… thanks for the contribution.  That said, the idea of hacking up the scriptaculous lib didn’t appeal to me, and in fact there is a much easier way to do this.

// the standard scriptaculous call, my controller name is 'application', function named 'search'
new Ajax.Autocompleter("function_name", "autocomplete_choices", '/application/search/', {});

and in the controller named ‘application’, here is my search function

function search()
    
{
        $searchTerm
= $this->input->post('function_name'); // autocomplete forces the post var back, so just read it out right here.
        
$function_search = $this->functions_model->getFunctionInfo($searchTerm);
        
$function_return = '<ul>';
        foreach (
$function_search as $func) {
            $function_return
.= '<li>' . $func->function_name . '</li>';
        
}
        $function_return
.= '</ul>';
        echo
$function_return;
    
}
 Signature 

DerekAllard.com - CodeIgniter, ExpressionEngine, and the World of Web Design
BambooInvoice - Open Source, CodeIgniter powered invoicing.

Profile
MSG
 
 
Posted: 11 September 2006 07:26 AM   [ Ignore ]   [ # 3 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  320
Joined  04-25-2006

Well I try dallard code and seeing my JS Errors I get this:

Error: Ajax is not defined
Archivo de origen: http://localhost/sql/entrega
Línea: 53

Why?
Note: I use .htaccess file

Regards,

 Signature 

ReynierPM

Profile
 
 
Posted: 11 September 2006 09:46 AM   [ Ignore ]   [ # 4 ]  
Summer Student
Total Posts:  6
Joined  08-01-2006

Reynier:

Ajax is part of the scriptaculous library. Make sure you have the scriptaculous javascript files included in the code that you are rendering.

http://script.aculo.us/downloads

Profile
 
 
Posted: 12 September 2006 12:25 PM   [ Ignore ]   [ # 5 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  320
Joined  04-25-2006

I have included scriptaculous in this way:

</script>
</script>

So what’s wrong here?
Regards,

 Signature 

ReynierPM

Profile
 
 
Posted: 12 September 2006 12:40 PM   [ Ignore ]   [ # 6 ]  
Summer Student
Total Posts:  6
Joined  08-01-2006

Here’s how my page looks. Your code showed up as two closing script tags (I replaced the script with scr1pt so it would show up).

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<
html>
  <
head>
    <
title>AutoSuggest Sample</title>
<
link href="/web_include/css/editinplace.css" rel="Stylesheet" type="text/css" />
<
scr1pt src="/web_include/javascripts/prototype.js" type="text/javascript"></script>
<scr1pt src="/web_include/javascripts/scriptaculous.js" type="text/javascript"></script>
<scr1pt type="text/javascript" src="/web_include/javascripts/effects.js"></script>
<scr1pt type="text/javascript" src="/web_include/javascripts/dragdrop.js"></script>
<scr1pt src="/web_include/javascripts/controller_asset.js" type="text/javascript"></script>
</head>
  </
head>
  <
body>
    <
div id="autosuggest"><ul></ul></div>
    <
h1>AutoSuggest v1.0</h1>

    <
form>
        <
fieldset>
        <
legend>State</legend>
        <
p>(Up and down arrows highlight a suggestion. Tab key inserts the highlighted suggestion. Escape hides the list.)</p>
        <
label for="state">Please enter a State:</label>
        <
input type="text" name="search" id="search2"/>
            <
div id="hints"></div>
            new
Ajax.Autocompleter("search2","hints","/assets/autocomplete/tag/tags/name/"+$F('search2'));</script>
        
<p>
        <
input type="submit" />
        </
p>
        </
fieldset>
    </
form>
    


    

    
  </
body>
</
html>


And my autocomplete functin looks like:

function autocomplete(){
        try{
            $type
= $this->uri->segment(3,'');
            
$table = $this->uri->segment(4,'');
            
$field = $this->uri->segment(5,'');
            
$prefix = $this->uri->segment(6,'');
            
$tmp_arr = explode(' search', $prefix);
            
$prefix = $tmp_arr[0];

            
$this->load->helper('autocomplete');
            
$this->data['Val'] = autocomplete($type,$table,$field,$prefix);
            
$this->load->show_smarty('generic_value.tpl',$this->data);
        
}catch(Exception $e){
            show_error
($e->getMessage());
        
}
    }

 

And my autocomplete_helper.php inside the system file looks like:

<?


function autocomplete($type,$table,$field,$prefix){
    $res
= array("");
    
// firstly clean up the data
    
$prefix = ltrim(preg_replace('/[^a-z0-9_. ]/', '', strtolower($prefix)));
    
$prefix = preg_replace('/\s+/', ' ', $prefix);
    
    if (
strlen($prefix) > 0) {
        $obj
=& get_instance();
        
$obj->load->database();

        
//Form the query
        
switch ($type){
            
case 'serv': //services use type and name to make the priamry key. type is hard coded as service
                
$query = $obj->db->query("SELECT $field, UPPER($field) AS fieldUC FROM $table WHERE type='service' AND $field ILIKE '$prefix%' ORDER BY fieldUC ASC");
                break;
            default:
                
$query = $obj->db->query("SELECT $field, UPPER($field) AS fieldUC FROM $table WHERE $field ILIKE '$prefix%' ORDER BY fieldUC ASC");
                break;
        
}
        

        $res
= $query->result_array();
    
}

    $val
= '';

    
//build the ul for the script.taculo.us autocompleter
    
if(count($res)!=0 && is_array($res)){
        $val
.= "<ul>";
        foreach (
$res as $r){
          $val
.= "<li>" .  $r[$field] . "</li>";
        
}
        $val
.= "</ul>";
    
}

    
return $val;

}


?>
Profile
 
 
Posted: 27 June 2008 04:42 PM   [ Ignore ]   [ # 7 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  287
Joined  01-25-2008
Derek Allard - 06 September 2006 05:31 AM

easement, let me second this… thanks for the contribution.  That said, the idea of hacking up the scriptaculous lib didn’t appeal to me, and in fact there is a much easier way to do this.

// the standard scriptaculous call, my controller name is 'application', function named 'search'
new Ajax.Autocompleter("function_name", "autocomplete_choices", '/application/search/', {});

and in the controller named ‘application’, here is my search function

function search()
    
{
        $searchTerm
= $this->input->post('function_name'); // autocomplete forces the post var back, so just read it out right here.
        
$function_search = $this->functions_model->getFunctionInfo($searchTerm);
        
$function_return = '<ul>';
        foreach (
$function_search as $func) {
            $function_return
.= '<li>' . $func->function_name . '</li>';
        
}
        $function_return
.= '</ul>';
        echo
$function_return;
    
}

 

HEy Derek.
Is there a way to pass in another POST variable back with the AutoCompleter?

For example… a user chooses the state field then comes to the city field… based on the keys he types, the city is populated. But of course, he can go BACK to the state field and change it.. in which case the the AutoCompleter needs to know.

1) How do I pass this extra variable name to the function that gets called? I’ve noticed that only the value in the “function_name” field gets passed back. What if I also wanted to pass another value in a different field?

2) IF solution to #1 is static, is there an “update” for the AutoCompleter that I can invoke once it’s been created?

 Signature 

http://PawshPal.com/ - Pets Rule

Profile
 
 
Posted: 27 June 2008 04:52 PM   [ Ignore ]   [ # 8 ]  
Administrator
Avatar
RankRankRankRankRankRank
Total Posts:  7321
Joined  03-23-2006

Yikes!  This thread is nearly 2 years old. Good thing good information never goes out of style!

I subsequently did http://video.derekallard.com, which may help you out, but I’ve long since moved onto jQuery, which I adore, so I’m not much help anymore with scripto - sorry.

 Signature 

DerekAllard.com - CodeIgniter, ExpressionEngine, and the World of Web Design
BambooInvoice - Open Source, CodeIgniter powered invoicing.

Profile
MSG
 
 
Posted: 05 March 2009 01:24 AM   [ Ignore ]   [ # 9 ]  
Summer Student
Total Posts:  1
Joined  03-05-2009

Suggested examples
link

Profile
 
 
Posted: 12 July 2009 10:48 AM   [ Ignore ]   [ # 10 ]  
Summer Student
Total Posts:  3
Joined  10-28-2007

I have successfully taken Derek’s code (http://video.derekallard.com/) to create a dynamic information retrieval form. It works great. I now want to add some extra functionality.

I’m building a form where a user will first select their city from a drop down list and then start typing into a text box their town/city which will then automatically appear. Thus, the dynamic retrieval of cities will only select cities from the country selected.

I need to know how to pass the select box data (country) through AJAX along with what the user types into the text box (city/town). I’m guessing I need to add some code to function_search.js in the javascript folder, but am not sure…

Any pointers would be greatly received.
Thanks
Rob

Profile
 
 
   
 
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 721, on January 06, 2010 09:38 AM
Total Registered Members: 115023 Total Logged-in Users: 70
Total Topics: 122459 Total Anonymous Users: 6
Total Replies: 647356 Total Guests: 474
Total Posts: 769815    
Members ( View Memberlist )