Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can i make sorting arrows separate? #601

Closed
ghost opened this issue May 1, 2014 · 3 comments
Closed

How can i make sorting arrows separate? #601

ghost opened this issue May 1, 2014 · 3 comments

Comments

@ghost
Copy link

ghost commented May 1, 2014

I'd like to click the "Up arrow" and the column to sort in Asc order, click "Down arrow" and column to sort in Desc order. If it's already sorted that way then nothing happens.

How do i go about it without using additional widgets?

@Mottie
Copy link
Owner

Mottie commented May 1, 2014

Hi @telegraphinho!

It takes a little bit of extra code & css, but it's doable! (demo):

CSS:

.tablesorter .tablesorter-header {
    background-image: none;
    cursor: default;
}
.tablesorter-header span, .tablesorter .arrows {
    cursor: pointer;
}
.tablesorter-header .arrows {
    position: relative;
    float: right;
}
.tablesorter-header div.arrows i {
    position: absolute;
    right: 0;
    display: block;
    width: 15px;
    height: 15px;
    background-position: center center;
    background-repeat: no-repeat;
    background-color: transparent;
}
.tablesorter-header i.tablesorter-headerAsc {
    top: -5px;
    background-image: url(http://mottie.github.io/tablesorter/css/images/black-asc.gif);
}
.tablesorter-header i.tablesorter-headerDesc {
    top: 5px;
    background-image: url(http://mottie.github.io/tablesorter/css/images/black-desc.gif);
}
.tablesorter-headerAsc i.tablesorter-headerAsc {
    background-image: url(http://mottie.github.io/tablesorter/css/images/white-asc.gif);
}
.tablesorter-headerDesc i.tablesorter-headerDesc {
    background-image: url(http://mottie.github.io/tablesorter/css/images/white-desc.gif);
}

Script:

$(function () {
    var $table = $('table');

    $table.tablesorter({
        theme: 'blue',
        headerTemplate: '<span>{content}</span>' +
            '<div class="arrows">' +
                '<i class="tablesorter-headerAsc"></i>' +
                '<i class="tablesorter-headerDesc"></i>' +
            '</div>',
        onRenderHeader: function (index) {
            $(this).find('.arrows i').click(function(){
                var direction = $(this).hasClass('tablesorter-headerAsc');
                $table.trigger('sorton', [ [[ index, direction ? 0 : 1 ]] ]);
            });
        },
        // you can still click on the header text to toggle the sort
        selectorSort: 'span'
    });
});

In this demo, the only limitation is that you can't do a multi-column sort using only the arrows, but you can if you shift-click on the header text.

@ghost
Copy link
Author

ghost commented May 1, 2014

Thanks a bunch, @Mottie!!!

That's a really elegant solution!

@Mottie
Copy link
Owner

Mottie commented May 6, 2014

The demo was added under "Sorting" on the Home wiki page.

@Mottie Mottie closed this as completed May 6, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant