Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

According to jquery ui API, the source of an autocomplete field may be an array variable. How can I populate the array variable with results from a vbscript recordset result?

In the body of my page the I have my code something like this:

Dim rs, data_source
Set rs = Server.CreateObject("ADODB.Recordset")
rs.ActiveConnection = MM_rtasql_STRING
rs.CursorType = 2
rs.CursorLocation = 2
rs.LockType = 3

rs.source = "select field1 from mydatabase where whatever > soandso"
WHILE NOT rs.EOF
//add each result to string array JavaScript/Jquery can access.

I would like to pull the variable in my document ready function for my jquery auto complete field. How can this be implemented?

Thank You.

share|improve this question
add comment (requires an account with 50 reputation)

2 Answers

http://jqueryui.com/autocomplete/#remote the remote datasource. When using firefox with firebug I press F12 to open the console and see the xhr request made when I type something.

Inspecting the response I can see that JSON is used:

[{"id":"Turdus philomelos","label":"Song Thrush","value":"Song Thrush"},
 {"id":"Melospiza melodia","label":"Song Sparrow","value":"Song Sparrow"}
]

I think you can leave out the id because that would be optional. You could try an array of strings return like:

["Song Thrush","Song Sparrow"]

The vb code you're showing looks like server side code that would not run in a browser, the autocomplete will make a call to that asp page when the user types something and that is then used to filter your data:

The GET parameter that the autocomplete sends is called term. Look for the js code on the jquery ui page: http://jqueryui.com/autocomplete/#remote

share|improve this answer
add comment (requires an account with 50 reputation)

You could do something like this, assuming that you are happy populating the list on page load and don't want to do a lookup to the server in real-time:

<%
' server-side ASP
Dim rs, data_source, ado
Set ado = CreateObject("ADODB.Connection")
Set rs = Server.CreateObject("ADODB.Recordset")
ado.ConnectionString = MM_rtasql_STRING
ado.Open
rs.ActiveConnection = ado
rs.CursorType = 2
rs.CursorLocation = 2
rs.LockType = 3

rs.Open "select field1 from mydatabase where whatever > soandso"

' build up a string which looks like a Javascipt array (eg "['val1','val2']")
' instead of a loop, you could use rs.GetString(), which is quicker
dim s: s = "["
do until rs.EOF
    s = s & "'" & CStr(rs("field1")) & "',"
loop
rs.Close
' remove trailing comma and complete array string
s = left(s, len(s) - 1) & "]"
%>
<!-- client-side -->
<script type="text/javascript">
     $(document).ready(function() {
           $("textbox-selector").autocomplete({
                 source: <%=s %> // ASP insert of javascript array string
           });
     });
</script>

I've not tested this and the jQuery I've taken from their example: http://jqueryui.com/autocomplete/

share|improve this answer
add comment (requires an account with 50 reputation)

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.