Select2 jQuery插件:有没有一种方法可以按字母顺序对标签列表进行排序?

问题描述:

我正在使用Select2插件( http://ivaynberg.github.io/select2/),正如您在屏幕快照中的标签列表中所看到的那样,它们不是按字母顺序列出的,我希望能够这样做.

I'm using Select2 plugin (http://ivaynberg.github.io/select2/) and as you can see by the list of tags I have in the screenshot, they aren't listed alphabetically and I'd like to be able to do so.

这是我目前拥有的,但是我想通过'文本'而不是'id'对数据(@appTags)进行排序,而不是查询:

This is what I currently have, but instead of query, I want to sort the data (@appTags) via 'text', not 'id':

scope.find('input[name=noun]').select2({
  data: @appTags,
  sortResults: function(results, container, query) {
    if (query.term) {
      return results.sort();
    }
    return results;
  }
});

我的控制台的屏幕截图在调试器中已暂停:

Screenshots of my Console paused in Debugger:

这是@appTags对象的图像,我想通过'text'对其进行排序:

Here's an image of the @appTags object, of which I'd like to sort via 'text':

以下是使用JS内置sort函数的文档中的一些代码.我将其修改为按字母顺序排序,而不是像文档中那样按长度排序.

Here is a bit of code from the docs that is using the JS built in sort function. I modified it to sort alphabetically instead of by length as they did in the docs.

$('#e22').select2({
    sortResults: function(results, container, query) {
        if (query.term) {
            // use the built in javascript sort function
            return results.sort();
        }
        return results;
    }
});