Search code examples
yii2detailview

How to write Gridview code in Yii2 Detail View


Here i like to explain my problem clearly,

the below code is used to store the attached file address, in gridview i have make it as a link, so if i click the link it will show the file which is on the link

this is grid view code:

          [
             'label'=>'document_details',
             'format'=>'raw',
             'value' => function($model){
                    $data = $model->document_details;
                    $url = explode(',', $data);
                    $result = "";
                    foreach ($url as $key => $value) {
                        $result .= Html::a($value, $value);
                    }

                    return $result;
                 }
            ],

but the problem is am not able to do it on Detail view, i dont know how to do it.


Solution

  • UPDATE: From version 2.0.11 it is fine to use a function for value. Have a look at DetailView::$attributes.


    Take a look at this official example. It works differently to the GridView. Here you also specify a list of the attributes that should be taken into account. But the way is different: you don't specify a function for where to get the value. Instead you have to tell DetailView the value directly. You could do this:

    function createDocDetailUrls($model) {
        $data = $model->document_details;
        $url = explode(',', $data);
        $result = "";
        foreach ($url as $key => $value) {
            $result .= Html::a($value, $value);
        }
    
        return $result;
    }
    
    echo DetailView::widget([
        'model' => $model,
        'attributes' => [
            //... other attributes
            [
                'label' => 'Document details',
                'format'=> 'raw'
                'value' => createDocDetailUrls($model),
            ],
            //... other attributes
        ],
    ]);
    

    A similar question is here.