2

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?

kafuchau
  • 5,573
  • 7
  • 33
  • 38

3 Answers3

1

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>
jogaco
  • 712
  • 8
  • 25
0

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.

tonytonov
  • 25,060
  • 16
  • 82
  • 98
0

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

DanNetwalker
  • 1,089
  • 7
  • 3