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;
Pjax::begin();
echo GridView::widget([
....
]);
Pjax::end();

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 »

Yii2: SQL of an Active Query

There are 2 ways to see actual SQL query.

Method 1: via Debug Module

If debug is enabled, you can find SQL query along with many other useful debug information.

Method 2: via Code

Say you have Record model:

$query = Record::find();
echo $query->createCommand()->rawSql;