How to open a new window in YII framework 2.0 - yii2

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 '],
],
]);
?>

Related

'target' => '_blank' doesn't work. Rendering click in another tab

I have following code presenting picture from database(Blob) in GridView::widget. On click at picture, another controller method will be rendered, but in the same tab. I want to be rendered this method in another tab.
'target' => '_blank'
will not work with this code. How can I achieve this?
[
'attribute' => 'photo',
'label' => 'Photo',
'format' => 'raw', // hier nicht html,da das Bild sonst nicht angezeigt wird
'vAlign' => 'middle',
'value' => function($model) {
$url = 'data:image/jpeg;base64,' . base64_encode($model->photo);
return Html::a(
Html::img($url, ['alt' => 'Kein Photo gefunden', 'class' => 'img-circle', 'style' => 'width:50px;height:50px']), [Url::to('ds-substanz/vergroessern'), 'id' => $model->id], ['title' => 'Photo vergrößern', 'target' => '_blank']
);
}
],
Check if your gridview is not enclosed in Pjax. In this case you should use linkSelector in Pjax for other required links.
Otherwise, it is better to use JavaScript and jQuery or ...
The browser may ignore it. They have settings for such issues, of course your syntax is correct.
You can do the following:
Html::a('IMG.....', ['ds-substanz/vergroessern', 'id' => $model->id], ['onclick'=>"window.open(this.href,'_blank');return false;",'target' => '_blank']);
Good luck.

Update form after ajax save

I want form data updated after ajax save. Cause if item was new (id - empty), it tries to create new one each time. Also there are back-end generated fields which are appears after save.
<?php $form = ActiveForm::begin([
'method' => 'post',
'action' => ['category/save', 'id' => $category ? $category->id : ''],
'enableClientValidation' => true,
// 'enableAjaxValidation' => false,
'validateOnChange' => false,
'validateOnBlur' => false,
'validateOnSubmit' => true,
'options' => [
'id' => 'customer-update',
'class' => 'ajax-submit',
],
'fieldConfig' => [
'template' => '<div class="row-content-col1">{label}</div><div class="row-content-col2">{input}{error}</div>'
]
]); ?>
.......
<?php echo $form->field($category, 'url')->textInput(['class' => 'ip', 'readonly' => true]); ?>
......
<?php $form->end(); ?>
Form field produce such html:
<div class="row-content-col1"><label class="control-label" for="category-url">Url</label></div><div class="row-content-col2"><input type="text" id="category-url" class="ip" name="Category[url]" readonly><div class="help-block"></div></div>
</div>
And than on controller i return this (tried different variations):
{"error":false,"message":"Category 'asdfzsdf sdf' saved","data":{"name":"asdfzsdf sdf","url":"asdfzsdf-sdf","project_id":1,"id":21}}
What is valid response for ajax form? Or is there other way to handle this all ?
Pjax is really useful for your challenge, Just add your form inside of Pjax widget. add form action to new path(such: site/control-data).
In your action method do what you want, but send response like that :
return $this->renderAjax('form path',$model);
It's the general of what you must do.
But maybe you have problem with jquery or pjax or need some more data, but all questions have an answer,
See Pjax for ActiveForm

GoogleMaps does not show on inactive tab (Yii2)

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.

Getting label with colon for model's field in a form

I need to have Body: (with colon at the end), not Body rendered as label for each field in my form. How can I achieve this the best way?
I tried modifying fieldConfig => template in ActiveForm::begin by adding div class=\"\">{label}:</div> into it:
<?php $form = ActiveForm::begin([
'id' => 'edit-form',
'options' => ['class' => 'form-horizontal'],
'fieldConfig' => [
'template' => "<div class=\"\">{label}:</div>\n<div class=\"\">{input}</div>\n<div class=\"\">{error}</div>",
'labelOptions' => ['class' => 'edit-label'],
]]); ?>
but it is wrong. Colon is rendered as separate DOM element, with incorrect styling and looks ugly.
I tried doing this awfully in CSS:
.edit-label::after {
content: ":";
}
but this is even worse.
I remember, that I made a lot of stupid things in Yii1 to get this. I don't want to repeat these stupid things, when implementing this in Yii2. What is the best way of achieving this?
When using Bootstrap 3 (yii\bootstrap\ActiveField) you can use additional placeholders in the $template and you need to replace {label} with {beginLabel}{labelTitle}:{endLabel}:
<?php $form = ActiveForm::begin([
'id' => 'edit-form',
'options' => [
'class' => 'form-horizontal',
'enctype'=>'multipart/form-data'
],
'fieldConfig' => [
'template' => "<div class=\"\">{beginLabel}{labelTitle}:{endLabel}</div>\n<div class=\"\">{input}</div>\n<div class=\"\">{error}</div>",
'labelOptions' => ['class' => 'edit-label'],
],
]); ?>
I don't know, how to solve this problem, if you're using basic yii\widgets\ActiveField instead.

Yii2: Method Not Allowed (#405) while logout user

I am loging out user through following code. This is my view code behind logout button:
<li>
<a href="<?= Url::to(['site/logout'])?>">
<i class="fa fa-sign-out"></i> Log out
</a>
</li>
My controller code is:
public function actionLogout()
{
Yii::$app->user->logout();
$model = new LoginForm();
$this->layout = 'index';
return $this->render('login', ['model' => $model]);
}
In the logout it shows me:
Method Not Allowed. This url can only handle the following request
methods: POST.
What is it?
Seems like you have VerbFilter attached to logout action in your SiteController:
/**
* #inheritdoc
*/
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'logout' => ['post'],
],
],
];
}
That means this action can requested only with POST method, and you are requesting with GET, that's why exception #405 is thrown.
Either remove this from VerbFilter or add data-method attribute to request with POST:
...
Update: Another reason of this problem can be missing dependency for yii\web\YiiAsset. Make sure it's included in AppAsset:
public $depends = [
'yii\web\YiiAsset',
...
];
YiiAsset provides data-method attribute feature which gives possibility to link act as a form with action post by writing less code. Without asset obviously link will act as regular link and standard GET request will be sent.
You can also use a custom template
'items' => [
[
'label' => 'Logout',
'url' => ['/user/security/logout'],
'template' => '{label}',
],
]
u can change the view code and echo instead of
<li>
<a href="<?= Url::to(['site/logout'])?>">
<i class="fa fa-sign-out"></i> Log out
</a>
</li>
this one:
<?= Html::a('<i class="fa fa-sign-out"></i>',
['/site/logout'],
['class'=>'btn btn-default btn-flat']), //optional* -if you need to add style
['data' => ['method' => 'post',]])
?>
You must only replace 'logout' => ['post'], with 'logout' => ['get']. In this way your error will be solved.
This way works only with Yii Framework version 2.
See more at: http://tutorials.scrisoft.com/solve-this-error-method-not-allowed-this-url-can-only-handle-the-following-request-methods-post/#sthash.fQmwYPJH.dpuf
If you are using Nav::widget to generate menus, the logout item should have linkOptions specified:
[
'label' => '<i class="fa fa-sign-out"></i>Logout',
'url' => ['/logout'],
'linkOptions' => ['data-method' => 'post'],
],
Following works too assuming you might extra class and data-method attribute.
<?=
Html::a(
'Logout (' . Yii::$app->user->identity->username . ')',
['/site/logout'],
['class' => 'ui inverted button', 'data-method' => 'post']
);
?>
this code is working for AdminLTE template.
['label' => 'Sign out (' . Yii::$app->user->identity->username . ')','url' => ['/site/logout'],'template' => '{label}',],