2017-07-29 86 views
0

我想在views/viewfile/index中使用pjax來點擊一個按鈕後更新數據庫的行狀態。如何一步一步地在文件中寫入JavaScript代碼以及從哪個文件中獲取AJAX數據?如何在yii2中使用pjax來更新Yii2中的列?

<?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> 
 
\t <?php \yii\widgets\Pjax::begin('id' => 'rp1'); ?> 
 
    <?= GridView::widget([ 
 
     'dataProvider' => $dataProvider, 
 
     'filterModel' => $searchModel, 
 
     'columns' => [ 
 
      ['class' => 'yii\grid\SerialColumn'], 
 

 
      'id', 
 
      'network', 
 
      'technology', 
 
      'sup_id', 
 
      'speaker', 
 
\t \t \t 'size', 
 
\t \t \t [ 
 
      '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('تایید'); 
 
       } 
 
      ] 
 
\t \t \t ], 
 
\t \t \t 
 
\t \t \t // [ 
 
      // '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'], 
 
     ], 
 
    ]); ?> 
 
\t <?php \yii\widgets\Pjax::end(); ?> 
 
</div>

如何保存在數據庫狀態的onclick( 'myButton的')?哪個文件應該爲jQuery調用ajax?

回答

0

是否有任何特別的原因使用pjax?

你可以在你的視圖文件中使用 $ this-> registerJs(「你的js代碼在這裏」)寫你的js代碼; 或在一個單獨的js文件中並將其加載到此頁面上。

您將通過您的控制器/操作中的ajax請求接收到的數據。 一旦您收到數據,就可以將您的邏輯工作並更新數據庫行。

+0

謝謝you.you右邊,但有沒有關於它的任何複雜的例子 – areff

+0

是的,絕對。對於複雜的js部分,我們更願意將js寫入單獨的文件,並將您的應用配置爲自動包含與視圖名稱相同的js文件。這應該處理所有你完整的js和實現將是整潔的 – Gunnrryy

+0

如何爲每個按鈕設置唯一的ID在一行? – areff