+ Reply to Thread
Page 1 of 2
1 2 LastLast
Results 1 to 10 of 14

Thread: jQuery: Zebra striped table

  1. #1
    Code Warrior Brandon W is a name known to all Brandon W is a name known to all Brandon W is a name known to all Brandon W is a name known to all Brandon W is a name known to all Brandon W is a name known to all Brandon W's Avatar
    Join Date
    Sep 2008
    Location
    Australia
    Age
    16
    Posts
    4,824
    Blog Entries
    10

    jQuery: Zebra striped table

    G'day everyone. Welcome two my second tutorial on jQuery, this one is a follow up from the other one. The first tutorial was based around learning all
    the selectors that jQuery has to offer, but in this tutorial we will be using some of those selectors to style a table and add zebra stripes to it.

    Most would think, isn't CSS3 implementing this new feature anyway? To answer that yes it is. Then why are we using jQuery? We are using jQuery because
    it's so very simple to do something like this, and we all know how slow IE can be to implement these new CSS features

    I will not be explaining any of the HTML in this tutorial, some CSS I will make exceptions. But most of the CSS explanations will be in comments. I
    will explain ALL the jQuery, considering this is what it's based on.

    Enough chit chat, let's get started.

    So I have gone ahead and created a simple table of some CC members. I got all of them from the order of post count You can use whatever information
    your heard desires, but this was the easiest for me. Also, if you do decide to create your own. Keep it, the next tutorial will be based on sorting
    this data the quick way

    Here is the code for my table;
    Code:
    <table width "650">
                <
    thead>
                    <
    tr>
                        <
    th>
                            
    Name
                        
    </th>
                        <
    th>
                            
    Rank
                        
    </th>
                        <
    th>
                            
    Post Count
                        
    </th>
                        <
    th>
                            
    Avatar Rating
                        
    </th>
                    </
    tr>
                </
    thead>
                <
    tbody>
                    <
    tr>
                        <
    td>
                            
    Jordan
                        
    </td>
                        <
    td>
                            
    Administrator
                        
    </td>
                        <
    td>
                            
    12,447
                        
    </td>
                        <
    td>
                            
    8
                        
    </td>
                    </
    tr>
                    <
    tr>
                        <
    td>
                            
    Xav
                        
    </td>
                        <
    td>
                            
    Code Slinger
                        
    </td>
                        <
    td>
                            
    12,146
                        
    </td>
                        <
    td>
                            
    7
                        
    </td>
                    </
    tr>
                    <
    tr>
                        <
    td>
                            
    TcM
                        
    </td>
                        <
    td>
                            
    Code Warrior
                        
    </td>
                        <
    td>
                            
    8,450
                        
    </td>
                        <
    td>
                            
    9
                        
    </td>
                    </
    tr>
                    <
    tr>
                        <
    td>
                            
    WingedPanther
                        
    </td>
                        <
    td>
                            
    Super Moderator
                        
    </td>
                        <
    td>
                            
    5,079
                        
    </td>
                        <
    td>
                            
    7
                        
    </td>
                    </
    tr>
                    <
    tr>
                        <
    td>
                            
    chili5
                        
    </td>
                        <
    td>
                            
    Code Warrior
                        
    </td>
                        <
    td>
                            
    4,431
                        
    </td>
                        <
    td>
                            
    9
                        
    </td>
                    </
    tr>
                    <
    tr>
                        <
    td>
                            
    marwex89
                        
    </td>
                        <
    td>
                            
    Guru
                        
    </td>
                        <
    td>
                            
    3,995
                        
    </td>
                        <
    td>
                            
    6
                        
    </td>
                    </
    tr>
                    <
    tr>
                        <
    td>
                            
    John
                        
    </td>
                        <
    td>
                            
    Co-Administrator
                        
    </td>
                        <
    td>
                            
    3,762
                        
    </td>
                        <
    td>
                            
    10
                        
    </td>
                    </
    tr>
                    <
    tr>
                        <
    td>
                            
    Egz0N
                        
    </td>
                        <
    td>
                            
    Guru
                        
    </td>
                        <
    td>
                            
    3,674
                        
    </td>
                        <
    td>
                            
    9
                        
    </td>
                    </
    tr>
                    <
    tr>
                        <
    td>
                            
    amrosama
                        
    </td>
                        <
    td>
                            
    Code Warrior
                        
    </td>
                        <
    td>
                            
    3,283
                        
    </td>
                        <
    td>
                            
    7
                        
    </td>
                    </
    tr>
                    <
    tr>
                        <
    td>
                            
    v0id
                        
    </td>
                        <
    td>
                            
    Retired
                        
    </td>
                        <
    td>
                            
    2,697
                        
    </td>
                        <
    td>
                            
    5
                        
    </td>
                    </
    tr>
                    <
    tr>
                        <
    td>
                            
    MathXpert
                        
    </td>
                        <
    td>
                            
    Guru
                        
    </td>
                        <
    td>
                            
    2,263
                        
    </td>
                        <
    td>
                            
    8
                        
    </td>
                    </
    tr>
                    <
    tr>
                        <
    td>
                            
    mendim.
                        </
    td>
                        <
    td>
                            
    Guru
                        
    </td>
                        <
    td>
                            
    2,032
                        
    </td>
                        <
    td>
                            
    8
                        
    </td>
                    </
    tr>
                    <
    tr>
                        <
    td>
                            
    Brandon W
                        
    </td>
                        <
    td>
                            
    Guru
                        
    </td>
                        <
    td>
                            
    2,004
                        
    </td>
                        <
    td>
                            
    10
                        
    </td>
                    </
    tr>
                    <
    tr>
                        <
    td>
                            
    MikeM
                        
    </td>
                        <
    td>
                            
    Guru
                        
    </td>
                        <
    td>
                            
    1,494
                        
    </td>
                        <
    td>
                            
    10
                        
    </td>
                    </
    tr>
                </
    tbody>
            </
    table
    NOTE: If you do create your own information, make sure you create the thead and tbody tags. And in the thead section make sure you use the th tags.
    This can all be seen in my example. The table looks like this;


    As you can see this is quite boring. But we will improve that in the next section.

    Now that we have our basic table, we have to add some "style" to it. Let's create two classes, both of them for our table rows. I am going to
    call mine "over" and "alt". DO NOT go through the table and add those two classes to every table row! jQuery will make it very easy for us Let's
    also give our table a class, I named mine zebra.

    Now let's add some shnazzy CSS to the classes we created. You won't quite understand what they will do yet, so you can just copy mine now and after
    you can come back and change it to the way you like it. Since I know the outcome, I am going to make my table purple

    Code:
                thead tr th {
                    
    background-color#93008E;
                
    }
                
                
    tr {
                    
    background-color#FFADFC;
                
    }
                
                
    tr.alt td {
                    
    background-colorwhite;
                }
                
                
    tr.over tdtr:hover td {
                    
    background-color#A1009B;
                

    As you can see, if you used my code, we have a little hover effect going now which is great! We also have a good looking table, I love purple hehe.


    If you think this is a zebra yet, it looks colourful, but it's still not stripey jQuery will fix that

    So let's get started with the jQuery. Before we can use it, we must include the jQuery.js file into out page (link found in previous tutorial, link
    at bottom).

    Code:
    <script src="jQuery.js" type="text/javascript"></script> 
    Now it's included we can now start to use the power of it. Here is the first line of code we will be using.
    Code:
    $(document).ready(function() {

    }); 
    This code you will see many times during jQuery. What this does is, the $ stands for jQuery. Then we use a selector that will select the document.
    And when it is ready it will fun the function that is enclosed between the {}. So in basic, all this is saying is when the DOM (document) is ready
    do the code in the function. Which we will now add some code too it
    Code:
    $(document).ready(function() {
        $(
    ".zebra tr").addClass("alt");
    }); 
    This means; when the DOM has finished loading, use a selector to select all the elements with the class of zebra (the table) and all the tr elements
    inside that element with the class of zebra. This will select all the table row elements in our table. Now we want to do something to that selector.
    We will use a predefined class named addClass() which will then go through and add;
    Code:
    class = "alt" 
    To every element it had selected. But this doesn't solve our problem of making it striped, we must choose every second table row to stripe. How can
    we do this? If you read through the previous tutorial, we only have to add a filter to the tr element in the jQuery selection process.
    Code:
    $(document).ready(function() {
        $(
    ".zebra tr:even").addClass("alt");
    }); 
    Now in just three lines of jQuery we have a stripped table! As you have noticed, in the CSS there is a class mentioned, over. This will also be
    used in the next tutorial

    Hope you enjoy playing around with your newly created striped table. Have fun
    Final result;


    +rep if you liked.

    Regards,
    Brandon

    Tutorial 1 can be found here;
    Tutorials - CodeCall Programming Forum
    Last edited by Brandon W; 02-24-2009 at 03:18 PM.
    jQuery Selectors Tutorial - jQuery Striped Table tutorial - jQuery Events - jQuery Validation
    Sorry if I don't post as often as I did, I'll try to get here as much as possible! I'm working my bum off to get this scholarship and other stuff!

  2. #2
    Super Moderator WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther's Avatar
    Join Date
    Jul 2006
    Age
    36
    Posts
    11,697
    Blog Entries
    57

    Re: jQuery: Zebra striped table

    Nice job. Any reason why you didn't include a screenshot of the final result? +rep
    CodeCall Blog | CodeCall Wiki | Shareware
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  3. #3
    Code Warrior Brandon W is a name known to all Brandon W is a name known to all Brandon W is a name known to all Brandon W is a name known to all Brandon W is a name known to all Brandon W is a name known to all Brandon W's Avatar
    Join Date
    Sep 2008
    Location
    Australia
    Age
    16
    Posts
    4,824
    Blog Entries
    10

    Re: jQuery: Zebra striped table

    Aww poo. Sorry, I took a screenshot but forgot to upload. I am going to add it now.
    jQuery Selectors Tutorial - jQuery Striped Table tutorial - jQuery Events - jQuery Validation
    Sorry if I don't post as often as I did, I'll try to get here as much as possible! I'm working my bum off to get this scholarship and other stuff!

  4. #4
    Administrator Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan's Avatar
    Join Date
    Nov 2005
    Location
    Hendersonville, NC
    Posts
    24,556
    Blog Entries
    97

    Re: jQuery: Zebra striped table

    Nice job Brandon! +rep.
    BTW, I moved all of your tutorials to the JavaScript sub-forum.

  5. #5
    Code Warrior Brandon W is a name known to all Brandon W is a name known to all Brandon W is a name known to all Brandon W is a name known to all Brandon W is a name known to all Brandon W is a name known to all Brandon W's Avatar
    Join Date
    Sep 2008
    Location
    Australia
    Age
    16
    Posts
    4,824
    Blog Entries
    10

    Re: jQuery: Zebra striped table

    Thanks Jordan. OK thanks, I will post them in Javascript from now on.
    jQuery Selectors Tutorial - jQuery Striped Table tutorial - jQuery Events - jQuery Validation
    Sorry if I don't post as often as I did, I'll try to get here as much as possible! I'm working my bum off to get this scholarship and other stuff!

  6. #6
    Co-Administrator John is a glorious beacon of light John is a glorious beacon of light John is a glorious beacon of light John is a glorious beacon of light John is a glorious beacon of light John's Avatar
    Join Date
    Jul 2006
    Age
    21
    Posts
    5,885
    Blog Entries
    25

    Re: jQuery: Zebra striped table

    Very nice! Thorough and detailed.

  7. #7
    Code Warrior
    /////////|||||\\\\\\\\\
    amrosama is a splendid one to behold amrosama is a splendid one to behold amrosama is a splendid one to behold amrosama is a splendid one to behold amrosama is a splendid one to behold amrosama is a splendid one to behold amrosama is a splendid one to behold amrosama's Avatar
    Join Date
    Aug 2007
    Location
    Pyramids st, Giza, Egypt
    Age
    21
    Posts
    8,182
    Blog Entries
    12

    Re: jQuery: Zebra striped table

    nice job, ill rep when i get the chance
    i like jquery

  8. #8
    Code Warrior Brandon W is a name known to all Brandon W is a name known to all Brandon W is a name known to all Brandon W is a name known to all Brandon W is a name known to all Brandon W is a name known to all Brandon W's Avatar
    Join Date
    Sep 2008
    Location
    Australia
    Age
    16
    Posts
    4,824
    Blog Entries
    10

    Re: jQuery: Zebra striped table

    Thanks John and Amro. Mean a lot from great people like yourselves.
    jQuery Selectors Tutorial - jQuery Striped Table tutorial - jQuery Events - jQuery Validation
    Sorry if I don't post as often as I did, I'll try to get here as much as possible! I'm working my bum off to get this scholarship and other stuff!

  9. #9
    Guru mendim. is a jewel in the rough mendim. is a jewel in the rough mendim. is a jewel in the rough mendim. is a jewel in the rough mendim.'s Avatar
    Join Date
    Nov 2008
    Location
    Kosovo.
    Posts
    2,393

    Re: jQuery: Zebra striped table

    Nice tutorial .. I can't +rep now .. but i will :$

  10. #10
    Code Warrior Brandon W is a name known to all Brandon W is a name known to all Brandon W is a name known to all Brandon W is a name known to all Brandon W is a name known to all Brandon W is a name known to all Brandon W's Avatar
    Join Date
    Sep 2008
    Location
    Australia
    Age
    16
    Posts
    4,824
    Blog Entries
    10

    Re: jQuery: Zebra striped table

    Thanks mendim. It must be shared around, I haven't seen you write any tutorials?
    jQuery Selectors Tutorial - jQuery Striped Table tutorial - jQuery Events - jQuery Validation
    Sorry if I don't post as often as I did, I'll try to get here as much as possible! I'm working my bum off to get this scholarship and other stuff!

+ Reply to Thread
Page 1 of 2
1 2 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Similar Threads

  1. jQuery: Selectors
    By Brandon W in forum Javascript
    Replies: 8
    Last Post: 02-26-2009, 07:23 AM
  2. Replies: 0
    Last Post: 10-29-2008, 10:00 PM
  3. Layout problem with IE7
    By Alhazred in forum HTML Programming
    Replies: 2
    Last Post: 05-17-2008, 12:37 PM
  4. Tutorial: Advanced SQL
    By Jordan in forum Tutorials
    Replies: 2
    Last Post: 04-07-2008, 03:18 PM
  5. Mysql/Php help need
    By mittalmak in forum PHP Forum
    Replies: 17
    Last Post: 01-12-2008, 12:36 PM

Bookmarks

Bookmarks

     
        Algorithms and Data Structures

        Java tutorials

        Algorithms Forum

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts