Displaying Row Details Tooltip on GridView using JQuery

by mosessaur 3/14/2008 1:10:36 PM

Continuing exploring JQuery! I decided to implement a feature to display some kind of details related to a row displayed on GridView. For example when displaying employees details on GridView some information might not fit in the GridView because it will make it huge and wide. These infomration can be diplayed as Tooltip. Or when you want to display a picture and some kind of formated text to be diplayed as tooltip when mouse hover on the image.

My sample here will show how to implement such feature using JQuery. Again, I'm using same data and designed presented in my previous posts:

  1. Building a grouping Grid with GridView and ASP.NET AJAX toolkit CollapsiblePanel.
  2. Building a grouping Grid with GridView and JQuery.

I'll display customer orders as tooltip. of course so much data to be diplayed as tooltip, but this is just demonstration [View Demo].

jquerytooltip

I'm not going to explore whole code, because most of it has been discussed on my previous posts. I'll focus only on the changes. Now, the sample based on nested GridViews, one master and the other one represents the details. The master data is displayed within a DIV element inside a TemplateField column of the GridView. The DIV element has a CSS class attribute with value "group":

   1: <div class="group">
   2: <span>
   3:<%#Eval("CustomerID")%> :<%#Eval("CompanyName")%> (<%#Eval("TotalOrders")%> Orders) </span>
   4: </div>

The details GridView is also wrapped with antoher DIV element. That DIV element is located just after the first DIV and has CSS class attribute value "order":

   1: <div class="order">
   2:     <asp:SqlDataSource ID="sqlDsOrders" runat="server" ConnectionString="<%$ ConnectionStrings:Northwind %>"
   3:         SelectCommand="SELECT [OrderID], [OrderDate], [RequiredDate], [Freight], [ShippedDate] FROM [Orders] WHERE ([CustomerID] = @CustomerID)">
   4:         <SelectParameters>
   5:             <asp:Parameter Name="CustomerID" Type="String" DefaultValue="" />
   6:         </SelectParameters>
   7:     </asp:SqlDataSource>
   8:     <asp:GridView Width="450PX" AutoGenerateColumns="false" CssClass="grid" ID="gvOrders" DataSourceID="sqlDsOrders"
   9:         runat="server" ShowHeader="true" EnableViewState="false">
  10:         <RowStyle CssClass="row" />
  11:         <AlternatingRowStyle CssClass="altrow" />
  12:         <Columns>
  13:             <asp:TemplateField ItemStyle-CssClass="rownum">
  14:                 <ItemTemplate>
  15:                     <%# Container.DataItemIndex + 1 %>
  16:                 </ItemTemplate>
  17:             </asp:TemplateField>
  18:             <asp:BoundField HeaderText="Order ID" DataField="OrderID" ItemStyle-Width="80px" />
  19:             <asp:BoundField HeaderText="Date Ordered" DataField="OrderDate" DataFormatString="{0:MM/dd/yyyy}"
  20:                 ItemStyle-Width="100px" />
  21:             <asp:BoundField HeaderText="Date Required" DataField="RequiredDate" DataFormatString="{0:MM/dd/yyyy}"
  22:                 ItemStyle-Width="110px" />
  23:             <asp:BoundField HeaderText="Freight" DataField="Freight" DataFormatString="{0:c}"
  24:                 ItemStyle-Width="50px" ItemStyle-HorizontalAlign="Right" />
  25:             <asp:BoundField HeaderText="Date Shipped" DataField="ShippedDate" DataFormatString="{0:MM/dd/yyyy}"
  26:                 ItemStyle-Width="100px" />
  27:         </Columns>
  28:     </asp:GridView>
  29: </div>

That was all about ASPX code. Moving now to JQuery and JavaScript code. JQuery has a wonderful easy syntax that let you pickup the elements you need with writing less code. For me now, I need when I hover over the row of GridView -practically the DIV element with class group-, the details GirdView -practically the DIV element with class order-should appear as tooltip. Means first I need to register onmouseover and onmouseout for the group DIV! This is very easy to be done with JQuery, Just one line of code. JQuery provide Selectors methods that enable you to get the element you want. In my case I want to get DIV elements inside the master GridView that has CSS class attribute equal to "group":

   1: function pageLoad(sender, args)
   2: {
   3:     //register events for mouseover and mouseout for group DIV.
   4:     $('div.group').hover(over,out);
   5: }

Note: I'm using AJAX Enabled Web Site, which means ScriptManager is placed in the page. I initialized/attached my events on pageLoad method which is know to ASP.NET AJAX. I did that because my data is placed inside UpdatePanel, and I enabled Paging on GridView. So everytime an AJAX Request is made the UpdatePanel will referesh and I need to reregister the events again. Best place to do that is OnLoad client event.

The only line that is wirtten in the code above is $('div.group').hover(over,out). This $('div.group') selecteor will get all DIV elements that has css Class attribute equal to "group". the hover method is used to attached JavaScript handlers for both mouseover "over" and mouseout "out". So as you might guessed I have 2 other Client JavaScript methods called over and out.

