This is skeleton code example on posting form data in JSON format processing by Yii’s controller action. Although this example uses Yii, the code can be easily adaped to pure PHP code.
$this->registerJsFile('@web/js/file.js', [ 'position' => View::POS_END, ]);
Note: optional position. values:
If order of the file is to be placed after some set of files, for example, css and js files defined in
use frontend\assets\SomeAsset; $this->registerJsFile('@web/js/file.js', [ 'depends' => SomeAsset::className(), ]);
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.
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
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!