简体   繁体   中英

How to use pjax in yii2 to update a column in Yii2?

I want to use pjax in views/viewfile/index to update state of row of database after clicking on a button. How can do that step by step, in which file write JavaScript code and in which file get data from AJAX?

 <?php use yii\\widgets\\Pjax; Pjax::begin(); echo GridView::widget([...]); Pjax::end(); ?> 

 <?php use yii\\helpers\\Html; use yii\\grid\\GridView; /* @var $this yii\\web\\View */ /* @var $searchModel app\\models\\LaptopSearch */ /* @var $dataProvider yii\\data\\ActiveDataProvider */ $this->title = 'Laptops'; $this->params['breadcrumbs'][] = $this->title; ?> <div class="laptop-index"> <h1><?= Html::encode($this->title) ?></h1> <?php // echo $this->render('_search', ['model' => $searchModel]); ?> <p> <?= Html::a('Create Laptop', ['create'], ['class' => 'btn btn-success']) ?> </p> <?php \\yii\\widgets\\Pjax::begin('id' => 'rp1'); ?> <?= GridView::widget([ 'dataProvider' => $dataProvider, 'filterModel' => $searchModel, 'columns' => [ ['class' => 'yii\\grid\\SerialColumn'], 'id', 'network', 'technology', 'sup_id', 'speaker', 'size', [ 'class' => 'yii\\grid\\ActionColumn', 'template' => '{myButton}' , // the default buttons + your custom button 'buttons' => [ 'myButton' => function($url, $model, $key) { // render your custom button return Html::a('تایید'); } ] ], // [ // 'class' => DataColumn::className(), // this line is optional /* 'attribute' => 'name', 'format' => 'text', 'label' => 'Name', ],*/ // 'optical_drive', //'webcam', // 'touchpad', // 'card_reader', // 'ethernet', // 'vga', // 'hdmi', // 'usb3_ports', // 'usb2_ports', // 'usb_type_c', // 'thunderbolt_ports', // 'serial_ports', ['class' => 'yii\\grid\\ActionColumn'], ], ]); ?> <?php \\yii\\widgets\\Pjax::end(); ?> </div> 

How to save state in database onclick('mybutton')? In which file should call jquery for ajax?

is there any particular reason to use pjax?

you can write your js code either in your view file using $this->registerJs(" your js code goes here"); or in a separate js file and get it loaded on this page.

the data you will receive via ajax request in your controller/action.. once you receive the data, you can put your logic to work and update the database row.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM