how to create custom autocomplete textfield in yii(如何在 yii 中创建自定义自动完成文本字段)
问题描述
我是 yii 的新手.我需要编写自定义 yii 自动完成.我知道 CJuiAutocomplete 在那里.但我需要实现自己的自定义自动完成.任何人都可以指导我或帮助我开发自定义自动完成文本字段.在文本字段中显示名称时获取 id.
提前致谢
这是站点控制器中的一个动作...
公共函数 actionAutoComplete($term){$query = Yourmodel::model()->findallbyattributes(array('somecolumn'=>$term));$list = 数组();foreach($query as $q){$data['value']= $q['id'];$data['label']= $q['name'];$list[]= $data;未设置($数据);}回声 json_encode($list);}这是您认为的搜索表单:
$form=$this->beginWidget('CActiveForm', array('id'='=>'搜索表单','enableAjaxValidation'=>false,'动作' =>'/'));?><字段集><div class="input-append"><?phpecho CHtml::hiddenField('selectedvalue','');$this->widget('zii.widgets.jui.CJuiAutoComplete', array('名称'=>'搜索框','值'=>'','source'=>CController::createUrl('/site/autoComplete'),'选项'=>数组('showAnim'='折叠','minLength'='2','选择'=>'js:函数(事件,用户界面){$("#searchbox").val( ui.item.label );$("#selectedvalue").val( ui.item.value );返回假;}',),'htmlOptions'=>数组('焦点' =>'js: this.value = null;$("#searchbox").val(null);$("#selectedvalue").val(null);','类' =>'input-xxlarge 搜索查询','占位符' =>搜索...",),));echo '<button class="btn" type="submit">Submit</button>';?></fieldset><?php $this->endWidget();?></表单>
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>
这篇关于如何在 yii 中创建自定义自动完成文本字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 yii 中创建自定义自动完成文本字段
基础教程推荐
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- 如何替换eregi() 2022-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
