在yii2中,如何获取gridview的列标题

时间:2017-09-01 11:03:19

标签: 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>

&#34;但它给出了mysql数据库中的列标题??并且要求是gridview列标题??&#34;

3 个答案:

答案 0 :(得分:0)

获取模型的所有标题/标签您可以使用

 $model->attributes();

http://www.yiiframework.com/doc-2.0/yii-db-activerecord.html#attributes()-detail

答案 1 :(得分:0)

检入您的模型类。它有一个名为attributesLabels的方法。将它用于您的解决方案。

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

如果这不符合您的要求,请在模型中使用所需标签创建新方法

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

}

将其命名为

$model->getGridLabels();

答案 2 :(得分:0)

获取attributeLabel

$model->attributeLabels();

enter image description here