Yii2: Include View Specific JavaScript

To include JavaScript for specific action, either the JavaScript code or file is to be registered in view file.

Method 1: Register JavaScript File

Use registerJsFile().

$this->registerJsFile('@web/js/file.js', [
    'position' => View::POS_END,

Note: optional position. values: POS_HEAD, POS_BEGIN, POS_END.

If order of the file is to be placed after some set of files, for example, css and js files defined in SomeAsset:

use frontend\assets\SomeAsset;
$this->registerJsFile('@web/js/file.js', [
    'depends' => SomeAsset::className(),

Method 2: Register Javascript Code

Continue »

Yii2: Ajax Gridview

Want to navigate through the grids or trying to sort and filter data without waiting for the entire page to load?

You need yii\widgets\Pjax and wrap it around GridView.

In the view:

use yii\grid\GridView;
use yii\widgets\Pjax;
echo GridView::widget([

This will cause all actions within the Grid to run in Ajax mode, including links.

Continue »

Update: b2evolution to WordPress migration

This is an update to previous post.

Since this blog was migrated successfully from b2evolution to WordPress, I decided to migrate another blog. More issues noticed with that particular migration. The blog was at a bigger scale and it’s too much work to fix issues manually. Therefore I decided to get my hands dirty to hack the script to make it work for me.

Issues with the script

Continue »

Yii2: Typeahead widget

In Yii V1, you use CJuiAutoComplete widget which displays an autocomplete field. There’s equivalent widget in Yii2 but I’ve not tried it yet because I tried something developed by external parties before I knew it exists. I’ve had successful use with the Typeahead widget, which is a wrapper class for Twitter Typeahead jQuery plugin.

Say the field stores user’s ID, but no one will ever remember user’s ID. You need to type in matching names to find user’s ID. This example submits AJAX call to find user with matching name.

Note that once a name is selected, ID is displayed in the field. It’d be nice to show user’s name instead, but I don’t know how to display the name yet to save the ID back into the database. If you know the answer, please leave a comment!

The View

Continue »

Yii2: Export GridView

Export is not available from the built-in GridView widget.

However there’s an enhanced GridView widget that include this functionality, along with many other features missing from the built in widget.

Here’s an example of exporting data in the grid to CSV or Excel format. In view file:
Continue »