Yii2: window.open from PHP - javascript

I have a GridView of a index view with some columns. I added a print button that link to a URL that has to be opened in a new window.
This code work but the URL is not open in a new window.
'columns' => [
'column1',
'column2',
'column3',
'column4',
'column5',
[
'class' => 'yii\grid\ActionColumn',
'template' => '{download} {update} {delete}',
'buttons' => [
'download' => function ($url, $model) {
return Html::a(
'<span class="fa fa-print"></span>',
'/disposicion-licencia/print-estival?id=' . $model->id,
[
'title' => 'Download',
'data-pjax' => '0',
]
);
},
],
],
]
I think I need JavaScript code like this:
window.open('/disposicion-licencia/print-estival?id=$id');
But I don't know where to use it.

For a new window you need 'target' => '_blank':
return Html::a(
'<span class="fa fa-print" ></span>',
'/disposicion-licencia/print-estival?id=' . $model->id,
[
'title' => 'Download',
'data-pjax' => '0',
'target' => '_blank',
]
);

Related

Trigger tab click event via button

How can I trigger click on 2nd tab event via butto, with jquery?
$('#packingGrid2').trigger('click');
I tried do this with trigger pasted above, but it doesnt work. Any thoughts?
#Keydose, there is my code.
$items = [
[
'url' => \Yii::$app->params['pjax_enabled']?null:Url::to(['step_1', 'tab' => 'packingGrid1']),
'label' => '<i class="glyphicon glyphicon-check"></i> Weryfikacja linii',
'content' => \Yii::$app->controller->renderPartial('step_1', [
'gridId' => 'packingGrid1',
'model' => $model,
'form' => $form,
]),
'linkOptions' => [
'data-id' => 'packingGrid1',
],
],
[
'url' => \Yii::$app->params['pjax_enabled']?null:Url::to(['step_2', 'tab' => 'packingGrid2']),
'label' => '<i class="glyphicon glyphicon-user"></i> Ustawienia pracowników',
'content' => ($model->isNewRecord) ? '<strong>Uzupełnij weryfikację linii</strong>' :
\Yii::$app->controller->renderPartial('step_2', [
'gridId' => 'packingGrid2',
'model' => $model,
'form' => $form,
]),
'linkOptions' => [
'data-id' => 'packingGrid2',
],
'active' => ($model->isNewRecord) ? false : true,
],
],
];
echo TabsX::widget([
'items' => $items,
'position' => TabsX::POS_ABOVE,
'encodeLabels'=>false,
'bordered'=>true,
]);

yii2 kartik switch input type radio button get value in javascript

I use the yii2 kartik switch input with the type radio and i want to get the value of the selected option in javascript (specially in a JsExpression), here is my code :
$model->orientation = 'Portrait';
echo $form->field($model, 'orientation')->widget(SwitchInput::classname(), [
'name' => 'information_orientation',
'type' => SwitchInput::RADIO,
'value' => 'Portrait',
'items' => [
['label' => 'Portrait&nbsp&nbsp&nbsp', 'value' => 'Portrait'],
['label' => 'Paysage&nbsp&nbsp&nbsp', 'value' => 'Paysage'],
],
'pluginOptions' => [
'onText' => 'Oui',
'offText' => 'Non',
'onColor' => 'success',
'offColor' => 'danger',
'size' => 'mini'
],
'labelOptions' => ['style' => 'font-size: 13px'],
]);
I have tried :
$([name='information-orientation']).val()
But it returned an undifined value
SwitchInput class does not care about the name property you give it
according to their docs, you need to wrap name in a options array
echo $form->field($model, 'orientation')
->widget(SwitchInput::classname(), [
'options' => ['name' => 'information_orientation'],
'type' => SwitchInput::RADIO,
....
from docs:
options: array the HTML attributes for the widget input tag.
edit:
you can then use the following
// to get value:
$("[name='information_orientation']").val()
// to check if the switch is or on off
$("[name='information_orientation']").prop('checked')

Pjax not working, reload the whole page

I'm developing a project using Yii2 and get frustrated with Pjax because it never works for me. Here is a simple gridview with pjax that I have created:
<?php Pjax::begin(['id' => 'member-list-pjax', 'timeout' => 5000]);?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
'member_card_number',
'member_name',
[
'attribute' => 'member_address',
'format' => 'raw',
],
'member_phone1',
'member_phone2',
[
'attribute' => 'member_type',
'value' => function ($model, $key, $index, $column){
return $model->getTypeLabel($model->member_type);
},
'filter' => $searchModel->getTypeLabel(),
],
],
]); ?>
<?php Pjax::end();?>
The Pjax doesn't work at all. When I search something in gridview's filter, it reloads the entire page. There is also no javascript error. Can anybody help me?
UPDATE
My action is actually very long. But here is the part that I think is related to the gridview:
$searchModel = new MemberSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
if (isset($_GET['MemberSearch']))
$searchModel->attributes = $_GET['MemberSearch'];
return $this->render('form', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
UPDATE 2
I use kartik's gridview now, I removed the PJax and put 'pjax' => true in the gridview, it successfully do Ajax Filter, BUT still reload the whole page not long after.
GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'pjax' => true,
'pjaxSettings' => [
'neverTimeout' => true,
],
'columns' => [ /*columns same as above*/ ]
]);
Please check your id and put this javascript in top of the page
$('body').on('click','.reload',function(){
$.pjax.reload({container: '#w0-pjax'});
});
Also use below code
<?php \yii\widgets\Pjax::begin(['linkSelector'=>'','id'=>'w0-pjax']); ?>