On the over method, the tooltip shown, on the out method the tooltip disapear. And both finds the DIV element declared just after the group DIV in order to show it or hide it. I had commented the code below to explain each line:

   1: function over(e)
   2: {
   3:     //$(this): refers to the element which fire the event, in our case the DIV element you hover on.
   4:     //offset(): Get the current offset (left/top) of the first matched element relative to the viewport
   5:     var offset = $(this).offset();
   6:     
   7:     //next([expr]): returns the very next sibling for each element.
   8:     //this case will return DIV element that wraps the Details GridView
   9:     var $next = $(this).next();
  10:     
  11:     //Set position for the tooltip element using css() method and passing the properties as JSON format.
  12:     $next.css({left:offset.left+$(this).width(), top:offset.top});
  13:     
  14:     //show the tooltip with fadeIn() effect
  15:     $next.fadeIn('normal');                   
  16: }
  17: function out(e)
  18: {
  19:     var $next = $(this).next();    
  20:     $next.fadeOut('normal');
  21: }

I guess that was all for this post. You can download the solution [GridViewMasterDetailTooltipDemo.rar (62.17 kb)] and explore the code. Also you can view the demo.

kick it on DotNetKicks.com

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: ,

ASP.NET | Client Side

Comments

3/15/2008 5:18:39 PM

mhinze.com

Pingback from mhinze.com

Links Today (2008-03-21)

mhinze.com

3/16/2008 8:27:37 AM

Paco

There is a better way to do this. This script throttles one of my cores to the max for a second. So it sucks.

Paco nl

3/16/2008 6:29:27 PM

mosessaur

Thank you Paco, and I would appreciate to if you could share your better way!

mosessaur eg

3/23/2008 12:38:07 PM

jeremy simmons

Beautiful css on the grid. Did you make it yourself or get it in a kit?

jeremy simmons us

3/24/2008 11:08:10 AM

mosessaur

Thank you! Actually Matt Berseth http://mattberseth.com/ should gain all credits for that.
You can read his post about it on: http://mattberseth.com/blog/2007/12/surrounding_your_data_table_an.html

mosessaur eg

3/25/2008 12:18:51 AM

jeremy simmons

Thanks for sharing the source Moses.
You've got some great articles on your blog. Keep it up!

jeremy simmons us

3/25/2008 9:48:23 AM

mosessaur

Thank you so much Jeremy! I'm trying to keep it up, wish me luck! Smile

mosessaur eg

5/29/2008 4:30:39 AM

Vlad

Nice post,
I don't think that the idea of showing details as a tooltip is really good in real life project, but as an example of some jQuery functionality and integration with GridView it is great.

By the way, it took me a couple of weeks to find a good way to integrate jQuery with UpdatePanel, so it is a shame I didn't find your article earlier...
Do you happen to know how to selectively register the events? For example, if there are 2 UpdatePanels, events will be registered only on the one that was updated.

Thank you for a great article Smile

Vlad il

6/1/2008 6:00:49 PM

mosessaur

Thank you Vlad for your comment! and sorry for the delay of my response, as my website was down.
Regarding your question at the end of your comment, I didn't really understand it! could you please give me more explanation.

And about tooltips Actually it has a good usage. For example in ASP.NET Website, it is used in learning section to display description about each video lesson. check it out. It is just my example didn't show a real business usage.

mosessaur eg

6/4/2008 9:48:56 PM

Sgt13Echo

Moses,

Don't worry about Paco, he is just an angry man and is taking it out on you. I like what you have done and you took time to make it look really good and not enough developers make that extra effort.

God Bless
Sgt13Echo

Sgt13Echo us

6/5/2008 11:04:25 AM

Vlad

Hi again,
I can see why you find it hard to understand my question...
First, the only relation between my question and this post is the fact that you use pageLoad instead of the regular $(document).ready() because you have updatpanel on the page.
Now I will try and find some example for my problem:
Lets say you have 2 UpdatePanels (up1 and up2) and in up2 you have some text in which, using jQuery, you put <b> tag around your name Smile
The first time the page loads you will get your name in bold, but what happens if after that only up1 will be updated? you will get "double" <b> tag around your name, because pageLoad runs each time when at least one updatepanel refreshes.
I hope you see my problem now.
It would be nice if you knew how to resolve it...
Thank you anyway for a great article and for replying to my comment.
Vlad

Vlad il

6/6/2008 4:59:37 AM

mosessaur

@Sgt13Echo: thank you Echo, for your comment. It is a free space where everyone can share his opinions and ideas.

@Vlad: I'll check it, and try to resolve it! I just check the comments now while I'm on harry. Beside the site was down for sometime.

Thank you all

mosessaur eg

7/4/2008 10:07:35 PM

Sethu Bharathi

Hi Moses,
I am very new to web development, i am using ur grid view master detail option in my project.
in my application, instead of mouse over i have an button in the next <div> after the detail grid view
when i click that button fade out will fired. its urgent .help me out

Thank you all

Sethu Bharathi in

7/5/2008 8:11:15 PM

mosessaur

Hello Sethu, sorry for delay, I'm currently out of town with limitted internet connection, and I just read your comment, I'll try to check it and get back to ASAP! But I'm not sure of my internet connection on the upcoming few days.
thanks

mosessaur eg

Add comment


(Will show your Gravatar icon)  

  Country flag

biuquote
  • Comment
  • Preview
Loading



Powered by BlogEngine.NET 1.4.0.0
Theme by Mads Kristensen

About the author

Name of author
mosesofegypt logo
Muhammad M. Mosa Soliman
Software Engineer.
MCT, MCSD.NET,
MCTS: .Net 2.0 Web, Windows, Distributed Applications
MCTS: WSS 3.0, MOSS 2007 Configuration & App Dev
MCPD: Enterprise Application Developer

E-mail me Send mail | Live Space My Live Space


Calendar

<<  July 2008  >>
MoTuWeThFrSaSu
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910

View posts in large calendar

Recent Comments

Comment RSS

Community Credit

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2008