Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to save sort order of listbox #24

Open
StateBarofArizona opened this issue Jan 23, 2012 · 17 comments
Open

Unable to save sort order of listbox #24

StateBarofArizona opened this issue Jan 23, 2012 · 17 comments

Comments

@StateBarofArizona
Copy link

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?

@vicb
Copy link
Owner

vicb commented Jan 23, 2012

This belongs to the server side, see #15

@vicb vicb closed this as completed Jan 23, 2012
@StateBarofArizona
Copy link
Author

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:[email protected]]
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)

@vicb
Copy link
Owner

vicb commented Jan 23, 2012

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.

@StateBarofArizona
Copy link
Author

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:[email protected]]
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)

@vicb
Copy link
Owner

vicb commented Jan 23, 2012

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.

@vicb vicb reopened this Jan 24, 2012
@vicb
Copy link
Owner

vicb commented Jan 24, 2012

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] ?

@vicb
Copy link
Owner

vicb commented Jan 24, 2012

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) ?

@StateBarofArizona
Copy link
Author

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:[email protected]]
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)

@vicb
Copy link
Owner

vicb commented Jan 24, 2012

Ok, is there any need for a generic solution as I have proposed earlier then ?

@StateBarofArizona
Copy link
Author

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:[email protected]]
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)

@vicb
Copy link
Owner

vicb commented Jan 24, 2012

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:[email protected]]
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)

@vicb
Copy link
Owner

vicb commented Jan 24, 2012

Q: do you prefer to receive all the options or just the selected ones ?

@StateBarofArizona
Copy link
Author

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:[email protected]]
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:[email protected]]
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)

@StateBarofArizona
Copy link
Author

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:[email protected]]
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)

@vicb
Copy link
Owner

vicb commented Jan 24, 2012

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

@vicb
Copy link
Owner

vicb commented Jan 24, 2012

Ah 'rel' is not allowed on inputs so the id as to be <select id>-order

@mithun12000
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants