2017-09-01 65 views
0

的列標題「我在我的項目一個GridView。現在我想列數組中打道回府。我應該使用哪種方法???看到代碼..」在yii2,如何讓GridView控件

<?php 

    use yii\helpers\Html; 
    use yii\widgets\ActiveForm; 
    /* @var $this yii\web\View */ 
    /* @var $model app\models\Companies */ 

    $this->title = 'Export File';//'Create Companies'; 
    $this->params['breadcrumbs'][] = ['label' => 'Companies', 'url' => ['index']]; 
    $this->params['breadcrumbs'][] = $this->title; 
    $allColumnsDBArray = $model->getTableSchema()->getColumnNames(); 
    //problem is here it is giving column heading of mysql database table 
?> 

<div class="companies-create"> 
    <h1><?= Html::encode($this->title) ?></h1> 

    <div class="form-group"> 
     <?= Html::submitButton('Export CSV', ['class' => 'btn btn-primary','name' => 'export']) ?> 

     <?php echo "<br>";  
     foreach ($allColumnsDBArray as $colHeading) { 
      echo $colHeading; echo "<br>"; 
     }?> 

    </div> 
</div> 

「但它給出了mysql數據庫中的列標題和要求是gridview列標題??」

+0

您可以在模型中使用自定義屬性標籤'$ model-> getAttributeLabel('attribute_name');'' – Kalu

回答

0

入住模型類。它有一個名爲attributesLabels的方法。將其用於您的解決方案。

$model   = new ModelName(); 
print_r($model->attributeLabels()); 

如果這個不適合你的需求,然後與您所需的標籤模型中創建新的方法,

public function getGridLabels(){ 
    return [ 
    "id"   => "Id", 
    "first_name" => "First Name", 
    "email"  => "Email", 
    ]; 

} 

說它如下

$model->getGridLabels(); 
0

爲了獲得attributeLabel

$model->attributeLabels(); 

enter image description here