Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using in GridView #10

Closed
larry-tx opened this issue Apr 11, 2015 · 2 comments
Closed

Using in GridView #10

larry-tx opened this issue Apr 11, 2015 · 2 comments

Comments

@larry-tx
Copy link

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
Copy link
Owner

CTOlet commented Apr 11, 2015

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
Copy link
Author

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!

@CTOlet CTOlet closed this as completed Apr 11, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants