Change sort order after search

Change sort order after search

GeoJunkieGeoJunkie Posts: 10Questions: 3Answers: 1
edited March 2015 in Free community support

In my application, I am using server-side AJAX calls to display a DataTable. Column searches are using standard SQL, but the full-text search at the top is using a MATCH protocol that also returns a relevance value. I've included the relevance in the return object, but I want to be able to automatically change the sort order to the relevance column when, and only when, the user enters a search term in that box. Is there a way to do it? I tried using the search.dt event, but it replaces the entire search API and then doesn't make the server call.

EDIT: I managed to get a DataTables live example working without the server side. Hopefully it will give you an idea of what I'm trying to do.

http://live.datatables.net/xaxutehu/1/

This question has accepted answers - jump to:

Answers

  • GeoJunkieGeoJunkie Posts: 10Questions: 3Answers: 1

    Edited original post...now there's a live example at http://live.datatables.net/xaxutehu/1/

  • allanallan Posts: 61,839Questions: 1Answers: 10,134 Site admin

    but I want to be able to automatically change the sort order to the relevance column when, and only when, the user enters a search term in that box

    Have you tried binding an event handler to that input that will use order() to change the order when the user enters search data? You would probably want to check the current order applied (again with order()) to make sure you aren't setting the order unnecessarily.

    Allan

  • GeoJunkieGeoJunkie Posts: 10Questions: 3Answers: 1

    Sorry, I inadvertently clicked "No" for the question being answered. I'm going to test this when I get to work and see if I can make it work. It didn't work before, but I think I missed a step...

  • GeoJunkieGeoJunkie Posts: 10Questions: 3Answers: 1
    Answer ✓

    That did it! I added the following code which made it so the search sorts by relevance, but then lets the user select other sort columns:

        opportunityTable.on('search.dt', function(e){
            var searchText = opportunityTable.search();
            if (searchText && (searchText != lastSearch)){
                lastSearch = searchText;
                currOrder = opportunityTable.order();
                if ((currOrder[0][0] != 1) || (currOrder[0][1] != 'desc')){
                    opportunityTable.order([1, 'desc']);
                    opportunityTable.draw();
                }
            }
    

    I'm not sure how to go back and fix my accidental click, though. I want to change your answer to "yes it answered the question".

    Thanks!

  • allanallan Posts: 61,839Questions: 1Answers: 10,134 Site admin
    Answer ✓

    Unfortunately there isn't a way to do that without hacking around int he database, so I've just marked your own answer as the correct on.

    Good to hear you got it working :-)

    Allan

This discussion has been closed.