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

编程入门 行业动态 更新时间:2024-10-23 17:33:42
本文介绍了Select2 jQuery插件:有没有一种方法可以按字母顺序对标签列表进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

I'm using Select2 plugin (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; } });

更多推荐

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

本文发布于:2023-10-26 23:25:40,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1531651.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字母   顺序   插件   标签   方法

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!