select2 只針對 元素的text()進行匹配,實際開發過程中可能會存在通過id 或者特殊編碼進行 多關鍵字匹配。
改動了下源碼:紅色為改動部分。
process=function(element, collection) { var group; if (element.is("option")) { // modify by yuan 2016.2.15 if (query.matcher(term, element.text(), element) || query.matcher(term, element.attr("data-help1"), element) || query.matcher(term, element.attr("data-help2"), element)) { collection.push(self.optionToData(element)); } } else if (element.is("optgroup")) { group=self.optionToData(element); element.children().each2(function(i, elm) { process(elm, group.children); }); if (group.children.length>0) { collection.push(group); } }
html 部分在元素上增加2個屬性,如果存在多個的場合可以根據情況修改。
做個記錄,方便碰到類似情況的朋友