Tech Programming Ideas is one of the best places on the programming for programmers. Learn coding with Tech Programming Ideas tutorials. We are covered android programming, php, yii2 framework, javascript, mysql, vb.net etc.
Sometimes we want to collect user input in a batch mode. That is, the user can enter the information for multiple model instances and submit them all at once. We call this tabular input because the input fields are often presented in an HTML table.
To work with tabular input, we first need to create or populate an array of model instances, depending on whether we are inserting or updating the data. We then retrieve the user input data from the $_POST variable and assign it to each model. A slight difference from single model input is that we retrieve the input data using $_POST['ModelClass'][$i] instead of$_POST['ModelClass'].
In contrast to the single model forms explained before, we are working with an array of models now. This array is passed to the view to display the input fields for each model in a table like style and we will use helper methods of yii\base\Model that allow loading and validating multiple models at once:
This example for validate and save tabular data. Help to avoid insert many times the same table with ActiveForm.
Modify your controller code to use multiple models
<?php
namespace backend\modules\tools\controllers;
use Yii;
use backend\modules\tools\models\Products;
use backend\modules\tools\models\ProductsSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter; use yii\base\Model;
/**
* ProductsController implements the CRUD actions for Products model.
*/
class ProductsController extends Controller
{
.................
................
public function actionCreate() { //Find out how many products have been submitted by the form //$count = count(Yii::$app->request->post('Product', [])); $count=4; //Send at least one model to the form $prts = [new Products()]; //Create an array of the products submitted for($i = 1; $i < $count; $i++) { $prts[] = new Products(); } //Load and validate the multiple models if (Model::loadMultiple($prts, Yii::$app->request->post()) && Model::validateMultiple($prts)) { foreach ($prts as $product) { //Try to save the models. Validation is not needed as it's already been done. $product->save(false); } return $this->redirect('view'); } return $this->render('create', ['prts' => $prts]); }
..........
...........
}
The view file for the form needs to be altered like this, to use the multiple models;
How to save data in model using Yii2 grid with Editable column
Actually the solution is easy and need to the id of that specific row to update. We are use ajax.
Explain how in Yii 2 Framework, we can setup the enhanced Krajee EditableColumn to work with theKrajee GridView widget. The objective is to achieve the following functionality using kartik\grid\GridView andkartik\grid\EditableColumn:
configure a grid cell content to be editable
use advanced input widgets to edit content
capture the editable content POSTED via controller and update database via ajax
trigger model validation and validation error messages if needed
Inputs
Editable::INPUT_HIDDEN or 'hiddenInput'
Editable::INPUT_TEXT or 'textInput'
Editable::INPUT_PASSWORD or 'passwordInput'
Editable::INPUT_TEXTAREA or 'textArea'
Editable::INPUT_CHECKBOX or 'checkbox'
Editable::INPUT_RADIO or 'radio'
Editable::INPUT_LIST_BOX or 'listBox'
Editable::INPUT_DROPDOWN_LIST or 'dropDownList'
Editable::INPUT_CHECKBOX_LIST or 'checkboxList'
Editable::INPUT_RADIO_LIST or 'radioList'
Editable::INPUT_HTML5_INPUT or 'input'
Editable::INPUT_FILE or 'fileInput'
Editable::INPUT_WIDGET or 'widget', use this for any custom widget class to be used
Easily set any displayed content as editable in Yii Framework 2.0. This is an enhanced editable widget for Yii 2.0 that allows easy editing of displayed data, using inputs, widgets and more with numerous configuration possibilities.
This example add editable column in kartik grid view.
This grid view contains three types of editable controls such as DATE, TEXTVIEW and DROP DOWN LIST.
*************************** Source code of above grid view.
***************************
Use packeges are use yii\helpers\Html; use kartik\grid\GridView; use kartik\editable\Editable; use yii\widgets\ActiveForm; use yii\widgets\Pjax;
Following example helps to implement redirection in Controller class.
namespace app\controllers
use Yii;
Class TestController extends Controller{ public function actionIndex(){ $this->redirect(\Yii::$app->urlManager->createUrl("test/show")); }
public function actionShow(){ //your action here } } In above example when we visit test/index then this action redirect to show method of the test class.
CRUD stands for Create, Read, Update, and Delete, representing the four common tasks taken with data on most Web sites. CRUD Generator generates a controller and views that implement CRUD operations for the specified data model.
What is Gii?
Gii is web-based code generator.
With Gii we can quickly generate models, forms, modules, CRUD, etc..
How to use Gii
In our environment we can use Gii web-based interface by going to this address:
There are several form fields which we have to fill:
Model class - This is the ActiveRecord class associated with the table that CRUD will be built upon. Search Model Class- This is the name of the search model class to be generated. Controller Class - This is the name of the controller class to be generated. View Path - Specify the directory for storing the view scripts for the controller. Widget Used in Index Page - This is the widget type to be used in the index page to display list of the models.
LoginActivity on successful login starts the Main activity and this time because the user is logged on, the Main activity will continue its normal course. LoginActivity is declared as following in the manifest file: