2014-09-05 38 views
5

在我的項目中,我使用了select2組件。對於我選擇的元素,我添加了圖標和說明。代碼是這樣的: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; } 
     }); 

我的問題是:如何設置工具提示只是格式選項元素的子字符串。我的意思是我只需要工具提示的開始,而不是使用HTML內部的全部語法。預先感謝您的幫助。

回答

1

select2構造函數添加這兩個參數:

formatResult: format, 
formatSelection: format 

,然後定義像下面的format功能選擇2構造函數外:

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

原帖是here