I am working on a form with 2 autocomplete widgets. The first widget gets the company name and populates various other items on the form:
<?php $this->widget('zii.widgets.jui.CJuiAutoComplete', array(
'attribute'=>'company_name',
'model'=>$model,
'sourceUrl'=>array('company/aclist'),
'options'=>array(
'minLength'=>'2',
'select'=>'js: function(e,u){
$("#'.CHtml::activeId($model,'company_address').'").val(u.item.address);
$("#'.CHtml::activeId($model,'company_phone').'").val(u.item.phone);
$("#'.CHtml::activeId($model,'company_id').'").val(u.item.id);
}',
),
'htmlOptions'=>array(
'size'=>45,
'maxlength'=>45,
),
));?>
This works perfectly. The second widget is very similar except it is meant to grab contacts associated with that company. How do I pass the company_id value to the second autocomplete?
This is what I tried, but it didn’t work:
'sourceUrl'=>array(
'company/aclist',
array("id"=>"js:$('#".CHtml::activeId($model,'company_id')."').val();"),
),
Anyone have any other suggestions on how to accomplish this?
The correct syntax would be:
But I’m afraid there is a problem. I’m almost sure that “js:$(..” would be treat as a string, so it wouldn’t be evaluated. So my suggestion is to implement the solution provided here.
That solution is applicable here because ‘sourceUrl’ invokes CHtml::normalizeUrl(), which in turn calls to CHtml::createUrl(). You would need to change the ‘click’ event to ‘change’. Of course there some other modifications that need to be done, but I hope you can work them out.