开发者

jQuery autocomplete: change SQL query depending on selected checkbox

开发者 https://www.devze.com 2023-01-22 05:04 出处:网络
imagine i have one textbox (with autocomplete 1.1, not the latest UI version) and a checkbox. The textbox triggers an asp.net .ashx page that will execute a stored procedure at the sql-server and retu

imagine i have one textbox (with autocomplete 1.1, not the latest UI version) and a checkbox. The textbox triggers an asp.net .ashx page that will execute a stored procedure at the sql-server and return the results.

It's all working, but i want to add another feature. When the checkbox is checked i want stored_procedure_1 to be executed. If the checkbox is unchecked, stored_procedure_2 must be executed. The checkbox is unchecked by default.

My question: How do i tell the ashx page if the checkbox is checked or not..? By default the autocomplete will trigger something like: autocompletedata.ashx?q=myname and that will execute stored_procedure_2, but when the checkbox is checked it must trigger autocompletedata.ashx?q=myname&mycheckbox=begin so that stored_procedure_1 will be executed.

Do i have to add some jQuery code to pass the checked checkbox? I am totally lost..

Thanks in advance.

Form elements:

<input id="search_employee" type="text" class="employee" />
<input type="checkbox" name="mycheckbox" value="begin" />

jQuery:

 $().ready(function() {

        $("#search_employee").autocomplete("includes/AutocompleteData.ashx", {
            minChars: 3,
            max: 15,
            selectFirst: false,
            scrollHeight: 300,
            formatItem: function(data, i, n, value) {
            if (value.split("_")[3]!== null)
            {
            return "<img style = 'width:40px;height:53px;float:left; margin:2px 5px 2px 0' src='/pictures/thumbs/"
            + value.split("_")[3] + "'/> " + value.split("_")[0] + "<br /><span class=smallname>" + value.split("_")[2] + "<br/>" + value.split("_")[4] + "</span>";
        } 
    },
            formatResult: function(data, value) {
    开发者_StackOverflow中文版            return value.split("_")[0];
            }
        });

Part of autocompletedata.ashx :

Dim searchname As String = context.Request.QueryString("q")
Dim searchstart as string = context.Request.QueryString("mycheckbox")
Dim searchsql as string

if searchstart = "begin" then
   searchsql = "stored_procedure_1"
else
   searchsql = "stored_procedure_2"
end if

Dim conn As SqlConnection = New SqlConnection
conn.ConnectionString = ConfigurationManager.ConnectionStrings("MyConn").ConnectionString
Dim cmd As SqlCommand = New SqlCommand
cmd.CommandType = Data.CommandType.StoredProcedure
cmd.CommandText = searchsql.ToString
cmd.Parameters.AddWithValue("@SearchText", searchname)         


You can use .setOptions to set additional parameters:

ac.setOptions({ params: { paramOne:'somevalue', somethingelse:'foobar' } });

That is the programmatic way, but in your case:

$("#search_employee").autocomplete("includes/AutocompleteData.ashx", {
   params: { flag:'true' }, // just grab the checked attr beforehand
     ...//rest of your AC stuff

That will perform the following HTTP GET:

GET: includes/AutocompleteData.ashx?flag=true

Allowing you to access it via the Request.QueryString collection in the ASHX.

EDIT

My bad - i was thinking another version of AC.

I think the param is called "extraParams":

$("#search_employee").autocomplete("includes/AutocompleteData.ashx", {
   extraParams: { flag:'true' }

Try them both, one will/should work. :)

HTH.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号