2014-09-05 13 views
5

Trong dự án của tôi, tôi đang sử dụng thành phần select2. Đối với các yếu tố đã chọn của tôi, tôi đã thêm biểu tượng và mô tả. Mã là một cái gì đó như thế này:chú giải công cụ của các phần tử đã chọn trong thành phần select2

function format(profile_opt) { 
     return profile_opt.text + "<br><span class=\"description_select2\"><i>" + $(profile_opt.element).attr('title') + "</i></span><br>" + 
     ($(profile_opt.element).attr('start').length==0?'':('from: <b>' + $(profile_opt.element).attr('start') + '</b>')) + ($(profile_opt.element).attr('end').length==0?'':(' to: <b>' + $(profile_opt.element).attr('end') + '</b>')) + 
     " <a href=\"#\" onclick=javascript:showUrlInDialogWithOptionId(\"<%=request.getContextPath()%>/assignments_date_set.jsp\",\"" + profile_opt.id + "\",\"" + $(profile_opt.element).attr('start') + "\",\"" + $(profile_opt.element).attr('end') + "\")>" + 
     "<img src=\"images/icons/small/grey/clock.png\" title=\"Set start and end assignment dates\" alt=\"Set start and end assignment dates\" class=\"clock\" id=\"clock\" width=\"20px\" height=\"20px\"></a>"; 
    } 

    function format2(profile_opt){ 
     return profile_opt.text +"<br><span class=\"description_select2\"><i>" + $(profile_opt.element).attr('title') + "</i></span>"; 
    } 

    $('#selected_profiles').select2({ allowSelectAllNone: true, closeOnSelect:false, width: '600px', placeholder: 'Click to select', 
     formatResult: format2, 
     formatSelection: format, 
     escapeMarkup: function(m) { return m; } 
     }); 

Câu hỏi của tôi là: cách đặt chú giải công cụ thành chuỗi con chỉ của phần tử tùy chọn định dạng. Tôi có nghĩa là tôi chỉ cần bắt đầu của tooltip không phải cú pháp này toàn bộ với html bên trong. Cảm ơn trước sự giúp đỡ nào.

Trả lời

1

Thêm những 2 thông số trong constructor select2:

formatResult: format, 
formatSelection: format 

Và sau đó xác định các chức năng format như dưới đây ngoài các nhà xây dựng Select2:

function format(item) { 
    var originalText = item.text; 
    return "<div title ='" + originalText + "'>" + originalText + "</div>"; 
} 

bài gốc là here

Các vấn đề liên quan