php yii2 中的自动完成
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23215869/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Autocomplete in yii2
提问by Dency G B
In Yii2 I want one of my input field to be autocomplete when user starts to type.Below is my code which uses Jui Autocomplete
.
在 Yii2 中,我希望我的输入字段之一在用户开始键入时自动完成。下面是我的代码,它使用Jui Autocomplete
.
<?php
$items= ArrayHelper::map(Company::find()->all(), 'c_id', 'name');
echo AutoComplete::widget([
'model' => $model,
'attribute' => 'company',
'clientOptions' => [
'source' => $items,
],
]);?>
This is not working.When i printed my array, i got like
这不起作用。当我打印我的数组时,我得到了
Array ( [1] => abc [2] => xyz [4] => pqr )
I got it working when i manually set like
当我手动设置时,我得到了它的工作
$items=['abc','xyz','pqr'];
The reason may be my c_id's
are not ordered?But i want to get the c_id
value to be submitted!Any idea how to fix this?
原因可能是我c_id's
没有订购?但我想获得c_id
要提交的价值!知道如何解决这个问题吗?
回答by Dency G B
This can be solved with the help of a hidden field input.Hope this will help somebody!
这可以在隐藏字段输入的帮助下解决。希望这会对某人有所帮助!
<?php
use yii\web\JsExpression;
$data = Company::find()
->select(['name as value', 'name as label','c_id as id'])
->asArray()
->all();
echo AutoComplete::widget([
'name' => 'Company',
'id' => 'ddd',
'clientOptions' => [
'source' => $data,
'autoFill'=>true,
'minLength'=>'4',
'select' => new JsExpression("function( event, ui ) {
$('#user-company').val(ui.item.id);
}")
],
]);
?>
<?= Html::activeHiddenInput($model, 'company')?>
回答by user1852788
Autocomplete just helps you fill the field with required value. If you need to submit c_id look to dropdownList or Select2 plugin.
自动完成只是帮助您用所需的值填充字段。如果您需要将 c_id 提交给 dropdownList 或 Select2 插件。
Check this http://demos.krajee.com/widget-details/select2yii2 widget for ideas. Here example code:
检查这个http://demos.krajee.com/widget-details/select2yii2 小部件的想法。这里示例代码:
<?php
use kartik\widgets\Select2;
use app\models\Modelname;
$model = new Modelname;
$data = ['qwe1'=>'color1','key2'=>'color3']
?>
<?= Html::beginForm() ?>
<?= Select2::widget([
'model' => $model,
'attribute' => 'color',
'data' => array_merge(["" => ""], $data),
'options' => ['placeholder' => 'Select a state ...'],
'pluginOptions' => [
'allowClear' => true
],
]); ?>
<?= Html::submitButton('Submit', ['class' => 'btn btn-primary']) ?>
<?= Html::endForm() ?>
It also supports ajax loaded data: http://demos.krajee.com/widget-details/select2#ajax
它还支持ajax加载数据:http: //demos.krajee.com/widget-details/select2#ajax
回答by Sudeep Talati
I wanted to use the Jui Autocomplete so that when I click or focus on autocomplete textbox, it should display options.
我想使用 Jui 自动完成,这样当我点击或关注自动完成文本框时,它应该显示选项。
I wrote following code and it seems to be working
我写了下面的代码,它似乎工作
$floorOptionsArray = ['Basement', 'Ground Floor', 'First floor', 'Second floor', 'Third floor'];
// $floorOptionsArray = array_combine($floorOptionsArray, $floorOptionsArray);
$model = new Customer();
echo $form->field($model, 'floor')
->widget(\yii\jui\AutoComplete::classname(), [
'value' => (!empty($model->floor) ? $model->floor : ''),
'clientOptions' => [
'source' => $floorOptionsArray,
'enabled' => true,
'minLength' => 0
],
'options' =>
[
'placeholder' => 'Floor',
'class' => 'form-control autocomplete-input-bg-arrow ',
'onclick' => "(function ( ) {
$( '#customer-floor' ).autocomplete( 'search', '' );
})();",
'onfocus' => "(function ( ) {
$( '#customer-floor' ).autocomplete( 'search', '' );
})();",
],
])->label(true);