1

Please see my jsfiddle i've created to replicate my problem. http://jsfiddle.net/motocomdigital/22KFS/3/

In my project, I have created a table, which is sortable. I've used the jquery table sorter to do this which is a very simple jquery plugin. The table sorter is also included in the jsfidde. If you click the table headers, you will notice it sorts the table columns from descending to ascending.

This is fine, the problem I have is...

On my live website, the table is about 125 rows high. And I want to avoid paginating the table at all costs.

So my idea was to fix the table header upon scrolling with jQuery, which is nearly working. Please see my fiddle. The jQuery I've used to fix the header to the top of the pages works.

The problem is because the table header becomes fixed, the table shifts up a row, and makes it very glitchy.

Can anyone help me remove the glitchness, and stop it skipping a row when it reaches the top of window.

Would really appreciate any help thanks.


See jsfiddle here http://jsfiddle.net/motocomdigital/22KFS/3/


jQuery

var $window     = $(window),
    $tableHead  = $(".table-head"),
    offset      = $tableHead.offset();

$window.scroll(function() {
    if ($window.scrollTop() > offset.top) {
        $tableHead.addClass('fixed');
    } else {
        $tableHead.removeClass('fixed');
    }
});


CSS

.fixed {
   position: fixed;
   top: 0;
}


HTML

<table id="table-sorter" width="400" border="0" cellspacing="0" cellpadding="10">         
    <thead>
            <tr class="table-head">
                <th width="100" height="18" valign="middle">Number</th>
                <th width="100" height="18" valign="middle">First Name</th>
                <th width="100" height="18" valign="middle">Surname</th>
                <th width="100" height="18" valign="middle">System</th>
            </tr>
    </thead>
    <tbody>
            <tr>
                <td>1</td>
                <td>John</td>
                <td>Smith</td>
                <td>Wordpress</td>     
            </tr>

            ... ...

    </tbody>
</table>



1

1 Answer 1

3

Solved your problem. In case you hadn't realised already, the reason it jumps is because you are essentially removing the header row from the table when you make it position fixed, so the rest of the table jumps up to fill the gap.

The solution? Well, it's up for debate. To prove my point, all I've done is add a new temporary header when you make the actual header fixed. This keeps the table in place and allows the header to move down. When you scroll back up, that temporary header then gets removed.

Is this the best way? Not sure, you maybe want to clone the real header as a temporary solution, just in case of graphical glitches. This would also mean that in case of headers with multiple lines (therefore pushing the height down), the effect would be maintained as it's taking an exact clone.

Anyway, on with the code: http://jsfiddle.net/chricholson/22KFS/5/

$("#table-sorter").tablesorter({
 widgets: ['zebra']
});


var $window     = $(window),
    $tableHead  = $(".table-head"),
    offset      = $tableHead.offset();

$window.scroll(function() {


    if ($window.scrollTop() > offset.top) {
        $tableHead.addClass('fixed');
        if ($('#table-sorter thead tr.temp').length <= 0){
            $('#table-sorter thead').append('<tr class="temp"><td>hello</td></tr>');
        }
    } else {
        $tableHead.removeClass('fixed');
        $('tr.temp').remove();
    }


}); 

​ Good luck.

EDIT

Here's the working product using clone() http://jsfiddle.net/chricholson/22KFS/8/

Code:

$("#table-sorter").tablesorter({
     widgets: ['zebra']
});


var $window     = $(window),
    $tableHead  = $(".table-head"),
    offset      = $tableHead.offset();

$window.scroll(function() {


    if ($window.scrollTop() > offset.top) {
        $tableHead.addClass('fixed');
        if ($('#table-sorter thead tr.temp').length <= 0){
            $tableHead.clone().appendTo('#table-sorter thead').removeClass('fixed').addClass('temp');
        }
    } else {
        $tableHead.removeClass('fixed');
        $('tr.temp').remove();
    }


});    ​
6
  • Thats is clever. Its not masively smoother because the styles like you said are a little off, and I can't seem to fix with css. It's only off by a few pixels. Is it actually possible to clone stuff? That would awesome if I could clone the header and the sorting functionality worked. That way there would be any table movement. Thanks for your help :-)
    – Joshc
    Mar 9, 2012 at 16:51
  • You will have to forgive me, I'm on my phone so can't give you complete code. See here for jQuery clone api.jquery.com/clone. I think you will want to clone and then remove and add a temporary class/id, this will allow you to remove it later: $tableHead.clone().removeClass('fixed').addClass('temp'); This should clone it in the same place and change the class to temp. Use the same code from before to remove. As I say I'm on my phone so this isn't tested, you may have to change it slightly, use the link I gave you to help.
    – Chris
    Mar 10, 2012 at 9:34
  • Thanks Chris, I'm on iPhone too do will check this out in a few hours and get back to. I'm quite new to javascript and jquery, but it doesn't look too tricky.
    – Joshc
    Mar 10, 2012 at 15:05
  • Hi Josh, just tried it out with clone: jsfiddle.net/chricholson/22KFS/8 My line was slightly wrong, should be $tableHead.clone().appendTo('#table-sorter thead').removeClass('fixed').addClass('temp'); I've also made the header text bigger so you can see that it works with odd shaped heights and such.
    – Chris
    Mar 12, 2012 at 12:27
  • awesome, just in time too. I check this out now. Thanks
    – Joshc
    Mar 12, 2012 at 13:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.