CTOlet / yii2-attachments

Yii2 extension for uploading and attaching the files to the models
66 stars 57 forks source link

Using in GridView #10

Closed larry-tx closed 9 years ago

larry-tx commented 9 years ago

Do you have any idea how to use your extension in conjunction with Katik's GridView (which for these purposes is not much different from the plain Yii2 GridView)? I've tried this approach (which I don't like because, lacking the ability to set one image a primary, I just want the first image) which simply produces a "(not set)":

'columns'      => [
                    [
                        'class' => '\kartik\grid\SerialColumn'
                    ],
                   [
                        'attribute' => 'files',
                        'label' => false,
                        'format' => 'raw',
                        'value' => function($data) {
                            foreach ($data->files as $file) {
                                echo Html::img($file->path);
                            }
                        }
                    ],
                    'name',
                    [
                        'attribute' => 'source',
                        'format'    => 'raw',
                        'label'     => 'Source',
                        'value'     => function ($data) {

                            return Html::a(Html::encode($data->source), $data->source_url, ['target' => '_blank']);
                        },
                    ],
...

I've also tried this approach which produces a "Undefined offset: 0" error:

[
                        'attribute' => 'files',
                        'label' => false,
                        'format' => 'raw',
                        'value' => function($data) {
                            echo Html::img($data->files[0]->path);
                        }
                    ],

I've also tried this approach which produces a "Setting unknown property: nemmo\attachments\components\AttachmentsTable::data" error. (The data when setting value like this is in $data, not $model):

'columns'      => [
                    [
                        'class' => '\kartik\grid\SerialColumn'
                    ],
                    [
                        'attribute' => 'files',
                        'label' => false,
                        'format' => 'raw',
                        'value' => function($data) {
                            foreach ($data->files as $file) {
                                echo \nemmo\attachments\components\AttachmentsTable::widget(['data' => $data]);
                            }
                        }
                    ],
                    'name',
                    [
                        'attribute' => 'source',
                        'format'    => 'raw',
                        'label'     => 'Source',
                        'value'     => function ($data) {

                            return Html::a(Html::encode($data->source), $data->source_url, ['target' => '_blank']);
                        },
                    ],

I really enjoy your extension. The more I work with the more I find that I can get out of it! Just looking for one more way to extend it. Any help that you can provide will be most appreciated.

CTOlet commented 9 years ago

First of all you should use $file->url instead of $file->path. The next your problem is that the value of the column must be a closure returning a string. Your solution is the following code:

[
    'attribute' => 'files',
    'label' => false,
    'format' => 'raw',
    'value' => function($data) {
        $output = '';
        if (isset($data->files[0]))
            $output .= Html::img($data->files[0]->url);
        return $output;
    }
]
larry-tx commented 9 years ago

Thanks a million! The returning a string in the closure was my mistake in copying, but somehow, I had also overlooked the url method in your code. Again, many thanks for such an excellent extension!