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

     1  package types
     2  
     3  import (
     4  	"html/template"
     5  	"strconv"
     6  
     7  	"github.com/kotovmak/go-admin/modules/language"
     8  	"github.com/kotovmak/go-admin/modules/utils"
     9  )
    10  
    11  type DefaultSelection struct {
    12  	*BaseButton
    13  	Options     FieldOptions
    14  	Placeholder string
    15  	Width       int
    16  }
    17  
    18  func btnUUID() string {
    19  	return "info-btn-" + utils.Uuid(10)
    20  }
    21  
    22  func GetDefaultSelection(placeholder string, options FieldOptions, action Action, widths ...int) *DefaultSelection {
    23  
    24  	id := btnUUID()
    25  	action.SetBtnId("." + id)
    26  
    27  	var width = 100
    28  	if len(widths) > 0 {
    29  		width = widths[0]
    30  	}
    31  	node := action.GetCallbacks()
    32  	return &DefaultSelection{
    33  		BaseButton: &BaseButton{
    34  			Id:     id,
    35  			Action: action,
    36  			Url:    node.Path,
    37  			Method: node.Method,
    38  		},
    39  		Placeholder: placeholder,
    40  		Width:       width,
    41  		Options:     options,
    42  	}
    43  
    44  }
    45  
    46  func (b *DefaultSelection) Content() (template.HTML, template.JS) {
    47  
    48  	optionsHtml := `<option value='__go_admin_all__'>` + language.Get("All") + `</option>`
    49  
    50  	for _, op := range b.Options {
    51  		optionsHtml += `<option value='` + op.Value + `'>` + op.Text + `</option>`
    52  	}
    53  
    54  	h := template.HTML(`<div class="btn-group pull-right" style="margin-right: 10px">
    55  <div style="width:`+strconv.Itoa(b.Width)+`px;">
    56  <select style="width:100%;height:30px;" class="`+b.Id+` select2-hidden-accessible" name="`+b.Id+`"
    57              data-multiple="false"  data-placeholder="`+b.Placeholder+`" tabindex="-1" aria-hidden="true">
    58  	<option></option>
    59      `+optionsHtml+`
    60  </select>
    61  </div>
    62  </div>
    63  <style type="text/css">
    64  	.box-header .select2-container .select2-selection--single {
    65  		height: 30px;
    66  		line-height: 24px;
    67  	}
    68  	.box-header .select2-container--default .select2-selection--single .select2-selection__rendered
    69  	{
    70  		line-height: 24px;
    71  	}
    72  </style>`) + b.Action.ExtContent()
    73  
    74  	return h, b.Action.Js() + template.JS(`
    75  	$(".`+b.Id+`").select2();
    76  `)
    77  }