自动完成文本字段 CJui自动完成小部件 yii 存储隐藏字段 ID 并显示名称



我正在寻找自动完成的工作原理,方法是在自动完成的文本字段中显示名称并存储隐藏字段 id 值。

当我检查网络时,我得到了名称和ID。但能够显示名称,但它没有为记录选择 ID,因此无法存储 ID

任何人都可以给我任何用于自动完成的链接/代码。 是否有任何链接/代码正在工作..

自动完成类

:::
class EAutoCompleteAction extends CAction{
public $model;
public $attribute;
public $id;
private $results = array();
public $returnVal = '';
public function run()
{
if(isset($this->model) && isset($this->attribute)) {
$criteria = new CDbCriteria();
$criteria->compare($this->attribute, $_GET['term'], true);
$model = new $this->model;
foreach($model->findAll($criteria) as $m)
{
$this->results[] = $m->{$this->attribute};
$this->results[] = $m->{$this->id};
//$this->results[] = array(
//      'name' => $m->{$this->attribute},
//      'id'=> $m->id
//);

}
}
echo CJSON::encode($this->results);
}
}

我正在使用这样的控制器/操作::

public function actions()
{
return array(
'aclist'=>array(
'class'=>'application.extensions.EAutoCompleteAction',
'model'=>'Organisation', //My model's class name
'attribute'=>'name', //The attribute of the model i will search

)

}

在我看来.php。

<div class="row">
<?php echo $form->labelEx($model,'organsiation'); ?>
<?php echo $form->hiddenField($model,'organisation_id',array()); ?>
<?php
$this->widget('zii.widgets.jui.CJuiAutoComplete', array(
'attribute'=>'organisation_id',
'model'=>$model,
'sourceUrl'=>array('benefit/aclist'),
'value'=>'Please select',
'name'=>'name',
'id'=>'organisation_id',
'options'=>array(
'minLength'=>'2',
'select'=>"js:function(event, ui) {
alert(ui.item.id);
$('#organisation_id').val(ui.item.id);
}",
),
'htmlOptions'=>array(
'size'=>45,
'maxlength'=>45,
),
)); ?>
<?php echo $form->error($model,'organisation_id'); ?>

这是我的自动完成代码

  1. 创建返回 json

    的操作
    public function listaItemMarcaAction(){
    $cmd = Yii::app()->db->createCommand();
    $cmd->select('id, nombre as value, nombre as label, pais_origen');
    $cmd->from('item_marca');
    $cmd->where('nombre like :term', array(':term'=>'%'.request()->getParam('term').'%'));
    $data = $cmd->queryAll();
    header('Content-type: application/json');
    echo CJavaScript::jsonEncode($data);
    Yii::app()->end();
    }
    
  2. 创建自动完成字段和隐藏字段(在视图文件中,示例_form.php)

    <?php echo $form->labelEx($model,'marca_id'); ?>
    <?php echo $form->hiddenField($model,'marca_id'); ?>
    <?php $this->widget('zii.widgets.jui.CJuiAutoComplete', array(
    'name'=>"Item[marca]",
    'value'=>$model->isNewRecord ? null : $model->marca->nombre,
    'sourceUrl'=>Yii::app()->createUrl('/item/listaitemmarca'),
    'options'=>array(
    'minLength'=>1,
    'change'=>'js:function(event,ui){fn_item_data(event,ui)}'
    ),
    )); ?>
    
  3. 创建一个 JavaScript 函数,通过自动完成来设置值检索。注意:只使用更改事件,不再需要。在示例中,活动记录是"项目",然后,输入的 id 将被Item_marca_id并Item_marca。

    function fn_item_data(event,ui){
    if(!ui.item){
    $("#Item_marca_id").val("");
    $("#Item_marca").val("");
    }else{
    $("#Item_marca_id").val(ui.item.id);
    $("#Item_marca").val(ui.item.value);
    //and use ui.item.pais_origen for another things
    if(ui.item.pais_origen == 'EEUU') alert('ok');
    }
    }
    

在您的情况下,我会从普通的 JQuery 开始,而不是客户端的 CJuiAutoComplete。JQuery UI文档有一个很好的演示,其中包含 http://jqueryui.com/autocomplete/#custom-data 的工作源代码。有三个基本步骤可以使事情正常工作:

  1. 以 JSON 对象数组的形式提供数据,而不是字符串。

  2. 使用自定义_renderItem函数将 JSON 对象呈现为可读字符串。

  3. 使用自定义select函数将所选JSON对象的可见name记录到文本字段中,并将该对象的id记录到隐藏字段中。

要执行步骤 1,您需要取消注释掉 EAutoCompleteAction 的一部分,并删除其上方的两行。之后,您应该能够在alert()消息中看到项目 ID 和名称。

步骤2(覆盖_renderItem)对于CJuiAutoComplete来说特别棘手,这就是我建议使用普通JQuery UI的原因。有关使用纯 JQuery UI 的示例,请参阅上面的链接。CJuiAutoComplete 的示例可以在 Yii 文档的注释部分找到: http://www.yiiframework.com/doc/api/1.1/CJuiAutoComplete#c8376。在步骤 2 之后,您应该能够看到可读的自动完成建议。

要进行第 3 步,您需要添加类似

$('#organisation_name').val( ui.item.name );

到您的select函数中,前提是organisation_name是文本字段的 ID,organisation_id是隐藏表单字段的 ID(您需要进行一些更改才能做到这一点)。

相关内容

最新更新