Ankur Soni
Ankur Soni

Reputation: 6018

target _blank in HTML::a not working yii2

I am using below peice of code.

[
            'attribute' => 'Application',
            'format' => 'html',
            'value' => function ($dataProvider) {
                $student_username = $dataProvider->student_username;
                return Html::a('Print', Url::toRoute(['/coordinatorpanel/print-form', 'student_username' => $student_username]),
                    ['target' => '_blank', 'class' => 'btn btn-success center-block']);
            },
        ]

HTML OUTPUT :

<a class="btn btn-success center-block" href="/nse/frontend/web/index.php?r=coordinatorpanel%2Fprint-form&amp;student_username=COR39690113" target="_blank">

But, when I click on the link, I am not navigated to new tab, the request is processed in same Tab. I tried this on both 'Mozilla' and 'Chrome'.

Any help would be deeply rewared :)

Upvotes: 5

Views: 18953

Answers (5)

111
111

Reputation: 1918

Disable Pjax in gridview. In my case I wanted to display an image in a new window and it was coming up as garbled characters in the grid instead.

'format' => "raw",
'value' => function ($d) {
     return Html::a($d->image_filename, '/'.$d->imagePath(),['target'=>'_blank', 'data-pjax'=>"0"] );
    },

ps. be mindful of XSS using raw format

Upvotes: 18

Rohit Suthar
Rohit Suthar

Reputation: 3628

Try this way, it's also working perfectly -

The problem may also be in pjax. even if you use the row format.

In this case just add linkSelector param:

\yii\widgets\Pjax::begin(
    ['id' => 'samle', 'linkSelector' => 'a:not(.target-blank)']
);

and add corresponding css class to your links:

return Html::a(
    'Print',
    ['/site/print', 'id' => $model->id],
    ['target'=>'_blank', 'class' => 'target-blank']
);

This will prevent only these links from pjax so they can be open in new tab.

Upvotes: 2

rishad2m8
rishad2m8

Reputation: 1508

Try to change your code like below

[
        'attribute' => 'Application',
        'format' => 'raw',
        'value' => function ($dataProvider) {
            $student_username = $dataProvider->student_username;
            return Html::a('Print', Url::toRoute(['/coordinatorpanel/print-form', 'student_username' => $student_username]),
                ['target' => '_blank', 'class' => 'btn btn-success center-block']);
        },
    ]

Here it used 'format' => 'raw' to avoid formatting.

Upvotes: 10

Ankur Soni
Ankur Soni

Reputation: 6018

I had to go other way round to run code properly.

You can click here for reference

Below is the piece of code that I modified and worked for me. I had to set 'onclick' event and set href as empty.

[
    'attribute' => 'Application',
    'format' => 'raw',
    'value' => function ($dataProvider) {
        $student_username = $dataProvider->student_username;
        return Html::a('Print', '',
            ['onclick' => "window.open ('".Url::toRoute(['/coordinatorpanel/print-form', 
                          'student_username' => $student_username])."'); return false", 
             'class' => 'btn btn-success center-block']);
    },
],

Upvotes: 1

Bizley
Bizley

Reputation: 18021

I assume this code is inside some widget that formats output because this is common case. If so just change format to raw.

Upvotes: 2

Related Questions