April 2010 JQuery Goodness Roundup

Here it is April and that means it is time for another roundup of jQuery plugins that will greatly assist you in making short work of your development efforts while at the same time pleasing the end user. I have noticed that there are quite a few of new jQuery plugins out there and they really caught my attention as well as the proven older plugins that I have found useful. If you have a favorite plugin why not leave a comment and share wit the community?

TextBoxes and DropDowns

dropdown

  1. FlexBox (CodePlex)
  2. JQuery DropDown

Drag and Drop

drag drop panel

  1. JQuery List DragSort
  2. Table Drag and Drop JQuery plugin
  3. Collapsible Drag & Drop Panels Using jQuery
  4. JQuery Iconize Plugin

Menus

context menu

  1. ASP.NET Multi-Level Drop Down Menu
  2. QuickSand
  3. Horizontal Accordion
  4. Jeegoocontext
  5. jsTree
  6. jBreadCrumb

LightBox

lightbox

  1. JQuery LightBox
  2. prettyPhoto
  3. ThickBox

Animation

  1. Spiritely
  2. Page Curls
  3. S3 Slider

Charts and Graphs

  1. JQuery Visualize

Date and Time

date range picker

  1. jMonthCalendar
  2. Date Range Picker
  3. DateJs

How To Process JSON With C# and JQuery

JavaScript Object Notation affectionately known as JSON is a wonderful way to deliver content to the browser in a lightweight method that can both save on bandwidth and reduce page weight. Who doesn’t like the idea of performance improvements?

In this tutorial, I will demonstrate how to make a call to a SQL Server database via a Web Method and create an employee object that will hold the top ten employees. This object will contain the JSON data that in turn will be displayed in a standard HTML table. Of course you could use CSS to display the data and reduce the need for a table.

The first step is to create the Employee class that defines the fields, properties, and constructors.

///
/// Summary description for Employee
///
public class Employee
{
private string _firstName;
private string _middleName;
private string _lastName;

public string FirstName
{
get { return _firstName; }
set { _firstName = value; }
}

public string LastName
{
get { return _lastName; }
set { _lastName = value; }
}

public string MiddleName
{
get { return _middleName; }
set { _middleName = value; }
}

public Employee(string firstName, string middleName, string lastName)
{
_firstName = firstName;
_middleName = middleName;
_lastName = lastName;
}
}

Now that we have modeled the Employee object we need to establish a web method that will communicate with the back end database. The method is named GetTopTenEmployees and the method will return a list of employees. Take note of the helper method that is engaged to return a DataTable object to the GetTopTenEmployees caller which resides in the code behind of the ASPX web form.

///
/// Gets the top ten employees.
///
/// Employee List
[WebMethod]
public static List GetTopTenEmployees()
{
const string query = "SELECT TOP 10 [FirstName], [MiddleName], [LastName] FROM [AdventureWorks].[Person].[Contact]";
DataTable dt = BuildDataTable(query);
List empList = null;

if (dt != null)
{
empList = new List();

for (int i = 0; i < dt.Rows.Count; i++)
{
empList.Add(new Employee(dt.Rows[i]["FirstName"].ToString(),
dt.Rows[i]["MiddleName"].ToString(),
dt.Rows[i]["LastName"].ToString()));
}
}

return empList;
}

///
/// Builds the data table.
///
/// The query. /// DataTable
public static DataTable BuildDataTable(string query)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Thor"].ConnectionString);
SqlDataAdapter ada = new SqlDataAdapter(query, con);
DataTable dt = new DataTable();
ada.Fill(dt);
return dt;
}

Since I am employing JQuery to make the Ajax call be sure to add a reference to the JQuery library either locally or the Content Delivery Network (CDN). Next we need to functions, the first will make the call to the web method and the second will process the JSON data and build a table along with the employee data and write the table out to the web form.

<script src="jquery-1.4.2.min.js" type="text/javascript"></script>

<script type="text/javascript">// <![CDATA[
$(document).ready(function() {
$.ajax({
type: "POST",
url: "Default.aspx/GetTopTenEmployees",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
BuildTable(msg.d);
}
});
});
function BuildTable(msg) {
var table = '
<table>
<thead>
<tr>
<th>First Name</th>
<th>Middle Name</th>
<th>Last Name</th>
</thead>
<tbody>';
for (var i = 0, l = msg.length; i < l; i++)
{
var person = msg[i];
var row = '
<tr>';
row += '
<td>' + person.FirstName + '</td>

';
row += '
<td>' + person.MiddleName + '</td>

';
row += '
<td>' + person.LastName + '</td>

';
row += '</tr>

';
table += row;
}
table += '</tbody>
</table>

';
$('#Container').html(table);
}
// ]]></script>
<div id="Container">

