From the documentation, I have been able to build a Form Select Control, However, It is unclear to me how to add classes to the select options...
Here is my select control in CakePHP Form
<?= $this->Form->select('type', [
'Value 1' => 'Name 1',
'Value 2' => 'Name 2'
],[
'class' => 'js-custom-select w-100 u-select-v2 u-shadow-v19 g-brd-none g-color-black g-color-primary--hover g-bg-white text-left g-rounded-30 g-pl-30 g-py-12',
'data-open-icon' => "fa fa-angle-down",
'data-close-icon' => "fa fa-angle-up"
]
) ?>
Here is my select control in HTML
<select name="type" class="js-custom-select w-100 u-select-v2 u-shadow-v19 g-brd-none g-color-black g-color-primary--hover g-bg-white text-left g-rounded-30 g-pl-30 g-py-12" data-placeholder="Type" data-open-icon="fa fa-angle-down" data-close-icon="fa fa-angle-up">
<option class="g-brd-secondary-light-v2 g-color-black g-color-white--active g-bg-primary--active" value="value 1">Name 1</option>
<option class="g-brd-secondary-light-v2 g-color-black g-color-white--active g-bg-primary--active" value="value 2">Name 2</option>
</select>
How do I get the classes that are on the options into the CakePHP Form of the control?
To add classes to your options, you need to use specific options array structure:
$options = [
[
"text" => "Text to display for option 1",
"value" => "Value to set for option 1",
"class" => "Class list to set for option 1"
],
[
"text" => "Text to display for option 2",
"value" => "Value to set for option 2",
"class" => "Class list to set for option 2"
],
/** ... **/
]
After preparing such array, you can use it with FormHelper::select() or FormHelper::control():
$this->Form->select("field_name",$options);
$this->Form->control("field_name",[
"label" => "My Label",
"type" => "select",
"options" => $options
]);
Related
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'm using Kartik tabs-x and yii2-google-maps-marker.
If the Tab 2 is set active, googleMap shows correctly.
But, If I set the Tab 1 (not 2) to active, googleMap shows incorrectly.
This is Tab-x in view.
<?php
$datsan_tab_items = [
[
'label'=>'<i class="glyphicon glyphicon-calendar"></i>'.Yii::t('app', ' Tab 1'),
'content'=> $this->render('_calendar_dat_san',['ids_sanCon' => $ids_sanCon,
'modelSanCon' => $modelSanCon,
'modelSanChu' => $modelSanChu
]),
'active'=>false
],
[
'label'=>'<i class="glyphicon glyphicon-map-marker"></i>'.Yii::t('app', ' Tab 2'),
'content'=> $this->render('_map',['dc' => $dc, 'modelSanChu' => $modelSanChu]),
'active'=>true,
],
[
'label'=>'<i class="glyphicon glyphicon-usd"></i>'.Yii::t('app', ' Tab 3'),
'content'=> $this->render('_bang_gia',[
'modelSanCon' => $modelSanCon,
'modelSanChu' => $modelSanChu
]),
'active'=>false
],
];
// Tab Chuyen doi giua Login/Register
echo TabsX::widget([
'items'=>$datsan_tab_items,
'position'=>TabsX::POS_ABOVE,
'bordered'=>true,
'encodeLabels'=>false
]);
?>
This is map view.
<div class="datsan-index-thong-tin-ban-do" id="datsan-index-thong-tin-ban-do">
<div class="panel panel-info">
<div class="panel-heading"><?= Yii::t('app', ' Bản Đồ') ?></div> <?php
echo GoogleMaps::widget([
'userLocations' => [
[
'location' => [
'address' => $dc,
'country' => $modelSanChu->dc_quocGia,
],
'htmlContent' => $modelSanChu->ten
],
],
'wrapperHeight' => '350px',
'googleMapsUrlOptions' => [
'key' => Yii::$app->params['GOOGLE_API_KEY'],
]
]);
?>
</div>
</div>
If google maps is inside div that has display:none style, or it's parent, or it's parent's parent (any predecessor) has display:none style, the map view won't initialise correctly. But no worries, there is an easy fix, you just have to trigger resize event on map. So using javascript, AFTER you switch to a desired tab (must be after, so google maps div is already visible!) just call:
google.maps.event.trigger(yii.googleMapManager.map, "resize");
It seems you work with TabX, so probably you know how to best determine when tab was clicked. But in case you don't, maybe give pluginEvents attribute a try. According to docs you should be able to do this:
echo TabsX::widget([
'items'=>$datsan_tab_items,
'position'=>TabsX::POS_ABOVE,
'bordered'=>true,
'encodeLabels'=>false,
'pluginEvents' = [
"tabsX.click" => "function() {
setTimeout(function() {
google.maps.event.trigger(yii.googleMapManager.map, 'resize');
}, 200);
}"
]
]);
Sam updated: Changed timeout from 10 to 200.
I've created an ActiveForm using yii2 like this:
<?=$form->field($item, 'finalPrice', [
'options' => [
'tag' => 'div',
'class' => '',
],
'template' => '<span class="col-md-2 col-lg-2"><label class="control-label">Final item price</label>{input}{error}</span>'
])->textInput([
// ** i want numeric value **
])->label(false)?>
and it rendered a result of:
<span class="col-md-2 col-lg-2"><label class="control-label">Final item price</label><input type="text" id="item-finalprice" class="form-control" name="Item[finalPrice]"><p class="help-block help-block-error"></p></span>
now i want to make it < input type="number" .. and not text.. (so user could change value using browser up/down buttons). is there any way to do it?
You can use ->textInput(['type' => 'number'] eg :
<?=$form->field($item, 'finalPrice', [
'options' => [
'tag' => 'div',
'class' => '',
],
'template' => '<span class="col-md-2 col-lg-2"><label class="control-label">Final item price</label>{input}{error}</span>'
])->textInput([
'type' => 'number'
])->label(false)?>
Try this . it worked for me
<?= $form->field($model, 'amount')->textInput(['type' => 'number']) ?>
Field like Phone Number/Membership No etc, some time we allow user only to enter numeric input in a text field. In such case applying pattern match rule work great for me.
Simply set a rule in the model class and you are done.
public function rules()
{
return [
...
[['contactno'], 'string', 'max' => 25],
[['contactno'], 'match' ,'pattern'=>'/^[0-9]+$/u', 'message'=> 'Contact No can Contain only numeric characters.'],
...
];
}
<?= $form->field($model, 'code')->textInput(['type'=>'number']) ?>
I want to click on a link inside Grid View, which should open a new window in a new tab.
I don't want CHtml::Link answers, since it is YII 1.1, I am using YII 2.0.
THE BELOW CODE IS INSIDE GRID VIEW.
['attribute'=>'EMPLOYEEID',
'label'=>'EMPLOYEEID',
'value'=> Html::a('E_ID', '?r=tb-run-engine/index', ['title' => 'Go']),
],
I didnt get any value for EMPLOYEEID instead am getting [notset] as value in Grid view.and am not getting hyperlink also.Am new to yii 2.0 can any one help me to figure out this problem??
To open link in new tab/window you have to set attribute target="_blank" for this link:
some text
So in Yii2 with Html helper in view file you can write:
<?= Html::a("some text","some_url",['target'=>'_blank']) ?>
And in yii2 grid you can show raw column:
[
'attribute'=>'name', //your model attribute
'format'=>'raw',
'value'=>function ($model, $index, $widget){
return Html::a(
$model->name, //link text
['page/update','id'=>$model->id], //link url to some route
[ // link options
'title'=>'Go!',
'target'=>'_blank'
]
);
}
],
add this to your Html:a options ['target' => '_blank', 'data-pjax' => 0] or turn off pjax in grid
here is my grid view:
<?= GridView::widget([
'dataProvider' => TbRunEngineSearch::$dataprovider_static,
'filterModel' => $searchModel,
'id'=>'searchgrid',
'columns' => [
['class' => 'yii\grid\SerialColumn'],
[
'attribute'=>'run_id',
'label'=>'field level details', //your model attribute
'format'=>'raw',
'value'=>function ($model, $index, $widget){
return Html::a(
$model->run_id, //link text
['page/update','id'=>$model->run_id], //link url to some route
[ // link options
'title'=>'Go!',
'target'=>'_blank'
]
);
}
],
['attribute'=>'run_id',
'value'=>'product_name',
'label'=>'Product Name'],
['attribute'=>'run_id',
'value'=>'module_name',
'label'=>'Module Name'],
['attribute'=>'run_id',
'value'=>'operation_name',
'label'=>'Operation Name'],
['attribute'=>'initiated_at',
'value'=>'initiated_at',
'label'=>'Start Time'],
['attribute'=>'finished_at',
'value'=>'finished_at',
'label'=>'End time'],
['attribute'=>'run_id',
'value'=>'pass_percent',
'label'=>'Pass %'],
['attribute'=>'run_id',
'value'=>'fail_percent',
'label'=>'Fail %'],
['attribute'=>'run_id',
'value'=>'operations_num',
'label'=>'Operations #'],
['attribute'=>'build_num_primary',
'value'=>'build_num_primary',
'label'=>'Build # Pri/Sec'],
'run_status',
'source',
['attribute'=>'env_primary',
'value'=>'env_primary',
'label'=>'ENV # Pri/Sec '],
['attribute'=>'instance_primary',
'value'=>'instance_primary',
'label'=>'INSTANCE # Pri/Sec '],
],
]);
?>