0

我正在做一个项目如何计算yii2中的总数。当我构建项目时,当我输入数据为百分比格式时,发现问题。当我保存数据时,索引看起来是2300%,即使它是23%。我能做什么? 在我看来,像this'format格式 '=> [' 百分”],在Kartik GridView中的百分比格式(yii2)

enter image description here 这是我的代码

<?php 
 

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

 
/* @var $this yii\web\View */ 
 
/* @var $searchModel backend\models\LaporanSearch */ 
 
/* @var $dataProvider yii\data\ActiveDataProvider */ 
 

 
$this->title = 'Laporans'; 
 
$this->params['breadcrumbs'][] = $this->title; 
 
?> 
 
<div class="laporan-index"> 
 

 
    <h1><?= Html::encode($this->title) ?></h1> 
 
    <?php // echo $this->render('_search', ['model' => $searchModel]); ?> 
 

 
    <p> 
 
     <?= Html::a('Create Laporan', ['create'], ['class' => 'btn btn-success']) ?> 
 
    </p> 
 
    <?= GridView::widget([ 
 
     'dataProvider' => $dataProvider, 
 
     'filterModel' => $searchModel, 
 
      'showPageSummary'=>true, 
 

 
     'columns' => [ 
 
      ['class' => 'kartik\grid\SerialColumn'], 
 

 
      // 'id', 
 
      // 'inv_no', 
 
      // 'inv_date', 
 
      //'rate', 
 
      [ 
 
      'attribute'=>'kode_customer', 
 
      // 'width'=>'150px', 
 
      //'hAlign'=>'right', 
 
      // 'format'=>['decimal', 0], 
 
     ], 
 
      // 'kode_item', 
 
      // 'qty', 
 
      // 'price', 
 
      // 'ed', 
 
      ['attribute'=>'total_price', 
 
      'pageSummary'=>true 
 
], 
 
      // 'dsc', 
 
['attribute'=>'total_dsc', 
 
      'pageSummary'=>true 
 
],   // 'trans', 
 
['attribute'=>'total_trans', 
 
      'pageSummary'=>true 
 
],    
 
['attribute'=>'total_margin_rp', 
 
      'pageSummary'=>true 
 
],    
 
['attribute'=>'total_margin_persen', 
 
      'pageSummary'=>true, 
 
      // 'groupSeparator' => '.', 
 
      'format'=>['percent',], 
 

 

 

 
],   // 'kode_area', 
 

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

+0

请让我看看你的db的真实total_margin_persen值内容 – scaisEdge

回答

0

你的问题很可能是由于这是因为您已将实际百分比值存储在数据库中。因此,例如,您的23%实际上是数据库表中的数字23。

如果你看一下在这里Yii2百分比格式的文档(http://www.yiiframework.com/doc-2.0/yii-i18n-formatter.html#asPercent()-detail),你会看到,它说:

的值进行格式化。它必须是一个因素,例如0.75将导致 在75%。

因此,为了让您的数据正确显示,您必须提供数据作为因子,使用不同的格式化程序或编写自己的格式化功能。