我在rails应用程序中有一个select2作为客户名,现在搜索功能与客户名配合得很好,因为选项的文本设置为客户名,现在我也希望select2也搜索客户的contact_no。 我在每个选项标签的data-attr中设置客户的contact_no。 因此,除了选项文本外,如何从数据属性中执行搜索。 预先感谢。
修改 这是HTML
<select>
<option value="1" data-contact-no="9898989898">Zamir</option>
<option value="2" data-contact-no="6767676767">Manihar</option>
<option value="2" data-contact-no="666666">Zimmie</option>
</select>
和JavaScript
$("select").select2();
答案 0 :(得分:0)
我找到了解决方案,只需将select2初始化为:
function matchCustom(params, data) {
// If there are no search terms, return all of the data
if ($.trim(params.term) === '') {
return data;
}
// Do not display the item if there is no 'text' property
if (typeof data.text === 'undefined') {
return null;
}
// `params.term` should be the term that is used for searching
// `data.text` is the text that is displayed for the data object
if (data.text.indexOf(params.term) > -1 || data.element.getAttribute('data-contact-no').indexOf(params.term) > -1) {
var matchedData = $.extend({}, data, true);
// You can return matched objects from here
return matchedData;
}
// Return `null` if the term should not be displayed
return null;
}
$("select").select2({
matcher: matchCustom
});
任何其他解决方案将不胜感激。 谢谢