Yii2 button onclick anonymous function

I am new to Yii2, and I am struggling to trigger an anonymous function by pressing a Yii2 button.
Below are 6 samples, of which first two are OK.
But that is not exactly what I wish to have.
I would like to know how I can get an anonymous function working, like cases for "Button 3" and "Button 5". I tested how to make a function call via Controller, and that works ok, but that is neither what I want. I would appreciate Your help - thanks!
// This works
$button1 = Button::begin (
[
'label' => 'Button 1',
'options' => [
'class' => 'btn btn-primary',
'onclick' => 'alert("Button 1 clicked");',
],
]);
$button1->run();
// This works
echo Html::button('Button 2', [ 'class' => 'btn btn-primary', 'onclick' => 'alert("Button 2 clicked");' ]);
// This DOES NOT work
echo Html::button('Button 3', [ 'class' => 'btn btn-primary', 'onclick' => 'function ( $event ) { alert("Button 3 clicked"); }' ]);
// This DOES NOT work
$button4 = Button::begin (
[
'label' => 'Button 4',
'options' => [
'class' => 'btn btn-primary',
// 'onclick' => 'alert("Button 1 clicked");',
],
]);
$button4->on('onclick', 'alert("Button 4 clicked");' );
$button4->run();
// This DOES NOT work
$button5 = Button::begin (
[
'label' => 'Button 5',
'options' => [
'class' => 'btn btn-primary',
'onclick' => 'function ( $event ) { alert("Button 5 clicked"); }',
],
]);
$button5->run();
// This DOES NOT work
$button6 = Button::begin (
[
'label' => 'Button 6',
'options' => [
'class' => 'btn btn-primary',
//'onclick' => 'function ( $event ) { alert("Button 4 clicked"); }',
],
]);
$button6->on('onclick', 'function ( $event ) { alert("Button 6 clicked"); }' );
$button6->run();
You can wrap your anonymous function in self-executing anonymous function ()().
So your second example would look something like this.
echo Html::button('Button 3', [ 'class' => 'btn btn-primary', 'onclick' => '(function ( $event ) { alert("Button 3 clicked"); })();' ]);
Search Google to learn more about self-executing anonymous functions in Javascript. You should find tons of information and examples.
You can try something like using a submitButton instead of a button,
if you have other submitButton insert a condition that is for example 0 when you use the first button and 1 when you use the second one, then put your function in the controller and check, the first submit launches the function, the second submit makes something else. I know is not the best way, but it is the only way I imagine to do that.
Or, you can use ajaxSubmitButton:
AjaxSubmitButton::begin([
'label' => 'Check',
'ajaxOptions' => [
'type' => 'POST',
'url' => 'country/getinfo',
/*'cache' => false,*/
'success' => new \yii\web\JsExpression('function(html){
$("#output").html(html);
}'),
],
'options' => [
'class' => 'customclass',
'type' => 'submit'
]
]);

How to draw a CGridView after document ready?

How to draw a CGridView after document ready? I have Yii framework 1.14, jquery and jquery.ui successfully loaded!
Console.error! Because the grid is draw after document ready
TypeError: jQuery(...).dialog is not a function
jQuery("#dialog").dialog();
My code:
[
'class' => 'zii.widgets.grid.CButtonColumn',
'template' => '{edit}',
'headerHtmlOptions' => ['class' => 'col-lg-1', 'style' => 'text-align:center'],
'buttons' => [
'edit' => [
'label' => '<span class="glyphicon glyphicon-comment"></span>',
'url' => "Yii::app()->controller->createUrl('ViewComments',['key'=>\$data->key])",
'imageUrl' => null,
'options' => [
'style' => 'color: black',
'rel' => 'tooltip',
'data-toggle' => 'modal',
'data-target' => "#myModal",
'title' => 'Комментировать',
'ajax' => [
'type' => 'post',
'url' => 'js:$(this).attr("href")',
'dataType' => 'html',
'success' => 'js:function(data){
jQuery("#dialog").dialog();
}'
],
],
],
]
],
Have you tried reloading js in your view
Yii::app()->clientScript->registerCoreScript('jquery');
Yii::app()->clientScript->registerCoreScript('jquery.ui');

Categories

Resources