2016-08-31 2 views
0

텍스트 상자에 gridview 열의 합계를 표시하려고합니다. 페이지가로드되면 합계가 텍스트 상자로 전달됩니다. index.php 파일에 다음 자바 스크립트 코드를 작성했습니다. 그러나 합계를 얻지 못합니다. 자바 스크립트 코드가 올바르지 않을 수 있습니다. 내가 그것을 수행하는 방법을 알려주세요 -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

당신이 GRIDVIEW

에 대한 showFooter을 사용할 수 있습니다 될 수

Firts 계산할 수 ca를 dataProvider를 사용하는 값 ​​

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

다음의 GridView에서 당신은 TRUE> showFooter =을 설정할 수 있고, 열에서 당신은 당신이 바닥 글 (결국 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를 사용하여 필요한 값을 계산하십시오. 바닥 글이 필요하지 않은 경우 계산 된 값과 단일 텍스트 상자를 사용하여 직접 값을 추가 할 수 있습니다. (시도하는 것처럼 자바 스크립트에서 gridTable의 값에 액세스하는 것은 쉽지 않습니다. .. 당신은 수표에 대한 HTML 결과 코드를 검사 할 수 있습니다) – scaisEdge

+0

안녕 ScaiseEdge .. 나는 그것을 통해 만들 수 없습니다. 전체 코드를 게시 할 수 있습니까? – Tanmay