jQuery 插件 asmSelect 不保留排序顺序

发布于 2024-09-13 02:03:29 字数 319 浏览 4 评论 0原文

因此,我使用 asmSelect 插件来创建列表,但也尝试用它来编辑现有列表。 asmSelect 允许您在提交之前手动排序/排列所选选项。

我的问题是,每当我从数据库中获取用户排序列表并让 asmSelect 在我的页面上执行其操作时,默认情况下(因为它是多重选择)它只会按照选择中原始选项出现的顺序对所选选项进行排序。因此,根本不保留我的排序顺序...

还有其他人看到这个问题并有解决方案吗?

So, I'm using the asmSelect plugin to create lists, but also trying to use it to edit existing lists. asmSelect allows you to manually sort/arrange the selected options before submitting.

My problem is whenever I go and grab the user sorted list from the database and let asmSelect do its thing on my page, by default (because it's a multiple select) it just orders the selected options in whatever order the original options in the select appears. Therefore, not retaining my sorted order at all...

Anyone else seen this issue yet and have a solution?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

素染倾城色 2024-09-20 02:03:29

我还尝试按照数据库中的顺序构建最初选择的列表。我最终更新了 asmselect。 在此处查看更新的 asmselect 代码和示例。

在我的 jsp 中:

<select id="availableItems" class="multiselect" name="menuDishes" multiple="multiple" title="Select items">
    <c:forEach var="item" items="${myAvailableItems}">
        <option value="${item.id}" data-sortby="${fn:indexOf(mySelectedItems, item.name)}" ${fn:contains(mySelectedItems, item.name) ? 'selected="selected"' : ''}>${item.name}</option>
    </c:forEach>
</select>

I also tried to build the initially selected list in the order from the database. I ended-up updating asmselect. See updated asmselect code and example here.

In my jsp:

<select id="availableItems" class="multiselect" name="menuDishes" multiple="multiple" title="Select items">
    <c:forEach var="item" items="${myAvailableItems}">
        <option value="${item.id}" data-sortby="${fn:indexOf(mySelectedItems, item.name)}" ${fn:contains(mySelectedItems, item.name) ? 'selected="selected"' : ''}>${item.name}</option>
    </c:forEach>
</select>
魄砕の薆 2024-09-20 02:03:29

这里同样的问题。

就我而言,我注意到值从 0 到 11 的 12 个项目列表的排序方式为 0、1、10、11、2、3...我的意思是,作为字符串排序而不是数字顺序。也许有某种方法可以强制 asm-select 转换为数字或按数字顺序排序?

编辑:没关系,我试图使用“option_1”、“option_2”等字符串来订购它。如果用于排序的值不是有效的数字字符串,则会回退为字符串排序。

Kchau,您是否没有将订单存储在额外的字段中,或者使用数据库 id 字段来检测退出存储选项的订单?一旦存储了订单(或检测到,您喜欢什么),就需要为要订购的代码生成 rel 值。我正在生成这样的东西:

<option value="202">A news item</option>
<option value="164">Another news item</option>
<option value="162">More boring news</option>
<option value="175" rel="option_00000" selected="selected">One of the selected news</option>
<option value="15" rel="option_00001" selected="selected">Another interesting selected news</option>
<option value="204" rel="option_00002" selected="selected">This interesting news was selected too</option>

它是对列表进行排序的 rel 值

Same problem here.

In my case, I noticed that a 12 items list with values 0 to 11 got sorted like 0, 1, 10, 11, 2, 3... As string ordering instead of numerical order, I mean. Maybe there's some way to force asm-select to do the conversion to numbers or to sort by numerical order?

EDIT: nevermind, I was trying to use a "option_1", "option_2", etc, string to order it. If the values it uses to order is not a valid numerical string, it falls back to order as string.

Kchau, could it be you aren't storing the order in an extra field, or using the database id field to detect the order in quitch the options where stored? Once you got the order stored (or detected, what you prefer), it's a matter of generating the rel value for the code to order. I'm generating something like this:

<option value="202">A news item</option>
<option value="164">Another news item</option>
<option value="162">More boring news</option>
<option value="175" rel="option_00000" selected="selected">One of the selected news</option>
<option value="15" rel="option_00001" selected="selected">Another interesting selected news</option>
<option value="204" rel="option_00002" selected="selected">This interesting news was selected too</option>

it's the rel value what orders the list

樱娆 2024-09-20 02:03:29

在启动 asm select 之前放置以下内容:

$('#id_of_your_select').html($("option", $('#id_of_your_select')).sort(function(a, b) { 
    var arel = $(a).attr('rel');
    var brel = $(b).attr('rel');
    return arel == brel ? 0 : arel < brel ? -1 : 1 
}));

它确保所选项目的放置顺序与您告诉它们的顺序相同。

$("select[multiple]").asmSelect({
    addItemTarget: 'bottom',
    animate: false,
    sortable: true,
    highlight: false
});

并确保当您将行保存到数据库中时,它们会保持该顺序,并且当您将行保存到数据库中时,它们会保持该顺序 。在页面加载时收集它们,它们也仍然按相同的顺序排列。

Place the following before you initiate the asm select:

$('#id_of_your_select').html($("option", $('#id_of_your_select')).sort(function(a, b) { 
    var arel = $(a).attr('rel');
    var brel = $(b).attr('rel');
    return arel == brel ? 0 : arel < brel ? -1 : 1 
}));

It makes sure that the selected items are put in the same order as you told them to be in.

$("select[multiple]").asmSelect({
    addItemTarget: 'bottom',
    animate: false,
    sortable: true,
    highlight: false
});

And make sure that when you save the rows in the database they maintain that order and when you collect them on the page load they are also still in the same order.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文