Yii2: Jui Auto Complete Widget how to? - php

I am trying to use yii2 Jui autocomplete widget.
I have this code which is showing the auto-complete date correctly, but I am not able to save the data.
$data=ArrayHelper::map(State::find()->all(), 'id', 'state_name' );
$data=array_merge($data);
And then
echo 'State' .'<br>';
echo AutoComplete::widget([
'model'=>$model,
'attribute' => 'state_id',
'clientOptions' => [
'source' => $data,
],
]);
Any solution will be greatly appreciated.
Thanks.

Ok I found the solution, it goes like this:
use yii\jui\AutoComplete;
use yii\web\JsExpression;
Then:
$data = State::find()
->select(['state_name as value', 'state_name as label','id as id'])
->asArray()
->all();
Then
echo 'State' .'<br>';
echo AutoComplete::widget([
'name' => 'State',
'id' => 'ddd',
'clientOptions' => [
'source' => $data,
'autoFill'=>true,
'select' => new JsExpression("function( event, ui ) {
$('#city-state_name').val(ui.item.id);//#City-state_name is the id of hiddenInput.
}")],
]);
and Finally:
<?= Html::activeHiddenInput($model, 'state_name')?>
That is all.
Hope some one will find it useful.
Thanks.

Related

How to perform Filter using Multi Select Dropdown in Yii2 GridView

Here i like to explain my problem clearly,
am trying to perform multi select dropdown filter, before this multiselect filter i have a basic filter.
Am using kartik-v dropdown extension
search.php
<?php
$status = ArrayHelper::map(Status::find()->all(),'id','status');
echo $form->field($model, 'status')->widget(Select2::classname(), [
'data' => $status,
'language' => 'en',
'options' => [
'placeholder' => 'Select Status..',
'multiple' => true
],
'pluginOptions' => [
'allowClear' => true
],
]);
?>
claimsSearch.php
$query->andFilterWhere([
'status' => $this->status
]);
if i try the above code am getting error as below
Array to string conversion
but here i don't know how to write filter code.
update searchview:
Try to delete 'status' from EmployeeSearch rules.
You cannot filter such field automatic way.
Or you must set up custom filter value for status column, like this (you can dig into this direction):
How can I use a simple Dropdown list in the search box of GridView::widget, Yii2? Try this link
You are not calling the model in that widget. You shoudl use like this:
echo $form->field($mySearchModel, 'state_10')->widget(Select2::classname(), [
'data' => $status,
'options' => [
'placeholder' => 'Select Status ...',
'multiple' => true
],
]);
And your select it's probably returning an array. So, your search would be something like:
$query->andFilterWhere([
'status' => ('in', 'status', $this->status)
]);
See more examples of queries here.
If that solution don't work, i will sugest you to do a var_dump($yourModel->status) in your view, just to check what is returning.
$this->status is array?
So, you can use
<?php
$status = ArrayHelper::map(Status::::model()->findAllByAttributes(array("id"=>$status));(),'id','status');
echo $form->field($model, 'status')->widget(Select2::classname(), [
'data' => $status,
'language' => 'en',
'options' => [
'placeholder' => 'Select Status..',
'multiple' => true
],
'pluginOptions' => [
'allowClear' => true
],
]);
?>

Yii2 auto complete - value missing in the field on update

I am using the yii2 auto complete widget, it is working fine, except when I update the form the field shows blank.
use yii\jui\AutoComplete;
use yii\web\JsExpression;
$data = app\models\Doctor::find()
->select(['doctor_name as value', 'doctor_name as label','id as id'])
->asArray()
->all();
and after that the auto-complete code like this
echo 'Doctor' .'<br>';
echo AutoComplete::widget([
'name' => 'Doctor',
'id' => 'ddd',
'clientOptions' => [
'source' => $data,
'autoFill'=>true,
'select' => new JsExpression("function( event, ui ) {
$('#appoinment-doctor_name').val(ui.item.id);
}")],
]);
<?= Html::activeHiddenInput($model, 'doctor_name')?>
What I am missing here? How can I get the value on update?
You have to explicitly set value:
echo AutoComplete::widget([
'name' => 'Doctor',
'id' => 'ddd',
'value' => $model->doctor_name,
...
This isn't necessary if you set model and attribute though.

How i do custom Yii2 gridview sort?

How can I sort with a customized gridview header?
Please give the difference between label and header in the Yii2 gridview widget dataprovider.
Here is my code:
<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
[
'class' => 'yii\grid\DataColumn',
'value' => function ($data) {
return $data->myTitle;
},
'headerOptions' => ['style'=>'text-align:center'],
'header' => 'Page Title',
'label' => 'Title'
],
]); ?>
Do header and label perform the same function?
How can I perform sorting in $data->myTitle?
Here my Output Screen:
I want Page Title, Status, Date Modified should be active.
Thanks in advance.
Found the answer.
Please add attributes to ActiveDataProvider in your Search Model.
$dataProvider = new ActiveDataProvider([
'query' => $query,
'pagination' => [
'pageSize' => 5,
],
'sort' => ['attributes' => ['myTitle']],
]);
Add the attribute option in widget:
<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
[
'class' => 'yii\grid\DataColumn',
'value' => function ($data) {
return $data->myTitle;
},
'headerOptions' => ['style'=>'text-align:center'],
'attribute' => 'myTitle',
'label' => 'Page Title'
],
]); ?>
Since myTitle is a field from the database and not a custom value, you can just use attribute. The rest may be unnecessary e.g the default class is DataColumn
'columns' => [
[
'attribute' => 'myTitle',
'label' => 'Label',
]
I am not very sure I understand your question, but sorting option can be included in your modelsearch.php. So in your case you have to do like this.
$dataProvider = new ActiveDataProvider([
'query' => $query,
'sort'=> ['defaultOrder' => ['your_column'=>SORT_ASC]]
]);
if myTitle is a field in the database, why you are using such a long syntax. Just
'Columns'=>[
..
'myTitle',
..
],
should work fine and should be active for sorting as you want
if you want a different header/label for the column, use label instead of header as header is only a cell content and cannot be used for sorting, while label can. details
[
..
'attribute'=>'myTitle',
'label' => 'Page Title'
..
],

Yii2 Autocomplete : Save the ID instead of value

I have a list of array called as datalist which contains the name of companies with id.when i use it with the typeahead widget,it captures the value of company with the variable coSearch(id of input).but i want to display the list of companies and when selected,it must give the vale of that id in variable .i am really messed with this problem working from three days.Please help me out.
Here is the code for my activeform which contains the widget.
<?php
$form = ActiveForm::begin([
'action' => ['search'],
'method' => 'get',
]);
$dataList=ArrayHelper::map($companies, 'id', 'name');
echo Typeahead::widget([
'model' => $companySearched,
'name'=>'coSearch',
'options' => ['placeholder' => 'Search company','id'=>'searchCompany1','class' => 'form- control','value'=>'1'],
'pluginOptions' => ['highlight'=>true],
'dataset' => [
[
'local' => $dataList,
'limit' => 10,
]
]
]);
?>
This can be solved with the help of a hidden field.I use autocomplete here
<?php
use yii\web\JsExpression;
use yii\jui\AutoComplete;
$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 ) {
$('#model-company').val(ui.item.id);
}")],
]);
?>
<?= Html::activeHiddenInput($model, 'company')?>
Hope this help!

Autocomplete in yii2

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.
<?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?
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')?>
Autocomplete just helps you fill the field with required value.
If you need to submit c_id look to dropdownList or Select2 plugin.
Check this http://demos.krajee.com/widget-details/select2 yii2 widget for ideas.
Here example code:
<?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
I wanted to use the Jui Autocomplete so that when I click or focus on autocomplete textbox, it should display options.
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);

Categories