Now fire up your web browser and the result will reflect the following:

json employee result

Not bad at all and the complexity level here is low. In fact if you use a HTTP sniffer you will see the following content being returned.

{"d":[{"__type":"Employee","FirstName":"Gustavo","LastName":"Achong","MiddleName":""},
{"__type":"Employee","FirstName":"Catherine","LastName":"Abel","MiddleName":"R."},
{"__type":"Employee","FirstName":"Kim","LastName":"Abercrombie","MiddleName":""},
{"__type":"Employee","FirstName":"Humberto","LastName":"Acevedo","MiddleName":""},
{"__type":"Employee","FirstName":"Pilar","LastName":"Ackerman","MiddleName":""},
{"__type":"Employee","FirstName":"Frances","LastName":"Adams","MiddleName":"B."},
{"__type":"Employee","FirstName":"Margaret","LastName":"Smith","MiddleName":"J."},
{"__type":"Employee","FirstName":"Carla","LastName":"Adams","MiddleName":"J."},
{"__type":"Employee","FirstName":"Jay","LastName":"Adams","MiddleName":""},
{"__type":"Employee","FirstName":"Ronald","LastName":"Adina","MiddleName":"L."}]}

As you can see we have the type of the Employee object along with the properties and values. I hope you find this tutorial beneficial and there are a number of different approaches you may take to achieve the same result. Determine what works best for you and make it your own. Be sure to take advantage of the free white paper titled Simply JavaScript which is packed with full-color examples, Simply JavaScript is all you need to start programming in JavaScript the right way.

Display Images With JQuery And A Standard HTML Select Tag

jQuery is a coding language that is a branch off of JavaScript. jQuery works like JavaScript in which its used to help with interaction and effects with your development code. The best featuring for jQuery is the effects you can accomplish, with less code than what it would take with JavaScript. Most common jQuery effects are drop down menus, drag and drop elements, animations and form validation. Developers have also connected this with other coding languages like JSP, ASP, PHP and CGI.

While JQuery has been around for a while I have not had the time to spend a great deal of time in this area. It seems technology changes entirely to quick and it is a never ending process of learning. With that said, I thought I would being looking at a basic example of displaying an image based upon the selection made from the end user. In this example I will use only a select tag and div element which contains the related image reflected from the select tag.

To begin we need to reference the JQuery library and write the function that fires upon a select change event to display the associated image.

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script>

<script type="text/javascript">// <![CDATA[
$(document).ready(function(){
$('#book0').hide();
$('#book1').hide();
$('#book2').hide();
$("#booklisting").change(function(){

if(this.value == 'all')
{$("#books").children().show();}
else
{$("#" + this.value).show().siblings().hide();}
});

$("#booklisting").change();
});
// ]]></script>

Now comes the easy part, all that remains is the HTML markup.

<select id="booklisting"> <option selected="selected" value="book0">&lt;-- Select --&gt;</option> <option value="book1">Book One</option> <option value="book2">Book Two</option> <option value="all">All Books</option> </select>
<div id="books">
<div id="book1"><img src="books1.gif" alt="" width="200" height="200" /></div>
<div id="book2"><img src="books2.gif" alt="" width="200" height="200" /></div>
</div>

That is all it takes, no fuss and no muss. upon loading and executing the page the following is the result depending upon the user’s action.

jquery example

March JQuery Goodness Roundup

I haven’t done a roundup for jQuery plugins before so I thought it was long overdue. I have noticed that there are quite a few of new jQuery plugins out there and they really caught my attention as well as the proven older plugins that I have found useful. If you have a favorite plugin why not leave a comment and share wit the community?

Tabs

tabs

  1. JQuery Tools: Tabs Done Right
  2. TabContainer
  3. jQuery UI Tabs
  4. Bookmarkable Tabs
  5. Tabs Made Easy

ToolTips

jtoolstip

  1. JQuery Tools: Tooltips Done Right
  2. The JQuery Tooltip
  3. qTip
  4. ClueTip
  5. AJAX Tooltip

Sliders

slider

  1. JQuery Tools: Scrollable
  2. AnythingSlider
  3. Featured Content
  4. Moving Boxes
  5. Easy Slider

Menus

dockmenu

  1. Simple JQuery Accordion menu
  2. Sexy Dropdown Menu
  3. jqDock
  4. DockMenu
  5. Apple Style Menu

Tables

flexgrid

  1. Tablesorter
  2. DataTables
  3. Fixed Header Tables
  4. Flexgrid

Snazzy File Uploading With .NET and JQuery

I must admit that more often than not when I run across an option to upload files the interface leaves a lot to be desired. Why is this? Should end users not be presented with eye candy as well as immediate feedback when it comes to uploading data?

Simply JavaScript – Free 150 Page Preview! Learn how easy it is to use JavaScript to solve real-world problems, build smarter forms, track user events (such as mouse clicks and key strokes), and design eye-catching animations. Then move on to more powerful techniques using the DOM and Ajax. World-renowned authors, Kevin Yank and Cameron Adams have used their exquisite skills and in-depth knowledge of JavaScript to deliver a book that teaches JavaScript with unprecedented clarity.

Thanks to JQuery and JQuery UI you can now provide snazzy interfaces for your file upload mechanism with very little effort. Thanks to Benj and Ronnie for Uploadify which a slick JQuery plug-in a great deal of the work has been accomplished and all that remains is the implementation. While my example does not use the latest release of this plug-in you should be able to adapt any required changes fairly easy. If you recall earlier I mentioned JQuey UI, jump over there and build a custom download that fits into your user interface. Right now there are 24 themes or you can create your very own should you feel the need to do so.

Now it is time to begin putting the pieces of the puzzle together. Create an ASPX and bring in the appropriate JavaScript and CSS files.

<link type="text/css" href="CSS/uploadify.css" rel="Stylesheet" />
<link type="text/css" href="CSS/ui-lightness/jquery-ui-1.7.2.custom.css" rel="stylesheet" />
<script type="text/javascript" src="scripts/jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="scripts/jquery.uploadify.js"></script>
<script type="text/javascript" src="scripts/jquery-ui-1.7.2.custom.min.js"></script>

Once you completed this step it is now time to wire up the HREF tag that will open a snazzy dialog. For example:

<form id="form1" runat="server">
<p>
<a href="#" id="dialog_link"><span>
</span>Open File Upload Dialog</a></p>
<!-- file upload-dialog -->
<div id="fileManagerDialog" title="File Upload Manager">
<a href="javascript:$('#<%=FileUpload1.ClientID%>').fileUploadStart()">Start Upload</a>&amp;amp;amp;amp;nbsp;
|&amp;amp;amp;amp;nbsp;<a href="javascript:$('#<%=FileUpload1.ClientID%>').fileUploadClearQueue()">Clear</a>
<div style="padding: 40px">
<asp:FileUpload ID="FileUpload1" runat="server" />
</div>
</div>
</form>

You will notice that the HREF tag that opens the file dialog has an ID of “dialog_link” and a DIV tag has an ID of “fileManagerDialog”. The associated JavaScript with these two tags is:

<script type="text/javascript">
$(function () {
// Dialog
$('#fileManagerDialog').dialog({
autoOpen: false,
width: 600,
buttons: {
"Close": function () {
$(this).dialog("close");
}
}
});

// Dialog Link
$('#dialog_link').click(function () {
$('#fileManagerDialog').dialog('open');
return false;
});

//hover states on the static widgets
$('#dialog_link, ul#icons li').hover(
function () { $(this).addClass('ui-state-hover'); },
function () { $(this).removeClass('ui-state-hover'); }
);
});
</script>

The above JavaScript performs the opening, closing and styling. The end result is:

snazzy uploads
Note: The file Upload Manager only appears when the Open File Upload Dialog is clicked.

Here is where the beauty of Uploadify comes in. As the end user selects files they are displayed as such:

uploadifyscreen

From here the end user can perform the following actions:

  • Start upload
  • Clear selected uploads
  • Clear single upload
  • Close the dialog

It is import to note the configuration for Uploadify, be sure to review the documentation. Here is an example:

<script type="text/javascript">
$(window).load(
function () {
$("#<%=FileUpload1.ClientID%>").fileUpload({
'uploader': 'scripts/uploader.swf',
'cancelImg': 'images/cancel.png',
'buttonText': 'Browse Files',
'script': 'Upload.ashx',
'folder': 'uploads',
'fileDesc': 'Image Files',
'fileExt': '*.jpg;*.jpeg;*.png',
'multi': true,
'auto': false
});
}
);
</script>

Download Example (Note: The solution was created in VS.NET 2010 Beta 2)

Another useful benefit of the use of JQuery UI is the File Upload Manage Dialog can be both re-sized and dragged within the browser. I hope that you find this example useful and I would be interested in what other products you have used when it comes to file uploads, just leave a comment stating what you have found works best in your situation.

Pages:12»