Displaying Row Details Tooltip on GridView using JQuery

by mosessaur| 14 March 2008| 24 Comments

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

Comments (24) -

Paco
Paco Netherlands on 3/12/2008 11:27 AM There is a better way to do this. This script throttles one of my cores to the max for a second. So it sucks.
mosessaur
mosessaur Egypt on 3/12/2008 9:29 PM Thank you Paco, and I would appreciate to if you could share your better way!
jeremy simmons
jeremy simmons United States on 3/19/2008 3:38 PM Beautiful css on the grid. Did you make it yourself or get it in a kit?
mosessaur
mosessaur Egypt on 3/20/2008 2:08 PM Thank you! Actually Matt Berseth http://mattberseth.com/ should gain all credits for that.
You can read his post about it on: mattberseth.com/.../...ing_your_data_table_an.html
jeremy simmons
jeremy simmons United States on 3/21/2008 3:18 AM Thanks for sharing the source Moses.
You've got some great articles on your blog. Keep it up!
mosessaur
mosessaur Egypt on 3/21/2008 12:48 PM Thank you so much Jeremy! I'm trying to keep it up, wish me luck! Smile
Vlad
Vlad Israel on 5/25/2008 7:30 AM 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
mosessaur
mosessaur Egypt on 5/28/2008 9:00 PM 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.
Sgt13Echo
Sgt13Echo United States on 6/1/2008 12:48 AM 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
Vlad
Vlad Israel on 6/1/2008 2:04 PM 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
mosessaur
mosessaur Egypt on 6/2/2008 7:59 AM @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
Sethu Bharathi
Sethu Bharathi India on 7/1/2008 1:07 AM 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
mosessaur
mosessaur Egypt on 7/1/2008 11:11 PM 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
mosessaur Egypt on 7/5/2008 9:02 PM @Sethu Ok, I tried to simulate what you asked for and here is what I did:
I added a button  just after the details gird. I added class attribute to the input:
<input class="something" type="button" value="Details!" />

And modified the javascript as the following:
<script type="text/javascript">
   jQuery.fn.fadeToggle = function(speed, easing, callback) {
   return this.animate({opacity: 'toggle'}, speed, easing, callback);};
   function pageLoad(sender, args)
   {
       //register click event for every button with class 'something'.
       $('.something').click(somethingClicked);
   }
  
   function somethingClicked(e)
   {
       //fadeOut all details except the current one (Related to clicked button)
       $('.something').not(this).prev().fadeOut('normal');
      
       var offset = $(this).offset();      
       //Get Details and show it or hide it
       var $prev = $(this).prev();
       $prev.css({left:offset.left+$(this).width(), top:offset.top});                
       $prev.fadeToggle('normal');
   }
</script>


Notice that I added a jQuery blugin for fadeToggle, you check it out in more details at
blog.pengoworks.com/.../jQuery-fadeToggle-plugin
www.learningjquery.com/.../simple-effects-plugins

I hope it helps

Sethu Bharathi
Sethu Bharathi India on 7/15/2008 1:07 AM Thank you,
and one more thing is, the paging option is not working for the details grid view, i want to display only 25 rows, whether it is possible to do paging in details grid view. ijust cant access the details grid view in code behind ( i dont know the reason why ?)

Thank you

mosessaur
mosessaur Egypt on 7/15/2008 2:16 AM @Sethu Yes you should be able to do that! let me check it and I'll get back to you. I just need sometime, got some busy times these days.
Lloyd George
Lloyd George India on 10/8/2008 8:55 AM Hi Moses,Great artcile..,thanks for sharing.I would like to know how to implement paging if we are ajaxifying the grid using jquery.Do we need to  creat custom paging and implement our own logic..Sorry to disturb if you are busy
ahtisam
ahtisam United Kingdom on 12/5/2008 2:41 AM Great Stuff Dude .. keep it up
Thomas Holland
Thomas Holland United States on 1/4/2009 8:30 AM Thank you for the great tutorial!

jQuery is going to be the UI savior of .NET in my opinion.

I love it when people tell you something that you did "sucks" but don't offer a better way.

Thanks again!
Rahul Panwar
Rahul Panwar India on 2/28/2009 4:04 PM Hello mosessaur,
Its a nice article.
Now i am trying to achieve the same with a master grid view which has more then 1 column and i want the detail grid view on master grid row over.

can you please help me out..

Thanks
Rahul Panwar
Rahul Panwar
Rahul Panwar India on 3/2/2009 8:27 PM Hello mosessaur,
Its a nice article.
Now i am trying to achieve the same with a master grid view which has more then 1 column and i want the detail grid view on master grid row over.

can you please help me out..

Thanks
Rahul Panwar

Can you please suggest the solution for above?
Rahul Panwar
Rahul Panwar India on 3/16/2009 9:23 AM waiting for your reply..
mosessaur
mosessaur Egypt on 3/16/2009 9:41 AM @Rahul Panwar I need to try it out and build a sample. And these days I don't have enough time. So it will take sometime. I am sorry, But I recommend that you try resolve it and check out on Forums.
Gaurav
Gaurav India on 6/11/2009 1:30 PM Hi mosessaur,
i really liked your article. But have problem when add the same to my project. I have .Net 2.0 installed. When i run this sample it shows me Jquery error 'Sys is undefined'
at
Sys.WebForms.PageRequestManager._initialize('ScriptManager1', document.getElementById('form1'));
Kindly help me resolve this error

Pingbacks and trackbacks (3)+

Comments are closed