2014-02-24 6 views

Antwort

18

gefunden Eigentlich die Lösung durch den Matcher opt

$("#myselect").select2({ 
    matcher: function(term, text, opt){ 
     return text.toUpperCase().indexOf(term.toUpperCase())>=0 || opt.parent("optgroup").attr("label").toUpperCase().indexOf(term.toUpperCase())>=0 
    } 
}); 

Unter der Prämisse, zu modifizieren, dass das Label Attribut in jedem optgroup gesetzt.

+0

Nice! Vielen Dank. – flu

+0

danke, das war super nützlich! – Dave

+1

Schön! Ich empfehle jedoch, die Standard-Matcher-Implementierung zu verwenden, es sei denn, Sie möchten absichtlich vom Standard-Matcher abweichen. 'element.select2 ({matcher: function (begriff, text, opt) {return element.select2.defaults.matcher (Begriff, Text) || element.select2.defaults.matcher (Begriff, opt.parent (" optgroup ") .attr ("label"));}}); 'Dies ermöglicht eine etwas bessere Übereinstimmung, indem nicht nur Groß-/Kleinschreibung ignoriert wird, sondern auch diakritische Zeichen ignoriert werden. – hvd

0

Ein paar kleineren Änderungen an Menschen vorgeschlagen Code, weniger repetitiven und meistern, wenn es keine Eltern optgroups ist:

$('select').select2({ 
    matcher: function(term, text, opt){ 
     var matcher = opt.parent('select').select2.defaults.matcher;       
     return matcher(term, text) || (opt.parent('optgroup').length && matcher(term, opt.parent('optgroup').attr("label"))); 
    } 
}); 
10

Die oben genannten Antworten scheinen nicht, wenn Sie mit Select2 4.0 so aus dem Kasten heraus zu arbeiten sind für die Jagd, check this out: https://github.com/select2/select2/issues/3034

(Verwenden Sie die Funktion wie folgt aus: $("#example").select2({matcher: modelMatcher});)

function modelMatcher (params, data) { 
    data.parentText = data.parentText || ""; 

    // Always return the object if there is nothing to compare 
    if ($.trim(params.term) === '') { 
    return data; 
    } 

    // Do a recursive check for options with children 
    if (data.children && data.children.length > 0) { 
    // Clone the data object if there are children 
    // This is required as we modify the object to remove any non-matches 
    var match = $.extend(true, {}, data); 

    // Check each child of the option 
    for (var c = data.children.length - 1; c >= 0; c--) { 
     var child = data.children[c]; 
     child.parentText += data.parentText + " " + data.text; 

     var matches = modelMatcher(params, child); 

     // If there wasn't a match, remove the object in the array 
     if (matches == null) { 
     match.children.splice(c, 1); 
     } 
    } 

    // If any children matched, return the new object 
    if (match.children.length > 0) { 
     return match; 
    } 

    // If there were no matching children, check just the plain object 
    return modelMatcher(params, match); 
    } 

    // If the typed-in term matches the text of this term, or the text from any 
    // parent term, then it's a match. 
    var original = (data.parentText + ' ' + data.text).toUpperCase(); 
    var term = params.term.toUpperCase(); 


    // Check if the text contains the term 
    if (original.indexOf(term) > -1) { 
    return data; 
    } 

    // If it doesn't contain the term, don't return anything 
    return null; 
} 
+0

das hat funktioniert. Dies sollte jetzt die Antwort sein, da die oben genannten Probleme tatsächlich bestehen. Danke, Willbradley. – bubjavier