github.com/kotovmak/go-admin@v1.1.1/template/types/display/copy.go (about)

     1  package display
     2  
     3  import (
     4  	"html/template"
     5  
     6  	"github.com/kotovmak/go-admin/template/types"
     7  )
     8  
     9  type Copyable struct {
    10  	types.BaseDisplayFnGenerator
    11  }
    12  
    13  func init() {
    14  	types.RegisterDisplayFnGenerator("copyable", new(Copyable))
    15  }
    16  
    17  func (c *Copyable) Get(args ...interface{}) types.FieldFilterFn {
    18  	return func(value types.FieldModel) interface{} {
    19  		return template.HTML(`
    20  <a href="javascript:void(0);" class="grid-column-copyable text-muted" data-content="` + value.Value + `" 
    21  title="Copied!" data-placement="bottom">
    22  <i class="fa fa-copy"></i>
    23  </a>&nbsp;` + value.Value + `
    24  `)
    25  	}
    26  }
    27  
    28  func (c *Copyable) JS() template.HTML {
    29  	return template.HTML(`
    30  $('body').on('click','.grid-column-copyable',(function (e) {
    31  	var content = $(this).data('content');
    32  	
    33  	var temp = $('<input>');
    34  	
    35  	$("body").append(temp);
    36  	temp.val(content).select();
    37  	document.execCommand("copy");
    38  	temp.remove();
    39  	
    40  	$(this).tooltip('show');
    41  }));
    42  `)
    43  }