Ajaxlink() Method In Yii2?

Hello everyone,

I’m now just learning yii, started straightly with yii2 (because i dont have any strictly-timed projects and believe that future is with yii2).

I realise that yii2 is under development. But still, is there any way to create an ajax link (like CHtml::ajaxLink() in yii1) by built-in methods?

P.S. I also realise the possibility of separate JS-code doing the same and even more accurately, but the question is about the built-in method :)

Thanks in advance!

In Yii2 most such client related methods have been removed. Recommendation is to use separate client files for all client assets (JS/CSS). You need to be using the new View object and Assets feature for handling all client calls. This is to mainly ensure right dependencies in which client scripts will be loaded.

If you are keen, you can easily mimic the Yii1.x ajax link method by passing the ‘ajax’ property to the options array for a normal ‘Html::a’ call. Use the $view->registerJs() function to register any javascript directly on your page.

Thanks for reply!

So, in that case, is that correct to do like this:


$menuItems[] = ['label' => Icon::show('starfull', [], Icon::WHHG).'Main', 'url' => '#', 'linkOptions' => ['onclick' => 'snwNav(\'site/index\'); return false;']]

or it is better to do like this:


$menuItems[] = ['label' => Icon::show('starfull', [], Icon::WHHG).'Main', 'url' => '#', 'linkOptions' => ['id' => 'snwNavLinkIndex']]

and then register js with $(’#snwNavLinkIndex’).click(…) ?

P.S. personal thanks for your extensions, they look great :)

The second option is better because you can control which position the java-script call will be loaded in the view. If you have many such client scripts, its usually better to put it in one JS file and call via the AssetBundle.

You are welcome.

Thanks!

So, i think, this topic can be finished now.

@Fortis please explain step by step i instant needed it to implement.