简体   繁体   English

如何在yii2中使用pjax更新Yii2中的列?

[英]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. 我想在单击按钮后在views / viewfile / index中使用pjax更新数据库行的状态。 How can do that step by step, in which file write JavaScript code and in which file get data from AJAX? 如何一步一步地在哪个文件中编写JavaScript代码以及在哪个文件中从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')? 如何在数据库onclick('mybutton')中保存状态? In which file should call jquery for ajax? 应该在哪个文件中为jajax调用jquery?

is there any particular reason to use pjax? 使用pjax是否有任何特殊原因?

you can write your js code either in your view file using $this->registerJs(" your js code goes here"); 您可以使用$ this-> registerJs在视图文件中编写js代码(“您的js代码在这里”); or in a separate js file and get it loaded on this page. 或在单独的js文件中,然后将其加载到此页面上。

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. 您将通过控制器/操作中的ajax请求接收到的数据。一旦收到数据,就可以使逻辑起作用并更新数据库行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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