Sunday 10 January 2016

autocomplete jquery example

 $(document).ready(function () {
            SearchText();
        });

=============================================
function SearchText() {
            $(".autosuggest").autocomplete({
                select: function (event, ui) {
                    $("#<% =hf_countryId.ClientID %>").val(ui.item.val);
                    //alert(ui.item.desc);
                    $("#ctl00_ContentPlaceHolder1_rb_currency").find("label")
                                          .each(function () {
                                              if ($(this).html() == ui.item.desc) {
                                                  console.log($(this).html());
                                                  $(this).parent().children("input").attr("checked", "checked");
                                              }
                                              else {
                                                  $(this).parent().children("input").attr("checked", "");
                                              }
                                              // console.log($(this).html());
                                          }
                                          )
                },
                source: function (request, response) {
                    $.ajax({
                        type: "POST",
                        contentType: "application/json; charset=utf-8",
                        url: "JoiningPersonalInfo.aspx/GetAutoCompleteData",
                        data: "{'country':'" + document.getElementById('txt_delLoc').value + "'}",
                        dataType: "json",
                        success: function (data) {
                            //response(data.d);
                            response($.map(data.d, function (item) {
                                return {
                                    label: item.CountryName,
                                    val: item.CountryId,
                                    desc: item.DefaultCurr
                                    //console.log(item);
                                }
                            }))

                        },
                        error: function (result) {
                            alert("Error");
                        }

                    });
                },
                change: function (event, ui) {
                    // provide must match checking if what is in the input is in the list of results. HACK!
                    var source = $(this).val();
                    var found = false;
                    $('.ui-autocomplete li').filter(function () {
                        if (ui.item != null) {
                            if (ui.item.value.toString() == source.toString()) {
                                found = true;
                                return;
                            }
                        }
                    });
                    if (found == false) {
                        $(this).val('');
                        alert("Please select country from given list.");
                    }
                    else {
                        $(this).val(ui.item.label);
                    }
                }
            });
        }

===========================================

[WebMethod]
    public static List<Country> GetAutoCompleteData(string country)
    {
        List<Country> result = new List<Country>();
        OleDbConnection cn = new OleDbConnection(ConfigurationManager.AppSettings["ConStr"].ToString());
        OleDbCommand cm = new OleDbCommand("", cn);
        using (OleDbConnection con = new OleDbConnection(ConfigurationManager.AppSettings["ConStr"].ToString()))
        {
            using (OleDbCommand cmd = new OleDbCommand("SELECT CM_NAME,CM_ID,CM_DEFAULT_CURR FROM id_country_mst where  cm_id <> 1 and  UPPER(CM_NAME) like UPPER('%" + country + "%')", con))
            {
                con.Open();
                //cmd.Parameters.AddWithValue("@SearchText", country);
               OleDbDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    result.Add(new Country() { CountryId = dr["CM_ID"].ToString(), CountryName = dr["CM_NAME"].ToString(), DefaultCurr = dr["CM_DEFAULT_CURR"].ToString() });
                }
                return result;
            }
        }
         
    }

==========================================

public class Country
{
    public String CountryId { get; set; }
    public String CountryName { get; set; }
    public String DefaultCurr { get; set; }
         
}

No comments:

Post a Comment