mdbootstrap / bootstrap-templates

A collection of free Bootstrap 5 templates.
https://mdbootstrap.com/freebies/
3.07k stars 1.01k forks source link

custom sorting #648

Closed karthiknamburu closed 2 years ago

karthiknamburu commented 5 years ago

Hi, we are using bootstrap-tagsinput v0.8.0 in which the default sort in alphabetical order.

    sorter: function (texts) {
        return texts.sort();
      },

can i override this?

pablocorezzola commented 5 years ago

Do you need to replace it with your method? Try:

$('input').on("itemAdded", function(e){ 
    sort($(this).prev()); //---> ".bootstrap-tagsinput"
});

$('input').tagsinput();

function sort(ctrl){
    var words = [];
    ctrl.find(".tag").each(function(e){
        words.push($(this).text()); //-- catch
    })

    words = words.sort(); //--- sort.
    for(x=0; x <=words.length; x++){
        var text = $(ctrl).find(".tag").eq(x).text(); //-- text original
        var replace = $(ctrl).find(".tag").eq(x).html(); //-- text to replace
        if (replace != undefined){ 
            replace = $(ctrl).find(".tag").eq(x).html().replace(text, words[x]); //-- replace text
            $(ctrl).find(".tag").eq(x).html(replace); //-- write
        } 
    } 
}