2016-08-31 38 views
0

我想在文本框中顯示gridview列的總和。加載頁面時,總和應該傳遞給文本框。 我在我的index.php文件中編寫了以下javascript代碼。但沒有得到這筆錢。 JavaScript代碼可能不正確。請讓我知道如何做到這一點 -yii2中的文本框中的gridview列的總和

的index.php -

<?php 

use yii\helpers\Html; 
use yii\grid\GridView; 

/* @var $this yii\web\View */ 
/* @var $searchModel frontend\modules\sgledger\models\SellitemsgSearch */ 
/* @var $dataProvider yii\data\ActiveDataProvider */ 

$this->title = 'Sunglass Ledger'; 
$this->params['breadcrumbs'][] = $this->title; 
?> 
<div class="sellitemsg-index"> 

    <h1>Sunglass Ledger</h1> 
    <?php // echo $this->render('_search', ['model' => $searchModel]); ?> 

    <div class= 'col-md-6'> 
     <?= GridView::widget([ 
     'id' => 'purchasetable', 
     'dataProvider' => $dataProvider2, 
     'filterModel' => $searchModel2, 
     'columns' => [ 
      ['class' => 'yii\grid\SerialColumn'], 

      //'poisg_id', 
      [ 
      'attribute' => 'Date', 
      'value' => 'posg.posg_date' 
      ], 
      'posg_invno', 
      [ 
      'attribute' => 'Vendor', 
      'value' => 'posg.posg_vname' 
      ], 

      //'poisg_sgname', 
      'poisg_qty', 

      //['class' => 'yii\grid\ActionColumn'], 
     ], 
    ]); ?> 
    </div> 

    <div class= 'col-md-6'> 
    <?= GridView::widget([ 
     'id' => 'selltable', 
     'dataProvider' => $dataProvider, 
     'filterModel' => $searchModel, 
     'columns' => [ 
      ['class' => 'yii\grid\SerialColumn'], 

      //'ssgi_id', 
      [ 
      'attribute' => 'Date', 
      'value' => 'sellsg.ssg_date' 
      ], 
      'ssgi_invoiceno', 
      [ 
      'attribute' => 'Customer', 
      'value' => 'sellsg.ssg_customer' 
      ], 
      //'ssgi_sgname', 
      //'ssgi_price', 

      //['class' => 'yii\grid\ActionColumn'], 
     ], 
    ]); ?> 
</div> 
</div> 
<?php 
/* start getting the textboxes */ 
$script = <<< JS 
$(document).on('ready', function(e) { 

     var purgrid = purchasetable.getElementsByTagName('poisg_qty'); 
     var total0 = 0; 


     for (var i = 0; i < purgrid.length; i++) { 

      var totp = purgrid[i].value 
      total0 = parseInt(total0) + parseInt(totp); 
     } 

     //^This number takes (n+1)th column 
     console.log('First table total value: ' + total0); 

    }) 
JS; 
$this->registerJs($script); 
/* end getting the textboxes */ 
?> 

本次輸出 - enter image description here

回答

2

可能是你可以使用showFooter爲GridView的

Firts你CA可以計算使用dataProvider的值

 $yourTotal =0; 
     $numRow = 0; 
     foreach ($dataProvider->models as $key => $value) { 
      $yourTotal += $value['your_attribute']; 
      $numRow++; 
     } 

然後在GridView中你可以設置showFooter => TRUE和列您可以根據需要添加腳註(最終footerOptions)

 echo GridView::widget([ 
     'dataProvider' => $dataProvider, 
     ...... 
     'showFooter'=>TRUE, 
     ..... 
     'columns' => [ 
      ['class' => 'yii\grid\SerialColumn'], 
      ...... 
      [ 'attribute' => 'your_attribute', 
       'label' => 'Your Lable', 
       'footer' => $yourTotal, 
       'footerOptions' => ['style' => 'text-align:right;'],   
      ],    
      ........ 

,你可以添加你的文字使用input

<?= Html::textInput('your_name', $yourTotal , options[]); ?> 

,或者你可以添加一個簡單的div

<div id='my_id'> <?= $yourTotal ?> </div> 
+0

Hi ScaiseEdge。 Showfooter我可以用kartik gridview自己做。但我實際上並不需要它。我想將數據傳遞給文本框。因爲我想有三個文本框。 1.從同一頁面購買(從一個gridview)2出售(從出售gridview)3.股票(前兩個文本框之間的差異) – Tanmay

+0

@Tanmay嗨...那麼你可以使用我的答案的第一部分使用dataProvider計算你需要的值。如果你不需要頁腳,你可以直接使用計算的值和單個文本框添加值。(像JavaScript一樣通過JavaScript訪問gridTable中的值並不容易。 ..你可以檢查html結果代碼進行檢查) – scaisEdge

+0

嗨ScaiseEdge ..我無法完成。你可以請發佈總代碼。 – Tanmay