jQuery UI dropdown/selectable
Very new to jQuery & jQuery-ui, and have a problem I can't seem to get working like I want it to work.
I'm using a $.get to do an ajax call to a sql stored procedure & return data in an XML format. In the callback part of the get, I create a function that builds a dropdown from the items returned. I also build an additional
<option> line & prepend it to the select. I've tried both the select that's part of jQuery & the .selectable that's part of jQuery-ui.
The code I'm using to build the box follows:
- $.get(sURL, function(data) {
- var select = $("#dList");
- //var blankline = $("<option value='' selected='selected'></option>");
- //select.prepend(blankline);
- var selectline = $("<option value='0' selected='selected'>Select Destination</option>");
- select.prepend(selectline);
- $(data).find("Row").each(function() {
- var mgid = $(this).find("MessageGroupID").text();
- var gname = $(this).find("GroupName").text();
- var option = $("<option>" + gname + "</option>");
- option.attr("value", mgid);
- select.append(option);
- });
- $("#dList").selectable();
- });
The code I use to remove the instruction that's currently shown (or the blank line) is as follows:
- $("#dList").one('mousedown', function() {
- $("option:first",this).remove();
- });
The XML that is the input to the dropdown is:
- <?xml version="1.0" encoding="UTF-8"?>
- <Rowsets DateCreated="2013-05-08T08:42:18" EndDate="2013-05-08T08:42:18" StartDate="2013-05-08T07:42:18" Version="1.1.1">
- <Rowset>
- <Columns>
- <Column Description="MessageGroupID" MaxRange="1" MinRange="0" Name="MessageGroupID" SQLDataType="4" SourceColumn="MessageGroupID"/>
- <Column Description="GroupName" MaxRange="1" MinRange="0" Name="GroupName" SQLDataType="12" SourceColumn="GroupName"/>
- </Columns>
- <Row>
- <MessageGroupID>1</MessageGroupID>
- <GroupName>First Shift</GroupName>
- </Row>
- <Row>
- <MessageGroupID>4</MessageGroupID>
- <GroupName>Alternate First Shift</GroupName>
- </Row>
- <Row>
- <MessageGroupID>7</MessageGroupID>
- <GroupName>Temporary First Shift</GroupName>
- </Row>
- </Rowset>
- </Rowsets>
What is happening though is:
- The instruction (or blank line) gets removed from the other items in the dropdown when the box is clicked on.
- However, what happens is that now the first row of data in the dropdown is now showing in the dropdown box, even though I haven't "selected" it, only clicked on the down arrow next to the dropdown.
- If I do click on the first row, it doesn't trigger a change.
- Clicking on other rows does trigger a change, and clicking BACK on to the first row then will trigger a change (but only after clicking on a different row first).
Here is what I'd like to happen:
- What I want the dropdown box to do is to either be blank, or have some type of meaningful instruction in it (this is what I was using the additional option & prepending it to the select).
- Secondly, I don't want that instruction to show, or if I can't suppress it, I don't want it to be selectable. Ideally, I would like for the instruction/blank to remain in the box until one of the items has been clicked on & actually selected.
- Thirdly, when an item in the list is selected, I want to then do an ajax call to another stored procedure to return data to populate a second dropdown list & show it.
- The general behavior of the list would be for dropdown1 to be visible & populate when the page is opened. Once dropdown1 has been selected, then dropdown2 would be visible & populate. When dropdown2 (which would have similar behavior as described in #2 above) was selected, a 3rd page item becomes visible & enabled.
I thought my issues might be related to limitations of the dropdown in jQuery, which is why I was trying to use jQuery-ui's version. Any help, suggestions or instruction would be appreciated.
Thanks!