How I can add an extra attribute like in below screenshot state_id=1 in options list for all.
<?= $form->field($model, 'district_id')->dropDownList(ArrayHelper::map($Districts, 'id', 'name')) ?>
You need to iterate through the $Districts array and associate all the attributes you want to add to the <option> of the dropdown, i assume that your $Districts array has something like below
$Districts=[
1=>"North Andaman",
2=>"South Andaman"
3=>"Nicobar"
];
Now you need to iterate that array and associate the attributes with every option
foreach ($Districts as $id => $name) {
$optionAttributes[$id] = ['my-attr' => 'value'];
}
The above will show you something like
Array
(
[1] => Array
(
[my-attr] => value
)
[2] => Array
(
[my-attr] => value
)
[3] => Array
(
[my-attr] => value
)
)
Now when creating your dropdown you should pass this array to the options option of the dropdownList() see below
echo $form->field($model, 'district_id')->dropDownList(
$Districts,
['options' => $optionAttributes]
);
Now if you see the source of the page it will show you the dropdown like below
<select id="contacts-district_id" name="Contacts[district_id]" class="form-control">
<option value="1" my-attr="value">North Andaman</option>
<option value="2" my-attr="value">South Andaman</option>
<option value="3" my-attr="value">Nicobar</option>
</select>
Related
I have not worked with the listview widget before so I cant find a solution for this, as I output a list with items I wanted to do 2 things, 1. auto increment and unique ID/number to each list item, 2. add a block(custom piece of code) after each 3rd list item.
I could not find any documentation about this so not sure if this is possible.
echo ListView::widget([
'id' => 'listofitems',
'dataProvider' => $dataProvider
]);
<div id="listofitems">
<div class="list_item_wrapper">
// my items which are in a seperate file
<div class="list_item_wrapper">
// when using the $index to check for a certain number the code will be build here.
</div>
</div>
// the needed solution
if($index == 12 || $index == 12){
echo 'some div here';
}
</div>
You can use the itemView option for the ListView widget where you can either provide
Specify as a callback function ($model , $key , $index , $widget) { and add your custom HTML inside and do your custom operations like check every third item,or use the actual id by calling $model->id and appending it with the html tag attribute, it provides you
$model: mixed, the data model
$key: mixed, the key value associated with the data item
$index: integer, the zero-based index of the data item in the items array returned by $dataProvider.
$widget: ListView, this widget instance
For example
echo ListView::widget([
'id' => 'listofitems',
'dataProvider' => $dataProvider,
'itemView'=>function ($model , $key , $index , $widget) {
//Do your Thing with Html you want to draw
//return '<div></div>';
}
]);
Or provide the view file path to the option, you can still use the above given params in the view file
For Example
echo ListView::widget([
'id' => 'listofitems',
'dataProvider' => $dataProvider,
'itemView'=>'_view-name'
]);
Your view can look like
<?php
use yii\helpers\Html;
?>
<div class="card">
<div class="header">
<h3 class="title"><?= Html::encode ( $model->title ) ?></h3>
</div>
<div class="body"><img src="<?= Html::encode ( $model->name ) ?>"><?= Html::encode ( $model->id ) ?></div>
<div class="footer"></div>
</div>
UPDATE
If your requirements are to draw or add an element after every item or any number of items you can use the afterItem option which takes an anonymous function that is called once AFTER rendering each data model, it passes same set of parameters as beforeItem
$model: the current data model being rendered
$key: the key value associated with the current data model
$index: the zero-based index of the data model in the model array returned by $dataProvider
$widget: the ListView object
UPDATE2
The below should work in your case with the given HTML
<div id="listofitems">
<?php
echo ListView::widget ( [
'id' => 'listofitems' ,
'dataProvider' => $dataProvider ,
'afterItem=' > function($model , $key , $index , $widget) {
// the needed solution
if ( $index == 12 || $index == 12 ) {
return 'some div here';
}
} ,
'itemView' => function ($model , $key , $index , $widget) {
//Do your Thing with Html you want to draw
return '<div class="list_item_wrapper">
// my items which are in a seperate file
<div class="list_item_wrapper">
// when using the $index to check for a certain number the code will be build here.
</div>
</div>
';
}
] );
?>
</div>
There is such an index by default in the widget. Configure the 'itemView' attribute of the ListView widget, that means you can use a custom view which will be rendered for each item. Like this:
<?= ListView::widget([
'id' => 'listofitems',
'dataProvider' => $dataProvider,
'itemView' => '/site/item',
?>
in your view file /site/item.php, you can access the index of the current item:
<?php
var_dump($index);
?>
More info about the itemView property here.
i have passed some values($list) fetched from database to yii2 default checkbox list and it is successfully showing the result
<?php $list=ArrayHelper::map(Questions::find()->all(),'id','question'); ?>
<?= $form->field($model, 'dept_id')->checkboxList($list); ?>
how can i pass the same $list with kartik checkboxlist,shown below
<?= $form->field($model, 'dept_id')->widget(CheckboxX::classname($list), [
'initInputType' => CheckboxX::INPUT_CHECKBOX,
'autoLabel' => true
])->label(false); ?>
This widget allows three checkbox states [1, 0 and null]
You could use Yii2: ActiveField CheckboxList Instead. This way you can specify *n options to in form of checkboxes.
could someone tell me how to create a selected value in dropdown list?
Here is my dropdown list:
<?= Html::dropDownList(
'calculation-type',
$calculateByMonths,
$calculationTypeList, [
'options' => [
Employee::DISABLED =>[
'disabled' => true,
'selection' => true
]
],
'id' => 'calculation-type',
]); ?>
That line selection => true doesn't work, I don't know why :( Thanks for the help.
As you can see in official Yii2 documentation second argument in Html::dropDownList is $selection, and it has to contain string or array of selected values.
Values in dropDownList are keys in $items array. For example, if you have an array of months and you need to make February selected:
<?php
$month = [
'jan' => 'January',
'feb' => 'February',
'mar' => 'March',
'apr' => 'April',
];
echo \yii\helpers\Html::dropDownList(
'months', //name of your select tag
'feb', // selected option value
$month // array of items
);
?>
<!-- Output of the dropDownList-->
<select name="months">
<option value="jan">January</option>
<option value="feb" selected>February</option>
<option value="mar">March</option>
<option value="apr">April</option>
</select>
Im displaying a form as a representation of data in my database. Im using a form because the data is editable by certain users.
The form contains a dropdown list:
<select name="Status" id="Status">
<option id="confirmed" value="Confirmed">Confirmed</option>
<option id="completed" value="Completed">Completed</option>
<option id="released" value="Released">Released</option>
<option id="deleted" value="Deleted">Deleted</option>
</select>
When loading the view I pass in a variable '$status' which is from the database. How do I set the 'selected' attribute for which ever option $status is equal to?
Im using blade tempting engine
if you're using blade template you may want to use the Form helper to build your form fields, pass an array of items to be listed as dropdown options on the 2nd parameter and at the 3rd parameter pass your selected value, see the code below for reference
From controller:
$selectedValue = "confirmed";
$statuses = array(
array("id"=> "confirmed", "value" => "Confirmed"),
array("id"=> "completed", "value" => "Completed"),
array("id"=> "released", "value" => "Released"),
array("id"=> "deleted", "value" => "Deleted"),
);
return view("yourview")->with("statuses", $statuses)->with("selectedValue", $selectedValue);
On your view you could do something like below:
{!! Form::select('status', $statuses , $selectedValue, ['class' => 'form-control', 'id' => 'status']) !!}
Note: the code is untested but should work in your case.
Also please check https://laravelcollective.com/docs/5.2/html
i am doing a select in html then I have something in the JS part like this
var languageEN = "en,1";
var languageFR = "fr,2";
var languageDE = "de,3";
and my html markup will be like
<select style="display: none;" id="select">
<option value=languageEN>English</option>
<option value=languageFR>Francais</option>
<option value=languageDE>Deutsch</option>
</select>
but it seems that I am hitting undefined.
I been trying to find whats the correct syntax to include in the option value but no avail. thanks.
If using PHP is an option you could do this. The first part could go at the top of the page, then the second part wherever the select needed to be. You could add as many options or change them any way you liked this way.
<?php
$options = array(
0 => array(
'name' => 'English',
'value' => 'languageEN'
),
1 => array(
'name' => 'Francais',
'value' => 'languageFR'
),
2 => array(
'name' => 'Deutsch',
'value' => 'languageDE'
),
);?>
<select style="display: none;" id="select">
<?php
foreach ($options as $option);
{
echo '<option value="'.$option['value'].'">".$option['name'].'</option>'
}
?>
</select>