Help On Implementing Autocomplete

Hi all,

I have a problem implementing autocomlete. The situation is as following:

Table 1: Messages: with field ‘user_id_rec’

Table 2: Profiles: with fields ‘user_id’ and ‘compname’

In the create view of Messages id like to have the ‘user_id_rec’ as autocompleted field.

The list has to consist of the ‘compnames’ of the table profiles and the ‘user_id’ of the chosen name should be submitted when the save button is hit.

I tried to achieve this by following this tutorial. But i can’t get it to work. The field does appear on the form, but does not autocomplete.

I did the following:

created the file at step one and saved it to the extensions map.

added the code from step 2 to my Profiles model. I changed the sql there to:

$sql= ‘SELECT user_id ,compname AS label FROM users WHERE compname LIKE :name’;

added the code from step 3 to my profiles controller. I changed some code there too.

$users = Users::usersAutoComplete($term); changed too $users = Profiles::usersAutoComplete($term);

added the code from step 4 to my _form-file of messages and changed it to this:




	<div class="row">

	<?php 

	// Note: ext.MyAutoComplete is equivalent/shortcut to application.extensions.MyAutoComplete

// This means Look for protected/extensions/MyAutoComplete.php file

$this->widget('ext.myAutoComplete', array(

    'model'=>$model,

    'attribute'=>'user_id_rec',

    'name'=>'compname',

    'source'=>$this->createUrl('profiles/usersAutoComplete'),  // Controller/Action path for action we created in step 4.

    // additional javascript options for the autocomplete plugin

    'options'=>array(

        'minLength'=>'0',

    ),

    'htmlOptions'=>array(

        'style'=>'height:20px;',

    ),        

)); ?>

</div>



i tried so many things the last hour, i can’t see it right anymore.

Some help would be appreciated

Thanks in advance!

Are you getting any javascript errors in your browser’s console?

Are the ajax requests being performed, and if so, is the data being correctly returned from the server?

Hi Keith, Thanks for your reply

i don’t get javascript errors in the browser.

I don’t know how to get the answers on your next two questions.

As i type the field, nothing happens. Im quite new to Yii, so how do i check if the ajax requests are being performed?

If you’re using Chrome, you can go into Tools -> Developer Tools. In there, there’s a Network tab which will show you the inbound and outbound traffic from the browser. Open up the tab before reloading the page so you can capture all the data.

You can click on the name of a request to view both the data that was sent (in the headers) as well as the response returned.

I don’t know the details involved with other browsers, but I expect they have equivalent functionality.

there are 28 requests. i’ve no idea which one i have to look for.

Let’s assume its allright. is de code i used right? and is het on the right places?

other ajax is working on the site btw.

edit: while the page was open, i typed in the auto complete field and some extra RED requests appeared. Wrong sql statement. Thanks Keith

sorry to say i have run into another problem:

the dropdown options list is working now, but it seems that there is no id passed to the value of the field. The form keeps telling me that the field may not be empty.

I think the problem occurs because I have an table field “user_id”, and de tutorial could be using “id” as a table field in the code. But i’ve changed code and nothing helps. any suggestions?

Thanks in advance

update: i’ve looked into the request. This is sent:

Messages[user_id_send]:2

Messages[date_time_sent]:

Messages[user_id_rec]:other bv

Messages[user_id_rec]:

Messages[subject]:atest

Messages[content]:gftesag

yt0:Send message

strange thing is: User_id_rec field is twice in the list!

UPDATE: i fixed it myself. The difference between user_id and id caused it indeed. I fixed it by adding someting to the sql statement: "Select user_id AS id"