Yiibooster

Hello! how can one use the Typeahead (bootstrap.widgets.TbTypeahead) OR CJuiAutoComplete to have autocomplete feature on related models (foreign key one-to-many relation)?

This is a very common scenario.

For example, when you have the following models and fields::


User.id_city


City.id

City.name


User.id_city is a foreign key to City.id



I’d like to do something similar to the following:




$this->widget('bootstrap.widgets.TbTypeahead', array(

    'model'=>$model, // instance of model 'User'

    'attribute'=>'id_city', // foreign key field

    'options'=>array(

        'source'=>City::model()->findAll(),

        'valueAttribute'=>'id', // city.id

        'displayAttribute'=>'name', // city.name

        'items'=>4,

        'matcher'=>"js:function(item) {return ~item.toLowerCase().indexOf(this.query.toLowerCase());}",

        )

    )

));



In other words, similar to how you can use the traditional drop-down list:


<?php echo $form->dropDownList($model, 'id_city', CHtml::listData(City::model()->findAll(), 'id', 'name')); ?>



…but with autocomplete feature.

How can this be done? Thank you.

UPDATE:

After reading a lot of forum posts, I can see that Typeahead and CJuiAutoComplete were not designed to work with "value and display" attributes of related models so you can use the required value/id of a selected display/name to submit for model creation/update.

Typeahead and CJuiAutoComplete seem to behave like “simple” autocomplete textboxes and I couldn’t see an easy way to implement the value/display attributes behavior.

Fortunately for me, I found the select2 extension and it was as simple as copying the extension folder and using the following in a view:




<?php $this->widget('ext.select2.ESelect2',array(

	'model'=>$model,

	'attribute'=>'id_city',

	'data'=>CHtml::listData(City::model()->findAll(), 'id', 'nombre'),

)); ?>



Simple code to implement, works fine and looks nice.

Just wanted to share what I used as a solution.

Could you tell me what is the browser you are using? I found my self having to modify some of the jquery plugins used, maybe this is one of the cases

You have to set the class as per column configuration, use the example at the yiibooster site per each column, it shouldn’t be a problem, is just a column class

You will have to render the form yourself using the required widgets per position as your layout is a combination of the bootstrap grid system and forms vertical.

I wouldn’t do it that way whenever you have to use an autocomplete. I would make use of the source for an ajax response.

Thanks for posting your solution, in fact, TbSelect2 is a new widget ready for the next version of YiiBooster. I just hope to have some time free to do it sometime soon.

My son gets operated, so I wont be able to do it till late this weekend.

Cheers and apologies for late response to all of you.

I am using Chrome 23.0.1271.64.

And I wish your son the best…

Hi,

Wonder if anyone else has had a similar issue. I am using An TBEditableField within a CGridview (using renderpartial) which is working fine. However I also have a dropdown box and AJAX button which add an item to the Gridview and causes the Gridview to refresh. When this happens the TBeditableFields stop working until you manually reload the page using F5. I have attached screenshot of what happens.

I can see from firebug that when the Gridview refreshes the items lose the editable attributes


<a href="#" rel="Attributevalue_value_102" data-pk="102" data-placement="right" data-original-title="Enter Value" class="editable">C</a>

changes to


<a href="#" rel="Attributevalue_value_102" data-pk="102">C</a>

I have found this on Stack Overflow but I have tried adding the suggestion to now avail.

Any suggestions would be greatly received.

Thanks

David

Instead of using TbEditableField use TbEditableColumn that solves the issue of ajax refresh a grid :)

Thank you so much for your wishes… my son came out great… everything went fine…

I am sorry but it seems there is an issue with the CSS… will talk to the designer at the company trying to figure out what is the reason of such behavior. If you could provide some example code for us, would be highly appreciated.

Thanks!

YiiBooster 1.0.4 is out

Thanks for your great effort.

How to use image gallery?

Any code snippet will be appreciated.

Going to write two wikis:

One for the gallery

Another one for the fileUpload

I am doing both right now

Thank you Antonio for a new version of YiiBooster!

Would you please specify in the YiiBooster documentation the options available for ‘select2’? I mean the options that you made available in your YiiBooster implementation and that there are not documentation available elsewhere (e.g. ‘asDropDownList’ property), not the full set of options that we can set in the ‘options’ property (which we can read in the ‘select2’ official documentation). It would be a great plus for the documentation of YiiBooster to have something like this for every component.

Of course, only if there are new options/properties introduced by you in your YiiBooster implementation. Those are the options that I’m referring to, that I think it’s a good idea to list and document in every component.

Antonio, I can’t get the placeholder of ‘select2’ to show… am I doing something wrong?


<?php $this->widget('bootstrap.widgets.TbSelect2', array(

	'model'=>$model,

	'attribute'=>'id_city',

	'asDropDownList'=>true,

	'data'=>CHtml::encodeArray(CHtml::listData(City::model()->findAll(), 'id', 'name')),

	'options' => array(

		'placeholder' => 'Please select',

		'allowClear'=>false,

))); ?>

It loads the values correctly but the first value is automatically pre-selected in the list. I want it to show ‘Please select’ to force the user to select an option.

I was using the ‘eselect2’ extension before, and this code was working for me:


<?php $this->widget('ext.select2.ESelect2',array(

	'model'=>$model,

	'attribute'=>'id_city',

	'data'=>CHtml::encodeArray(CHtml::listData(City::model()->findAll(), 'id', 'name')),

	'options'=>array(

		'placeholder'=>'Please select',

		'allowClear'=>false,

))); ?>

Thanks.

You are great.

Go ahead…

No need for CHtml::encodeArray, apart from that, all seems right to me…

Will have to wait one more day… got caught up by work

I am planning to create a new repository at git, to work for the API Docs. I am so busy right now that is nearly impossible to do it alone and keep up with new versions.

Will let you know when it happens

Thanks

Hola Antonio,

you didn’t include TbExtendedFilter in version 1.0.4. I was really looking forward to it… bummer!

An observation: Shouldn’t the input for TbSelect2 have round corners, too?

Another question: Is there any way to omit the highlighting of errors in forms? It would be quite useful if there were the following boolean options: highlightErrorsOnLabels, highlightErrorsOnInputs, highlightErrorsOnHelpers. Of course the default is true. When set to false the element will not be highlighted red if there is any error.

Edit: The original option "errorMessageCssClass" gets overwritten in TbActiveForm and it would only effect the help-block (not the label and input) hence the overwrite.

Cheers,

Felipe

There you go for the Image Gallery, will try to write the second one asap…

http://www.yiiframework.com/wiki/424/how-to-use-yiibooster-bootstrap-gallery/

Cheers