Sorting - Using external buttons for Asc and Desc

Sorting - Using external buttons for Asc and Desc

btBillbtBill Posts: 31Questions: 0Answers: 0
edited June 2013 in General
Hello,

I would like to be able to sort my table using external links or buttons.
Currently I am :
[code]
var thisHeadinput = $('.dataTables_scrollHeadInner').find('table > thead > tr > th');
thisHeadinput.unbind('click.DT');
$('#accountSortList li').click(function() {
historyPage.table.fnSortListener($('#accountSortList'), 3);
});
[/code]

There is an external div with a list that has two LI links that say "Sort A->Z" and "Sort Z->A"
The links work to sort, but i can continue to click on either and it will switch the sorting depending on the previous sort.
(you can keep clicking the same link and it resorts by reversing the sort of the previous sort)

I want the LI links to just sort in one direction, no matter how many times you click the same link.

Is this possible? I dont see any info on how to specify a direction.

Thanks.

Replies

  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin
    Yes - use fnSort to have each element specifically sort the table as you need, rather than using the sort listener which will provide functions for shift clicking, changing sort direction etc which it sounds like you do not want.

    Allan
  • btBillbtBill Posts: 31Questions: 0Answers: 0
    Thanks.

    I was trying this after posting, but it was not working. I would sort the first time, but then not after that.
    THE ISSUE WAS.....
    I was using an OL with and
  • oblaobla Posts: 4Questions: 1Answers: 0
    Hi BtBill,

    Can you explain us your solution, or show us your table online, because i don't understand your explanation...

    I'm trying to use buttons or links, in the table's page but outside the table, to change the sorting.
    I would very grateful for your help !

    thanks,
  • oblaobla Posts: 4Questions: 1Answers: 0
    edited July 2013
    oups, sorry, i just find the solution here : http://datatables.net/forums/discussion/4067/sorting-with-a-hyperlink/p1

    You just have to create a div, a button... with an id (here "buttonId"), the you can sort the column as you want (asc or desc) :

    [code]
    var oTable = $('#example').dataTable();
    $('#buttonId').click(function(){
    oTable.fnSort([ [6,'asc']] );

    });
    [/code]

    then in your htm :
    Sort ascending on column 6
  • btBillbtBill Posts: 31Questions: 0Answers: 0
    edited July 2013
    The code I ended up using is:
    [code]
    allCols = {
    '2' : 'val1',
    '3' : 'val2',
    };

    for(var allCol in allCols) {
    (function() {
    // This element \/ is in a div with a ul: li ▲ Asc
    $('#'+allCols[allCol]+'SortList li').click(function() {
    historyPage.table.fnSort([index, $(this).attr('data-sortDir')]);
    });
    })();
    }
    [/code]
    And in case anyone is interested....
    I am using the filtering plugin.
    After some searching and testing trying to do range filtering, I ended up using the plugin, and then just moving the inputs that the default plugin generates and moving them into a div that appears when you rollover the header row.
    Seems to work well.
This discussion has been closed.