Autocomplete using Html, JSON, MVC, jQuery

Filed in ASP.NET MVC | C# | jquery 6 Comments

I found an awesome AutoComplete tutorial (here: http://www.nodstrum.com/2007/09/19/autocompleter/#comment) using PHP, jQuery, MySQL, and decided to port it over to Microsoft’s ASP.NET MVC. Turned out to be way easier than I thought! You can download the auto-complete code sample here.

Configure MVC Controller

So this is basic: create a JsonResult method in your controller that runs a query. I’m using nHibernate and ActiveRecord to perform a top 10 query as you can see here:

public JsonResult GetCountry(string searchString)
{
StringBuilder sb = new StringBuilder();
ICriterion[] query = { Expression.Eq("Code", "COUNTRYLIST"), Expression.Like("Value",searchString, MatchMode.Start) };
LookupCode[] countries = LookupCode.SlicedFindAll(0, 10, query);
for (int i = 0; i < countries.Length; i++)
{
sb.Append("<li onClick=\"fill('" + countries[i].Value + "');\">" + countries[i].Description + "</li>");
}
return Json(sb.ToString());
}

Create jQuery Function

There are two jQuery functions we need. The first function simply calls the GetCountry method:

function lookup(inputString) {
$.post("/LookupCode/GetCountry", { searchString: "" + inputString + ""},
function(data) {
var myObject = eval('(' + data + ')');
if (data.length > 0) {
$("#suggestions").show();
$( "#autoSuggestionsList").html('<ul>' + myObject + '</ul>');
}
});
}

The second method is called from the auto-suggest list, and its job is to populate the selected value into the form field and to hide the auto-suggest list:

function fill(thisValue) {
$("#Country").val(thisValue);
setTimeout("$("#suggestions").hide();", 200);
}

Prep Textbox

There are two things we need to do to configure the textbox to trigger the auto-complete functionality:

1. Add the onkeyup=”lookup(this.value);” attribute within the input:

<input type="text" name="Country" id="Country"  onkeyup="lookup(this.value);"  />

2. Add a DIV to hold the suggestion list:

<div class="suggestionsBox" id="suggestions" style="display: none;">
<div class="suggestionList" id="autoSuggestionsList">
</div>
</div>

That’s it! For extra credit, we can add some style in the next step.

Add Style

The following style was taken and modified from the original tutorial (here: http://www.nodstrum.com/2007/09/19/autocompleter/#comment) , and is really easy to customize:

.suggestionsBox {
position: relative;
left: 5px;
margin: 10px 0px 0px 0px;
width: 200px;
background-color: #e4e4e4;
-moz-border-radius: 7px;
-webkit-border-radius: 7px;
border: 2px solid #336699;
color:#333333;
}

.suggestionList {
margin: 0px;
padding: 0px;
}

.suggestionList ul {
margin: 0px;
padding: 0px;
}

.suggestionList li {
list-style-type: none;
margin: 0px 0px 0px 0px;
padding: 3px;
cursor: pointer;
}

.suggestionList li:hover {
list-style-type: none;
color:White;
background-color: #336699;
}
<div id="_mcePaste" style="overflow: hidden; position: absolute; left: -10000px; top: 106px; width: 1px; height: 1px;">function lookup(inputString,prefix) {
if (inputString.length == 0) {
$('#suggestions').hide();
} else {
$.post("/LookupCode/GetCountry", { id: "" + inputString + "", prefix: prefix },
function(data) {

var myObject = eval('(' + data + ')');
var suggestlist = "#" + prefix + "autoSuggestionsList";
var suggestfield = "#" + prefix + "suggestions";

if (data.length > 0) {

$(suggestfield).show();
$(suggestlist).html('<ul>' + myObject + '</ul>');
}
});
}
}
</div>

, , , , , ,

How to use jQuery to copy form values

Filed in Html | jquery Leave a comment

This is a simple little exercise to show how to copy form values from one set of fields into another. The sample will be a “check box if billing information is the same as contact information.”

To set this up, first we create the form fields:

<fieldset>
<legend>Contact Information</legend>
<b>Name</b> (first, last)<br>
<input id="ContactFirstName" name="ContactFirstName" style="width: 100px;" value="" type="text">&amp;nbsp;
<input id="ContactLastName" name="ContactLastName" style="width: 120px;" value="" type="text">
<br><br>

<b>Address</b><br>
<input id="ContactAddress1" name="ContactAddress1" style="width: 150px;" value="" type="text"><br><br>
<input id="ContactAddress2" name="ContactAddress2" style="width: 150px;" value="" type="text">
<br><br>

<b>City, State</b><br>

<input id="ContactCity" name="ContactCity" style="width: 100px;" value="" type="text">,&amp;nbsp;
<select id="ContactState" name="ContactState"><option value="AK">Alaska</option>
</select>
<br><br>

<b>Zip Code</b><br>
<input id="ContactZip" name="ContactZip" style="width: 70px;" value="" type="text">
</fieldset><br/>

<fieldset>
<legend>Billing Information</legend>
<input onclick="CopyContactInfo()" type="checkbox"> Check box if same as Contact Information
<b>Name</b> (first, last)<br>
<input id="MailingFirstName" name="MailingFirstName" style="width: 100px;" value="" type="text">&amp;nbsp;
<input id="MailingLastName" name="MailingLastName" style="width: 120px;" value="" type="text">
<br><br>

<b>Address</b><br>
<input id="MailingAddress1" name="MailingAddress1" style="width: 150px;" value="" type="text"><br><br>
<input id="MailingAddress2" name="MailingAddress2" style="width: 150px;" value="" type="text">
<br><br>

<b>City, State</b><br>

<input id="MailingCity" name="MailingCity" style="width: 100px;" value="" type="text">,&amp;nbsp;
<select id="MailingState" name="MailingState"><option value="AK">Alaska</option>
</select>
<br><br>

<b>Zip Code</b><br>
<input id="MailingZip" name="MailingZip" style="width: 70px;" value="" type="text">
</fieldset>

Next we paste in the jquery code:

function CopyContactInfo() {
$("#BillingAddress1").val($("#ContactAddress1").val());
$("#BillingAddress2").val($("#ContactAddress2").val());
$("#BillingCity").val($("#ContactCity").val());
$("#BillingState").val($("#ContactState").val());
$("#BillingZip").val($("#ContactZip").val());
}

That’s it!

TOP