5

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 :)

Ankur Soni
  • 5,725
  • 5
  • 50
  • 81

5 Answers5

18

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

111
  • 1,788
  • 1
  • 23
  • 38
10

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.

rishad2m8
  • 1,365
  • 20
  • 27
2

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

Bizley
  • 17,392
  • 5
  • 49
  • 59
  • I changed it but it is still the same. I haveupdated the code – Ankur Soni Feb 13 '17 at 10:23
  • 1
    It doesn't work with `'format' => 'raw'`? Check source if the link tag has been generated properly. – Bizley Feb 13 '17 at 11:16
  • Thank you for your reply. I have updated the above code with HTML output for link tag by setting Format to 'raw'. But still no positive effect. – Ankur Soni Feb 13 '17 at 12:37
  • 1
    This will not work if the rest of a widget is using Pjax. In that case you simply have to use: `['data-pjax' => 0, 'target' => '_blank']` – Rich Harding Aug 02 '18 at 10:19
2

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.

Rohit Suthar
  • 3,528
  • 1
  • 42
  • 48
1

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']);
    },
],
Community
  • 1
  • 1
Ankur Soni
  • 5,725
  • 5
  • 50
  • 81