Unable to save sort order of listbox
Opened this issue · 17 comments
I can reorder the items and they are also reordered in the listbox. However, it does not appear to be possible to store this sort order unless it's held in another hidden field. Can this feature be added or can you suggest how to implement such a feature?
No, not entirely. It was suggested creating a hidden field that holds a comma delimited string of the indexes sort order. I'd need to modify the bsmSelect script to update that hidden field unless I attach to the listbox's update event.
Connie DeCinko, Programmer Analyst
T: 602.340.7290 F: 602.416.7490
-----Original Message-----
From: Victor Berchet [mailto:reply@reply.github.com]
Sent: Monday, January 23, 2012 10:22 AM
To: Connie S. DeCinko
Subject: Re: [bsmSelect] Unable to save sort order of listbox (#24)
This belongs to the server side, see #15
Reply to this email directly or view it on GitHub:
#24 (comment)
Yep but why would you store it client side, that is redundant information with the <select>
itself.
Anyway if you really need to do this the best would be to listen on the original select change event as explained in the docs / examples.
Problem as reported on the asp.net forum:
The problem is that postback doesn't retain the order of list items. It will remember the selected item, but not the order. If you need to save it you'll have to iterate the items in script, then post that back directly from script. Or, iterate in script, concatenate into a string placed into a hidden field, then do a normal post back. In server code use the hidden field value to reconstruct the order.
I'll take a look at the listen example.
Connie DeCinko, Programmer Analyst
T: 602.340.7290 F: 602.416.7490
-----Original Message-----
From: Victor Berchet [mailto:reply@reply.github.com]
Sent: Monday, January 23, 2012 10:52 AM
To: Connie S. DeCinko
Subject: Re: [bsmSelect] Unable to save sort order of listbox (#24)
Yep but why would you store it client side, that is redundant information with the <select>
itself.
Anyway if you really need to do this the best would be to listen on the original select change event as explained in the docs / examples.
Reply to this email directly or view it on GitHub:
#24 (comment)
True, if you want to restore the order of unselected options, you might want to use your solution. Then I would suggest to serialize the order on the form submit event.
You have convinced me, it seems to be a valid issue after all.
What do you think of automatically populating the input[type='hidden'][id*='order'][rel=selected id]
?
Well... not so sure after all. Do you have an example where the sort ordre is not maintained (may be a link on asp.net) ?
Here is the code that did not work for me. Items were saved, but not in the order shown in the browser:
protected void session_DetailsView_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
{
panel_SqlDataSource.Delete();
ListBox panel_ListBox = session_DetailsView.FindControl("panel_ListBox") as ListBox;
int so = 0;
foreach (int i in panel_ListBox.GetSelectedIndices())
{
so++;
panel_SqlDataSource.InsertParameters["presenterID"].DefaultValue = panel_ListBox.Items[i].Value;
panel_SqlDataSource.InsertParameters["sortOrder"].DefaultValue = so.ToString();
panel_SqlDataSource.Insert();
}
}
Check the thread at http://forums.asp.net/t/1661881.aspx/1 for a discussion of the issue.
Here is what I came up with for a solution. Feels ugly, but it works:
$("#session_DetailsView_sponsors_ListBox").bsmSelect({
addItemTarget: 'bottom',
animate: true,
highlight: true,
debugMode: false,
plugins: [
$.bsmSelect.plugins.sortable({ axis: 'y', opacity: 0.5 }, { listSortableClass: 'bsmListSortableCustom' }),
$.bsmSelect.plugins.compatibility()
],
listClass: 'bsmList' // Class for the list ($ol)
});
// listen for changes and copy IDs in sorted order to a hidden field
$("#session_DetailsView_sponsors_ListBox").change(function (e, data) {
$("#session_DetailsView_sponsors_sortOrder").val($("#session_DetailsView_sponsors_ListBox").val() || []);
});
<asp:TemplateField HeaderText="Sponsor(s)">
<ItemTemplate>
<asp:Label ID="sponsor_Label" runat="server" Text='<%# addLineBreaks(Eval("sponsors").ToString()) %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:ListBox ID="sponsors_ListBox" runat="server" DataSourceID="sponsors_SqlDataSource" DataValueField="presenterID" DataTextField="presenter" SelectionMode="Multiple" title="Click to Select a Sponsor" class="sminit">
</asp:ListBox>
<asp:HiddenField ID="sponsors_sortOrder" runat="server" />
</EditItemTemplate>
<InsertItemTemplate>
<asp:ListBox ID="sponsors_ListBox" runat="server" DataSourceID="sponsors_SqlDataSource" DataValueField="presenterID" DataTextField="presenter" SelectionMode="Multiple" title="Click to Select a Sponsor" class="sminit">
</asp:ListBox>
<asp:HiddenField ID="sponsors_sortOrder" runat="server" />
</InsertItemTemplate>
</asp:TemplateField>
// prepopulate choices from database
protected void session_DetailsView_DataBound(object sender, EventArgs e)
{
if (session_DetailsView.CurrentMode == DetailsViewMode.Edit)
{
ListBox sponsors_ListBox = session_DetailsView.FindControl("sponsors_ListBox") as ListBox;
DataView sponsors_DataView = (DataView)sponsors_SqlDataSource.Select(DataSourceSelectArguments.Empty);
DataTable sponsors_dt = sponsors_DataView.ToTable();
if (sponsors_dt.Rows.Count > 0)
{
string sponsors_sortOrder = string.Empty;
for (int i = 0; i < sponsors_dt.Rows.Count; i++)
{
if (!string.IsNullOrEmpty(sponsors_dt.Rows[i]["sortOrder"].ToString()))
{
sponsors_ListBox.Items[i].Selected = true;
sponsors_sortOrder = sponsors_sortOrder + sponsors_dt.Rows[i]["presenterID"].ToString() + ",";
}
}
HiddenField sponsors_sortOrder_Hidden = session_DetailsView.FindControl("sponsors_sortOrder") as HiddenField;
if (sponsors_sortOrder.Length > 0)
{
sponsors_sortOrder_Hidden.Value = sponsors_sortOrder.Remove(sponsors_sortOrder.Length - 1, 1);
}
}
. . .
// save changes to database when saving detail record
protected void session_DetailsView_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
{
// update sponsors presenters
sponsors_SqlDataSource.Delete();
HiddenField sponsors_sortOrder = session_DetailsView.FindControl("sponsors_sortOrder") as HiddenField;
if (sponsors_sortOrder.Value.Length > 0)
{
string[] sponsorsIDs = sponsors_sortOrder.Value.ToString().Split(',');
int so = 0;
foreach (string id in sponsorsIDs)
{
so++;
sponsors_SqlDataSource.InsertParameters["presenterID"].DefaultValue = id.ToString();
sponsors_SqlDataSource.InsertParameters["sortOrder"].DefaultValue = so.ToString();
sponsors_SqlDataSource.Insert();
}
}
. . .
Connie DeCinko, Programmer Analyst
T: 602.340.7290 F: 602.416.7490
-----Original Message-----
From: Victor Berchet [mailto:reply@reply.github.com]
Sent: Tuesday, January 24, 2012 2:01 AM
To: Connie S. DeCinko
Subject: Re: [bsmSelect] Unable to save sort order of listbox (#24)
Well... not so sure after all. Do you have an example where the sort ordre is not maintained (may be a link on asp.net) ?
Reply to this email directly or view it on GitHub:
#24 (comment)
Ok, is there any need for a generic solution as I have proposed earlier then ?
I could see it if it's fairly easy to implement. This could be an issue with other platforms, not just .net.
Connie DeCinko, Programmer Analyst
T: 602.340.7290 F: 602.416.7490
-----Original Message-----
From: Victor Berchet [mailto:reply@reply.github.com]
Sent: Tuesday, January 24, 2012 8:44 AM
To: Connie S. DeCinko
Subject: Re: [bsmSelect] Unable to save sort order of listbox (#24)
Ok, is there any need for a generic solution as I have proposed earlier then ?
Reply to this email directly or view it on GitHub:
#24 (comment)
I'll give it a shot, should be easy.
You'll just have to add an hidden input which id contains 'order' and
rel attribute set to the id of your original select,
does that sound reasonable ?
On 01/24/2012 04:57 PM, StateBarofArizona wrote:
I could see it if it's fairly easy to implement. This could be an issue with other platforms, not just .net.
Connie DeCinko, Programmer Analyst
T: 602.340.7290 F: 602.416.7490-----Original Message-----
From: Victor Berchet [mailto:reply@reply.github.com]
Sent: Tuesday, January 24, 2012 8:44 AM
To: Connie S. DeCinko
Subject: Re: [bsmSelect] Unable to save sort order of listbox (#24)Ok, is there any need for a generic solution as I have proposed earlier then ?
Reply to this email directly or view it on GitHub:
#24 (comment)
Reply to this email directly or view it on GitHub:
#24 (comment)
Q: do you prefer to receive all the options or just the selected ones ?
Sounds like you're on the right path.
Connie DeCinko, Programmer Analyst
T: 602.340.7290 F: 602.416.7490
-----Original Message-----
From: Victor Berchet [mailto:reply@reply.github.com]
Sent: Tuesday, January 24, 2012 9:05 AM
To: Connie S. DeCinko
Subject: Re: [bsmSelect] Unable to save sort order of listbox (#24)
I'll give it a shot, should be easy.
You'll just have to add an hidden input which id contains 'order' and
rel attribute set to the id of your original select,
does that sound reasonable ?
On 01/24/2012 04:57 PM, StateBarofArizona wrote:
I could see it if it's fairly easy to implement. This could be an issue with other platforms, not just .net.
Connie DeCinko, Programmer Analyst
T: 602.340.7290 F: 602.416.7490-----Original Message-----
From: Victor Berchet [mailto:reply@reply.github.com]
Sent: Tuesday, January 24, 2012 8:44 AM
To: Connie S. DeCinko
Subject: Re: [bsmSelect] Unable to save sort order of listbox (#24)Ok, is there any need for a generic solution as I have proposed earlier then ?
Reply to this email directly or view it on GitHub:
#24 (comment)
Reply to this email directly or view it on GitHub:
#24 (comment)
Reply to this email directly or view it on GitHub:
#24 (comment)
For my use, I only need the selected ones.
Connie DeCinko, Programmer Analyst
T: 602.340.7290 F: 602.416.7490
-----Original Message-----
From: Victor Berchet [mailto:reply@reply.github.com]
Sent: Tuesday, January 24, 2012 9:27 AM
To: Connie S. DeCinko
Subject: Re: [bsmSelect] Unable to save sort order of listbox (#24)
Q: do you prefer to receive all the options or just the selected ones ?
Reply to this email directly or view it on GitHub:
#24 (comment)
May be the full list is better if you need to maintain a sorting order in a DB (i.e. with a unique constraint).
Let me know, there is an implementation here: 9ddb468
Ah 'rel' is not allowed on inputs so the id as to be <select id>-order
Hey guys i have some idea on it.
what if we add data-order
attribute in option
tag then it will get easier to maintain selection order using this plug-in.
I did it with asmSelect
which works fine.
regards
mithun