关于 jquery select2 多个关键字 模糊查询的解决方法

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个属性,如果存在多个的场合可以根据情况修改。

做个记录,方便碰到类似情况的朋友

原文地址:https://www.cnblogs.com/yuanhuaming/p/5190058.html