jQuery autocomplete with images and text

  Рет қаралды 28,246

kudvenkat

kudvenkat

Күн бұрын

Link for all dot net and sql server video tutorial playlists
www.youtube.co...
Link for slides, code samples and text version of the video
csharp-video-tu...
Healthy diet is very important both for the body and mind. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking our KZbin channel. Hope you can help.
/ @aarvikitchen5572
In this video we will discuss how to display both images and text in the suggestions menu of jQuery autocomplete widget. Let us understand this with an example.
When we type the first character of a country name, we want to retrieve all the countries that start with that letter and display their flag and name in the suggestions menu.
Web Service Code
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web.Script.Serialization;
using System.Web.Services;
namespace Demo
{
[WebService(Namespace = "tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[System.Web.Script.Services.ScriptService]
public class CountryService : System.Web.Services.WebService
{
[WebMethod]
public void GetCountries(string term)
{
string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
List<Country> countries = new List<Country>();
using (SqlConnection con = new SqlConnection(cs))
{
SqlCommand cmd = new SqlCommand("spGetCountries", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter paramName = new SqlParameter()
{
ParameterName = "@Name",
Value = term
};
cmd.Parameters.Add(paramName);
con.Open();
SqlDataReader rdr = cmd.ExecuteReader();
while(rdr.Read())
{
Country country = new Country();
country.Id = Convert.ToInt32(rdr["Id"]);
country.Name = rdr["Name"].ToString();
country.FlagPath = rdr["FlagPath"].ToString();
countries.Add(country);
}
}
JavaScriptSerializer js = new JavaScriptSerializer();
Context.Response.Write(js.Serialize(countries));
}
}
}
jQuery Code
<!DOCTYPE html>
<html xmlns="www.w3.org/1999...">
<head runat="server">
<title></title>
<script src="jquery-1.11.2.js"></script>
<script src="jquery-ui.js"></script>
<link href="jquery-ui.css" rel="stylesheet" />
<script type="text/javascript">
$(document).ready(function () {
$('#txtName').autocomplete({
minLength: 1,
source: function (request, response) {
$.ajax({
url: 'CountryService.asmx/GetCountries',
method: 'post',
data: { term: request.term },
dataType: 'json',
success: function (data) {
response(data);
},
error: function (err) {
alert(err);
}
});
},
focus: updateTextBox,
select: updateTextBox
})
.autocomplete('instance')._renderItem = function (ul, item) {
return $('<li>')
.append("<img class='imageClass' src=" + item.FlagPath + " alt=" + item.Name + "/>")
.append('<a>' + item.Name + '</a>')
.appendTo(ul);
};
function updateTextBox(event, ui) {
$(this).val(ui.item.Name);
return false;
}
});
</script>
<style>
.imageClass {
width: 16px;
height: 16px;
padding-right: 3px;
}
</style>
</head>
<body style="font-family: Arial">
<form id="form1" runat="server">
Country Name : <input id="txtName" type="text" />
</form>
</body>
</html>

Пікірлер: 13
@roadtripping
@roadtripping Жыл бұрын
You didn't have to close the with ?
@raman-jn6yt
@raman-jn6yt 6 жыл бұрын
well explained but one question. When you created updateTextBox(event, ui) function and called it from 'focus' and 'select' event, you didn't pass arguments then how it was able to get the 'event' and 'ui' parameters?
@nbtion
@nbtion 7 жыл бұрын
This is best tutorial for jQuery Step by Step (Thank)
@aalmohalla391
@aalmohalla391 9 жыл бұрын
Yessss, finally with images thank u so much.
@CodingSquid
@CodingSquid 6 жыл бұрын
Nice example! Is to possible to store/pull the image from the database? Let me guess, absolutely! Many thanks.
@dosovi4548bustayes
@dosovi4548bustayes 9 жыл бұрын
Thank you for your videos
@josephregallis3394
@josephregallis3394 6 жыл бұрын
Venkat, when do you use a 'post' and when do you use a 'get'? I get confused on this and I haven't heard a good explanation from you on how to choose these.
@Csharp-video-tutorialsBlogspot
@Csharp-video-tutorialsBlogspot 6 жыл бұрын
Hello Joseph - In addition to GET and POST. We also have PUT and DELETE. The following is the general rule for using these HTTP verbs GET - To retrieve data POST - To create a new item PUT - To update an item DELETE - To delete an item
@josephregallis3394
@josephregallis3394 6 жыл бұрын
OK. Thanks.
@mehmetakifvurucu7940
@mehmetakifvurucu7940 7 жыл бұрын
firstly thanks this video. ı have a question. this app run on localhost but not running hosting
@muhammadrehbarsheikh8498
@muhammadrehbarsheikh8498 9 жыл бұрын
Thanks venkat sir !
@sosoabashidze6880
@sosoabashidze6880 4 жыл бұрын
Can I use it in Wordpress?
@JediPhantom
@JediPhantom 9 жыл бұрын
that was complicated stuff...
Cascading dropdownlist using jquery and asp net
29:12
kudvenkat
Рет қаралды 63 М.
Autocomplete textbox using jquery in asp net
17:30
kudvenkat
Рет қаралды 87 М.
Or is Harriet Quinn good? #cosplay#joker #Harriet Quinn
00:20
佐助与鸣人
Рет қаралды 58 МЛН
отомстил?
00:56
История одного вокалиста
Рет қаралды 6 МЛН
Touching Act of Kindness Brings Hope to the Homeless #shorts
00:18
Fabiosa Best Lifehacks
Рет қаралды 19 МЛН
No, Einstein Didn’t Solve the Biggest Problem in Physics
8:04
Sabine Hossenfelder
Рет қаралды 289 М.
asp net generic handler return json
19:28
kudvenkat
Рет қаралды 57 М.
Autocomplete textbox using jquery and asp net web service
13:58
Postgres just got even faster
26:42
Hussein Nasser
Рет қаралды 24 М.
jQuery UI Autocomplete: Highlight Matching Text in jQuery UI Autocomplete
18:31
jQuery datatables get data from database table
21:04
kudvenkat
Рет қаралды 282 М.
Or is Harriet Quinn good? #cosplay#joker #Harriet Quinn
00:20
佐助与鸣人
Рет қаралды 58 МЛН