HEEN
HEEN

Reputation: 4737

jQuery UI autocomplete values not getting selected after selection from mouse

I have created a functionality of autocomplete using jQuery. It works perfectly fine but for example:

Whenever I type a text and and select the values from the list using mouse, it gets selected for the first time but if I again type and select the list. The values don't get selected.

Below is my code. Why is it not working?

$(document).ready(function () {
$('#txtAssignVendor').autocomplete({
    source: AppConfig.PrefixURL + 'VendorData.ashx',
    position: {
        my: "left bottom",
        at: "left top",
    }
});});

Update

And in Vendor.ashx file below is the code which I use

public void ProcessRequest(HttpContext context)
    {
        try
        {
            //DataTable dt = new DataTable();
            string term = context.Request["term"] ?? "";
            List<string> VendorNames = new List<string>();
            string connString = ConfigurationManager.ConnectionStrings["ConnAPP_NEIQC_PLNG"].ConnectionString;

            using (OracleConnection conn = new OracleConnection(connString))
            {
                OracleCommand cmd = new OracleCommand("", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = ConfigurationManager.AppSettings["PackageName"].ToString() + ".GET_VENDOR_NAME";
                cmd.Connection = conn;

                cmd.Parameters.Add(new OracleParameter { ParameterName = "P_VENDORNAME", Value = term, OracleDbType = OracleDbType.NVarchar2, Direction = ParameterDirection.Input });

                cmd.Parameters.Add(new OracleParameter
                {
                    ParameterName = "TBL_DATA",
                    OracleDbType = OracleDbType.RefCursor,
                    Direction = ParameterDirection.Output
                });

                if (conn.State != ConnectionState.Open) conn.Open();
                OracleDataAdapter da = new OracleDataAdapter(cmd);

               // da.Fill(dt);

                OracleDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    VendorNames.Add(dr["VENDORNAME"].ToString());
                    //VendorNames.Add(string.Format("{0}-{1}", dr["VENDOR_CODE"], dr["VENDOR_NAME"]));
                }
            }

            JavaScriptSerializer js = new JavaScriptSerializer();
            context.Response.Write(js.Serialize(VendorNames));
        }
        catch (Exception ex)
        {                
            throw;
        }    
    }

Upvotes: 4

Views: 1411

Answers (1)

vmf91
vmf91

Reputation: 1937

Could you provide the expected JSON result from the request?

AS I don't have the JSON output, I have made a working example with an AJAX based autocomplete. You should type 3 characters to start the search.

$( "#country" ).autocomplete({
	source: function( request, response ) {
		$.ajax({
			type: 'GET',
			url: "https://restcountries.eu/rest/v2/name/" + request.term,
			dataType: "json",
			success: function( resp ) {
        		var data = []
        		resp.forEach(d => {data.push(d.name)})
	            response(data)
			}
		});
	},
	minLength: 3,
	select: function( event, ui ) {
		console.log( ui.item ?
			"Selected: " + ui.item.label :
			"Nothing selected, input was " + this.value);
	},
});
<script type="text/javascript" src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script type="text/javascript" src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

<input type="text" id="country" />

Upvotes: 1

Related Questions