how to create custom autocomplete textfield in yii - php

I am new to yii. I need to write custom yii auto complete.I knew that CJuiAutocomplete is there.but I need to implement own custom auto complete. can anyone pls guide me or help me to develop custom autocomplete textfield. taking the id while displaying name in the textfield.
Thanks in advance

Here is an action in site controller...
public function actionAutoComplete($term){
$query = Yourmodel::model()->findallbyattributes( array('somecolumn'=>$term));
$list = array();
foreach($query as $q){
$data['value']= $q['id'];
$data['label']= $q['name'];
$list[]= $data;
unset($data);
}
echo json_encode($list);
}
and here is a search form in your view:
$form=$this->beginWidget('CActiveForm', array(
'id'=>'searchform',
'enableAjaxValidation'=>false,
'action' => '/'
)); ?>
<fieldset>
<div class="input-append">
<?php
echo CHtml::hiddenField('selectedvalue','');
$this->widget('zii.widgets.jui.CJuiAutoComplete', array(
'name'=>'searchbox',
'value'=>'',
'source'=>CController::createUrl('/site/autoComplete'),
'options'=>array(
'showAnim'=>'fold',
'minLength'=>'2',
'select'=>'js:function( event, ui ) {
$("#searchbox").val( ui.item.label );
$("#selectedvalue").val( ui.item.value );
return false;
}',
),
'htmlOptions'=>array(
'onfocus' => 'js: this.value = null; $("#searchbox").val(null); $("#selectedvalue").val(null);',
'class' => 'input-xxlarge search-query',
'placeholder' => "Search...",
),
));
echo '<button class="btn" type="submit">Submit</button>';
?>
</div>
</fieldset>
<?php $this->endWidget(); ?>
</form>

Because of this condition
array('somecolumn'=>$term)
it will show results only if you write full string. For example, you have ['coffee', 'cake']. When you type in search box it wont show results for coff, cof, co, ca, cak etc. only will show results if you enter full word coffee, you will get ['coffee'] as result.
So you need something like:
$match = $_GET['term'];
$tags = Tags::model()->findAll(
'tag_name LIKE :match',
array(':match' => "%$match%")
);
This will show results for coff, cof, co, ca, cak etc.

Related

Yii dropdown with AJAX not working as expected

I have the following view named 'findreseller.php':
<?php
$countries = CHtml::listData(Country::model()->findAll(), 'id', 'name');
echo CHtml::dropdownlist('find_reseller', '', $countries,
array('ajax'=>array(
'type'=>'POST',
'url'=>Yii::app()->getController()->createAbsoluteUrl('/webcare/reseller/loadAjaxData'),
'update' =>'#data',
)
)
);
?>
<div id="data">
<?php $this->renderPartial('_ajaxContent', array('dataProvider'=>$dataProvider))?>
</div>
_ajaxContent just echoes the result, nothing special there...
The dropdown, as you can see is generated with CHtml because I dont't need a form. I just need an onChange event to do something...
As per the code that follows, in '/webcare/reseller/loadAjaxData' I have:
public function actionLoadAjaxData() {
$country = $_POST['find_reseller'];
//do something...
$dataProvider=new CArrayDataProvider($country_reseller);
$this->render('findreseller', array('dataProvider' => $dataProvider));
}
I can tell that I am missing something, but I am not sure what exactly.
Edit
Modified like this:
<?php
//CHtml::form();
$countries = CHtml::listData(Country::model()->findAll(), 'id', 'name');
echo CHtml::dropdownlist('find_reseller', '', $countries,
array(
'ajax' => array(
'type'=>'POST', //request type
'url'=>CController::createUrl('/webcare/reseller/loadAjaxData'), //url to call.
//Style: CController::createUrl('currentController/methodToCall')
'update'=>'#city_id', //selector to update
'data'=>'js: $(this).val()',
//leave out the data key to pass all form values through
)
)
);
//empty since it will be filled by the other dropdown
echo CHtml::dropDownList('city_id','', array());
//CHtml::endForm();
?>
<div id="data">
<?php $this->renderPartial('_ajaxContent', array('dataProvider'=>$dataProvider))?>
</div>
And now I get:
http://prntscr.com/42wwx6
And I have the following controller action:
public function actionLoadAjaxData() {
$country = $_POST['country_id'];
...
$dataProvider=new CArrayDataProvider($country_reseller);
$data = User::model()->findAll('country_id=:country_id AND reseller=:reseller',
array(':country_id'=>(int) $_POST['country_id'], ':reseller'=>1));
$data=CHtml::listData($data,'city','city');
foreach($data as $value=>$name)
{
echo CHtml::tag('option',
array('value'=>$value),CHtml::encode($name),true);
}
$this->render('action_name', array('dataProvider' => $dataProvider));
}
Edit 2
If I write a die in actionLoadAjaxData(), right at the beginning, the method is loaded fine, the action is ok and the server answers 200.

Yii. Application form. Adding info about user company + SEVERAL goods in one form

Don't know, if it is a typical problem, but can't figure how to realise it.
3 tables:
Application. It's relations:
return array(
'company_info' => array(self::HAS_ONE, 'AppCompany', 'applicant_id'),
'region'=>array(
self::HAS_ONE,'AppRegion',array('company_region_id'=>'id'),
'through'=>'company_info'
),
'sector'=>array(
self::HAS_ONE,'AppSector',array('company_sector_id'=>'id'),
'through'=>'company_info'
),
);
AppCompany. Relations:
return array(
'application' => array(self::BELONGS_TO, 'Application', 'applicant_id'),
'region' => array(self::BELONGS_TO, 'AppRegion', 'company_region_id'),
'sector' => array(self::BELONGS_TO, 'AppSector', 'company_sector_id'),
'goodsservices' => array(self::HAS_MANY, 'AppGoodsServices', 'company_id')
);
App.GoodsServices. Relations:
return array(
'company'=>array(self::BELONGS_TO, 'AppCompany', 'company_id'),
);
So, one user adds info about his company (company_name, date_of_registration etc), and than add some goods/services that this company produces. How to COLLECT, UPDATE AND VIEW all this information in one form? Is it possible?
How do I see this:
User addes some info and meets the field: "Goods and services and their percentage" and two fields - "GOODS AND SERVICES" and "PERCENTAGE" and ADD MORE button and he has 6 goods that he wants to add. So, he clicks on ADD MORE button and each time the new fields below appear. But the goods limit is 5, so the button becomes inactive after 5 fields have info.
And after SUBMIT button, all this info records to its table. It can be taken from table on UPDATE button and can be viewd in one CDetailView.
Please, how it can be implemented at least in general terms. Thank you
UPDATE
Ok, some work is done (don't know if it right, but...):
I used this extension: http://www.yiiframework.com/extension/esaverelatedbehavior/
Now I have:
public function actionCreate()
{
$model=new Application;
// Uncomment the following line if AJAX validation is needed
$this->performAjaxValidation($model);
if(isset($_POST['Application']))
{
$model->attributes=$_POST['Application'];
if(isset($_POST['AppGoodsServices']))
{
$model->goodsservices = $_POST['AppGoodsServices'];
}
if ($model->saveWithRelated('goodsservices'))
{
$this->redirect(array('view', 'id' => $model->id));
} else {$model->addError('goodsservices', 'Error occured while saving goods/services.'); }
}
$this->render('create',array(
'model'=>$model,
));
}
public function actionUpdate($id)
{
$model=$this->loadModel($id);
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['Application']))
{
$model->attributes=$_POST['Application'];
if(isset($_POST['AppGoodsServices']))
{
$model->goodsservices = $_POST['AppGoodsServices'];
}
if ($model->saveWithRelated('goodsservices'))
{
$this->redirect(array('view', 'id' => $model->id));
} else {$model->addError('goodsservices', 'Error occured while saving goods/services.'); }
}
$this->render('update',array(
'model'=>$model,
));
}
public function loadModel($id)
{
$model=Application::model()->findByPk($id);
if($model===null)
throw new CHttpException(404,'The requested page does not exist.');
return $model;
}
In my _form.php
<div id="goodsservices">
<?php
$index = 0;
foreach ($model->goodsservices as $id => $goods):
$this->renderPartial('goods/_form', array(
'model' => $goods,
'index' => $id,
));
$index++;
endforeach;
?>
</div>
and subform in goods/_form:
<div style="margin-bottom: 20px; width:100%; clear:left;" class="crow">
<div class="row" style="float: left;">
<?php echo CHtml::activeLabelEx($model, '['.$index.']goods_and_services'); ?>
<?php echo CHtml::activeTextField($model, '['.$index.']goods_and_services', array('size' => 30, 'maxlength' => 150)); ?>
<?php echo CHtml::error($model, '['.$index .']goods_and_services'); ?>
</div>
<div class="row" style="float: left;">
<?php echo CHtml::activeLabelEx($model, '['.$index .']percentage'); ?>
<?php echo CHtml::activeTextField($model, '['.$index.']percentage', array('size' => 5)); ?>
<?php echo CHtml::error($model, '['.$index.']percentage'); ?>
</div>
<div class="row" style="float: left;">
<?php echo CHtml::link('Delete', '#', array('onclick' => 'deleteGoods(this, '.$index.'); return false;'));
?>
</div>
</div>
<div style="clear: both;"></div>
<?php
Yii::app()->clientScript->registerScript('deleteGoods', "
function deleteGoods(elm, index)
{
element=$(elm).parent().parent();
/* animate div */
$(element).animate(
{
opacity: 0.25,
left: '+=50',
height: 'toggle'
}, 500,
function() {
/* remove div */
$(element).remove();
});
}", CClientScript::POS_END);
?>
Everything works, but I cant understand how to make a batch CREATE for children (goodsservices) model. Now it only shows me children in UPDATE form (I've made some records straight into DB) with DELETE button near each. But how to implement CREATE? So, one empty row should be visible and I could ADD other rows on click "Add row"?
It is possible. Using javascript you should create additional items in view, in controller action you are getting $_POST with all data, fill and validate all models in one transaction. In update action view you are showing related models with php foreach.
There is an example of code that can be useful for you. See comments also.

Yii Dependent-dropdown code not working

I am trying to setup a new application using the Yii framework that requires a dependent dropdown, so that when a user selects the jobSkillArea, the options for the next dropdown, jobSkillSpecialty, gets loaded using the built-in jQuery methods. I have copied and modified the code from things I found here and the Yii forums, but I am getting nothing, not even in Chrome's javascript console. Can anyone look at this and see where I've gone wrong? Thanks.
Here is the code in my view for the two dropdowns:
<div class="row">
<?php echo $form->labelEx($model,'jobSkillArea'); ?>
<?php
$list = array();
$list = CHtml::listData(validJobSkillAreas::model()->findAll(), 'JobSkillArea', 'JobSkillArea');
echo $form->dropDownList($model, 'jobSkillArea', $list,
array('prompt'=>'--Select Skill Area--'),
array(
'ajax'=>array(
'type'=>'POST',
'data'=>array('jobSkillArea'=>'js:this.value'),
'url'=>CController::createUrl('NewConsFormController/getSkillSpecialty'),
'update'=>'#'.CHtml::activeId($model,'jobSkillSpecialty')
)
)
);
?>
<?php echo $form->error($model,'jobSkillArea'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'jobSkillSpecialty'); ?>
<?php
$list = array();
$list = CHtml::listData(validJobSkillSpecialties::model()->findAll(),'jobSkillSpecialty','jobSkillSpecialty');
echo $form->dropDownList($model, 'jobSkillSpecialty', array(), array('prompt'=>'--Select Skill Specialty--'));
?>
<?php echo $form->error($model,'jobSkillSpecialty'); ?>
</div>
Then below is the code called by the first dropdown from my controller. The first find is to get the ID that links the parent with the child since I am not storing the KeyValue in the end product. The rest is as it came from the forums.
public function actionGetSkillSpecialty() {
$areaID = ValidJobSkillAreas::model()->find('JobSkillArea=:SkillArea',
array(':SkillArea'=>'$_POST[$jobSkillArea]'));
$data=ValidJobSkillSpecialties::model()->findAll('SkillAreaId=:SkillAreaId',
array(':SkillAreaId'=>$areaID->ID));
$list=array();
$list=CHtml::listData($data,'jobSkillSpecialty','jobSkillSpecialty');
echo "<option value=''>--Select Skill Specialty--</option>";
foreach($list as $value=>$jobSkillSpecialty) {
echo CHtml::tag('option',
array('value'=>$value),CHtml::encode($jobSkillSpecialty),true);
}
}
The view is a partial render within the _form view because that was the only way I could get the accordion widget to work with the fields I have. This is the accordion code that calls the jobDetails section that contains the two dropdown selection boxes.
<div id="accordion">
<?php
$this->widget('zii.widgets.jui.CJuiAccordion', array(
'panels'=>array(
'Job Details'=>$this->renderPartial('_partial_jobdetails',array('model'=>$model,'this'=>$this,'form'=>$form),true,false),
'Consultant Details'=>$this->renderPartial('_partial_consdetails',array('model'=>$model,'this'=>$this,'form'=>$form),true,false),
'Client Details'=>$this->renderPartial('_partial_clientdetails',array('model'=>$model,'this'=>$this,'form'=>$form),true,false),
'Internal Info'=>$this->renderPartial('_partial_internaldetails',array('model'=>$model,'this'=>$this,'form'=>$form),true,false),
'Form Requirements'=>$this->renderPartial('_partial_formsdetails',array('model'=>$model,'this'=>$this,'form'=>$form),true,false),
'JPMC Details'=>$this->renderPartial('_partial_jpmcdetails',array('model'=>$model,'this'=>$this,'form'=>$form),true,false),
),
// additional javascript options for the accordion plugin
'options'=>array(
'collapsible'=>true,
'active'=>false,
'autoHeight'=>false,
'heightStyle'=>'content',
),
'htmlOptions'=>array(
// HTML options you may need
),
));
?>
</div>
Please try the following code
View
<?php
echo CHtml::dropDownList('region_id','',
CHtml::listData($courses, 'course_id', 'course_name'),
array(
'prompt'=>'Select Region',
'ajax' => array(
'type'=>'POST',
'url'=>CController::createUrl('loadcities'),
'update'=>'#city_name',
'data'=>array('region'=>'js:this.value'),
)));
echo CHtml::dropDownList('city_name','', array(), array('prompt'=>'Select City'),
);
?>
=================================================
controller function
public function actionLoadcities()
{
$data=City::model()->findAll('course='.$_POST['region'],
array(':region'=>(int) $_POST['region']));
$data=CHtml::listData($data,'city_id','city_name');
echo "<option value=''>Select City</option>";
foreach($data as $value=>$city_name)
echo CHtml::tag('option', array('value'=>$value),CHtml::encode($city_name),true);
}

Yiiframework Range Slider input

I have a user settings page where users can change the settings of the application. The users can input a number between 0 and 10. See screenshot: http://oi43.tinypic.com/2uerazp.jpg
Now I would like to transform the input fields into a range slider using the CJuiSliderInput widget of the yiiframework: http://www.yiiframework.com/doc/api/1.1/CJuiSliderInput#maxAttribute-detail
I cannot seem to get the code working, this is the current code in view:
<?php foreach($settings as $i=>$value): ?>
<?php $setting=UserSetting::model()->findByPk($value->setting_id); ?>
<h4><?php echo CHtml::encode($setting->label); ?></h3>
<?php echo CHtml::activeTextField($value,"[$i]value"); ?>
<?php endforeach; ?>
I would like to replace the activeTextField with
$this->widget('zii.widgets.jui.CJuiSliderInput',array(
'model'=>$model,
'attribute'=>'[$i]value',
'maxAttribute'=>'timeMax',
// additional javascript options for the slider plugin
'options'=>array(
'range'=>true,
'min'=>0,
'max'=>10,
),
));
What values do I need to fill in to the widget to get it to work? Each textField input is from a different model btw.
The controller looks something like this(don't know if you need it):
$settingsvalues = UserSettingValue::model()->findAll('client_id=:id', array(
':id' => $id,
));
if(isset($_POST['UserSettingValue'])){
$valid = true;
foreach($settingsvalues as $i=>$value){
if(isset($_POST['UserSettingValue'][$i]))
$value->attributes = $_POST['UserSettingValue'][$i];
$value->save();
$valid = $value->validate() && $valid;
}
if($valid)
$value->save();
}
$this->render('settings',array(
'model' => $model,
'settings' => $settingsvalues,
));
Thanks a lot!
I fixed the problem by putting a javascript function in the slider. It is not exactly as I intended in the beginning, but it'll do. The slider now changes the value in the input fields.
<?Php
$this->widget('zii.widgets.jui.CJuiSliderInput',array(
'name' => $i,
'model'=>$value,
'attribute'=>"value",
'event'=>'change',
//'value'=>'$value',
'options'=>array(
'min'=>0,
'max'=>10,
'animate' => true,
'slide'=>'js:function(event,ui){$("#UserSettingValue_'.$i.'_value").val(ui.value);}',
),
)); ?>
This was a little big to be a comment. But please try the following
$this->widget('zii.widgets.jui.CJuiSliderInput',array(
'name'=>'$setting->label',
'attribute'=>'[$i]value',
'value'=>$value,
'options'=>array(
'min'=>0,
'max'=>10,
),
));
Also please try this
$this->widget('zii.widgets.jui.CJuiSliderInput',array(
'name'=>'$setting->label',
'attribute'=>'[$i]value',
'value'=>$value,
'options'=>array(
'min'=>0,
'max'=>10,
),
'htmlOptions'=>array(
'style'=>'height:20px;',
),
));

yii : how to ajax update the cgridview

Q : How to ajax update the CgridVeiw?
Status : I've done with cookie. but there are a problem. it is when the page upload, the date are always show in from-date and to-date text box. and When reload the page, it doesn't clear out.
This is my view
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'page-form',
'enableAjaxValidation'=>true,
)); ?>
<div style="margin-top:30px;">
<b>From :</b>
<?php
$this->widget('zii.widgets.jui.CJuiDatePicker', array(
'name'=>'from_date', // name of post parameter
//'value'=>Yii::app()->request->cookies['from_date']->value, // value comes from cookie after submittion
'options'=>array(
'showAnim'=>'fold',
'dateFormat'=>'yy-mm-dd',
),
'htmlOptions'=>array(
'style'=>'height:20px;'
),
));
?>
<span> </span>
<b>To :</b>
<?php
$this->widget('zii.widgets.jui.CJuiDatePicker', array(
'name'=>'to_date',
//'value'=>Yii::app()->request->cookies['to_date']->value,
'options'=>array(
'showAnim'=>'fold',
'dateFormat'=>'yy-mm-dd',
),
'htmlOptions'=>array(
'style'=>'height:20px;'
),
));
?>
<span> </span>
<?php echo CHtml::submitButton('Go'); ?>
<?php
echo CHtml::ajaxSubmitButton('Search', CHtml::normalizeUrl(array('index')),
array(
'success'=>'js:'
.'function(){'
.'$.fn.yiiGridView.update("acc-payment-recei-grid", {url:"index"});'
.'}',
),
array('id'=>'go', 'name'=>'go'));
?>
<?php $this->endWidget(); ?>
<p style="float:right;">
New Payment Receive
</p>
</div>
<style>
.items table tr:last-child td:first-child {
-moz-border-radius-bottomleft:10px;
-webkit-border-bottom-left-radius:10px;
border-bottom-left-radius:10px
}
.items table tr:last-child td:last-child {
-moz-border-radius-bottomright:10px;
-webkit-border-bottom-right-radius:10px;
border-bottom-right-radius:10px
}
</style>
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'acc-payment-recei-grid',
'dataProvider'=>$accpaymentrecei->search(),
//'filter'=>$accpaymentrecei,
'columns'=>array(
//'id',
array('name' => 'acc_category_id',
'value'=>'(isset($data->acccategories->name)) ? CHtml::encode($data->acccategories->name) :""',
),
array('name' => 'acc_recei_id',
'header'=> 'Account Received',
//'value'=>'(isset($data->method)) ? CHtml::encode($data->method) :""',
),
array(
'name' => 'date',
'value'=>'($data->date= 0) ? "" : date("d M yy",strtotime($data->date))',
),
array('name' => 'method',
'value'=>'(isset($data->method)) ? CHtml::encode($data->method) :""',
),
array('name' => 'description',
//'value'=>'(isset($data->method)) ? CHtml::encode($data->method) :""',
),
/*
'created_date',
'updated_date',
'file_name',
*/
array(
'class'=>'CButtonColumn',
),
),
));
This is controller
public function actionIndex()
{
$accpaymentrecei=new AccPaymentRecei('search');
$accpaymentrecei->unsetAttributes(); // clear any default values
if(isset($_GET['AccPaymentRecei']))
$accpaymentrecei->attributes=$_GET['AccPaymentRecei'];
$acccategory = AccCategory::model()->findAll();
$arr_method = array('Cash'=>'Cash', 'Cheque'=>'Cheque', 'Credit Card'=>'Credit Card', 'Bank Transfer'=>'Bank Transfer');
$this->from_to_date($accpaymentrecei);
//exit();
$this->render('index',array(
'accpaymentrecei'=>$accpaymentrecei,
'acccategory'=>$acccategory,
'arr_method'=>$arr_method,
));
}
protected function from_to_date($model)
{
unset(Yii::app()->request->cookies['from_date']); // first unset cookie for dates
unset(Yii::app()->request->cookies['to_date']);
//$model=new XyzModel('search'); // your model
$model->unsetAttributes(); // clear any default values
if(!empty($_POST))
{
Yii::app()->request->cookies['from_date'] = new CHttpCookie('from_date', $_POST['from_date']); // define cookie for from_date
Yii::app()->request->cookies['to_date'] = new CHttpCookie('to_date', $_POST['to_date']);
$model->from_date = $_POST['from_date'];
$model->to_date = $_POST['to_date'];
}else{
Yii::app()->request->cookies['from_date'] = new CHttpCookie('from_date', date("Y/m/d"));
Yii::app()->request->cookies['to_date'] = new CHttpCookie('to_date', date("Y/m/d"));
}
}
Code below - it's only example, main principles of solution. First principle: we don't need refresh page (by the submit), we need only refresh CGridView after request.
The solution has a few parts:
We need form with some parameters. In your case - fromDate and toDate. You have this one already. But we don't need any submit element. At all! We will use AJAX.
We need some function for initiate AJAX-request. For example, let's event for this request will be change of parameters *from_date* or *to_date*. This function also must update CGridView after request.
We need action in controller for processing AJAX-request.
Let's begin from part 2. It may be some like this:
Yii::app()->clientScript->registerScript('scriptId',
"var ajaxUpdateTimeout;
var ajaxRequest;
$('**selector_for_parameters_fields**').change(function(){
ajaxRequest = $(this).serialize();
clearTimeout(ajaxUpdateTimeout);
ajaxUpdateTimeout = setTimeout(function () {
$.fn.yiiGridView.update(
// this is the id of the CGridView
'acc-payment-recei-grid',
{data: ajaxRequest}
)
},
// this is the delay
300);
});"
);
Place this code into view. Notes:
$(this).serialize() - key moment. Look for more info
selector_for_parameters_fields - give them ( fromDate and toDate ) some unique
selector
We use some delay (300 ms) for decreasing loading from unnecessary AJAX-requests
Now - about action in Controller
public function actionIndex($fromDate='', $toDate='')
{
$criteria = new CDbCriteria();
if(!empty($fromDate))
$criteria->addSearchCondition('your search condition about fromDate');
if(!empty($toDate))
$criteria->addSearchCondition('your search condition about toDate');
$dataProvider=new CActiveDataProvider('YourModel',
array('criteria' => $criteria,));
$this->render('YourView',array('dataProvider'=>$dataProvider,));
}
It must works, but requires some tuning for concrete conditions.

Categories