I have installed the extension
\supplyhog\ClipboardJs\ClipboardJsWidget
which seemingly working fine.
but I am struggling to make it work in gridview.
as the code for the extension is already using a widget and gridview itself being a widget, so I think that is making the conflict.
The suggested code outside gridview is like this:
//Button to copy text
<?= \supplyhog\ClipboardJs\ClipboardJsWidget::widget([
'text' => "Hello World",
// 'label' => 'Copy to clipboard',
// 'htmlOptions' => ['class' => 'btn'],
// 'tag' => 'button',
]) ?>
//Button to copy text from input id
<?= \supplyhog\ClipboardJs\ClipboardJsWidget::widget([
'inputId' => "#input-url",
// 'cut' => false, // Cut the text out of the input instead of copy?
// 'label' => 'Copy to clipboard',
// 'htmlOptions' => ['class' => 'btn'],
// 'tag' => 'button',
]) ?>
How I can integrate this in Gridview?
example -
it just shows like this - the third cell being the result of clipboard code.
example of text getting hidden
with the help of @Omer I tried to modified the code like this:
.mycopy{
position:absolute;
right:-10px;
top:-10px;
}
.mycopy .btn{
padding:2px !important;
}
[
'attribute' => 'title',
'format' => 'raw',
'value' => function($model){
$button=\supplyhog\ClipboardJs\ClipboardJsWidget::widget([
'text' => $model->title,
'label' =>'<i class="far fa-copy" aria-hidden="true"></i>',
]);
return "<div style='position:relative'><div style='margin-right:10px;'>".$model->title."</div><div class='mycopy'>".$button."</div></div>";
}
],
and the result looks like this:
What i understand is that you are trying to populate the button inside the gridview against every row and when clicked, you want a specific text from that row to be copied in the clipboard.
If that is correct then you can do the following, I will create the example code assuming the
user
model being used in theGridView
and when clicking on the button theBio
of the user will be copied to the clipboard.Note: You can change the
$model->description
to any column name you are trying to copy'text' => $model->description
.And if you are planning to show it inside the
DataColumn
rather than theActionColumn
you can use the following piece of code, it will show the column text and a button under it to copy the text to the clipoard.Update
To replicate the same css as mentioned in your image you should do the following
Add this css on top of your view
Then change your column definition to the following, I am using the fontawesome clipboard icon instead of using the image,
Note: I am not a css expert but managed to create the same, if some one can suggest better classes I am open to suggestions