Copyable.php 1001 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php
  2. namespace Dcat\Admin\Grid\Displayers;
  3. use Dcat\Admin\Admin;
  4. /**
  5. * Class Copyable.
  6. *
  7. * @see https://codepen.io/shaikmaqsood/pen/XmydxJ
  8. */
  9. class Copyable extends AbstractDisplayer
  10. {
  11. protected function addScript()
  12. {
  13. $script = <<<'JS'
  14. $('.grid-column-copyable').off('click').on('click', function (e) {
  15. var content = $(this).data('content');
  16. var $temp = $('<input>');
  17. $("body").append($temp);
  18. $temp.val(content).select();
  19. document.execCommand("copy");
  20. $temp.remove();
  21. $(this).tooltip('show');
  22. });
  23. JS;
  24. Admin::script($script);
  25. }
  26. public function display()
  27. {
  28. $this->addScript();
  29. $content = $this->column->getOriginal();
  30. $html = <<<HTML
  31. <a href="javascript:void(0);" class="grid-column-copyable text-muted" data-content="{$content}" title="Copied!" data-placement="bottom">
  32. <i class="fa fa-copy"></i>
  33. </a>&nbsp;{$this->value}
  34. HTML;
  35. return $this->value ? $html : '-';
  36. }
  37. }