Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Chapter 8. Client Templating in jQuery > Using callback functions with jQuery t...

Using callback functions with jQuery templates

jQuery templates allow us to execute callback functions for further processing of the formatted contents before rendering it on the page. In this recipe, let's see how we can use the callback function to highlight alternate rows of a table. We will use the web services example described earlier and execute a callback function for each item in the returned response object.

Getting ready

  1. Add a new web form Recipe6.aspx to the current project.

  2. Add a css class to hide elements on the form:

    .hide
    {
    display:none;
    }
    
  3. Add a css class to highlight the background color of a row:

    .highlight
    {
    background-color:Silver;
    }
    
  4. Add a button control to the form to trigger the AJAX request. Also add the following markup to the form to render the data received from the web service.

    <form id="form1" runat="server">
    <div align="center">
    <asp:Button ID="btnSubmit" runat="server" Text="Click to retrieve Employee data" />
    <br /><br />
    <table border="1" cellspacing="2" cellpadding="2" id="contentTble">
    <tr class="header">
    <td>Employee ID</td>
    <td>Employee Name</td>
    <td>Email</td>
    <td>Contact</td>
    </tr>
    </table>
    </div>
    </form>
    
    
    					  
  5. Define an inline template to format the JSON data returned from the web service:

    <script type="text/x-jquery-tmpl" id="empTemplate">
    <tr>
    <td>${EmpID}</td>
    <td>${Name}</td>
    <td>${Email} </td>
    <td>${Contact}</td>
    </tr>
    </script>
    

  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint